From 8661ea87db4b4911fdcb3172799713a29226afef Mon Sep 17 00:00:00 2001 From: Candifloss Date: Thu, 18 Sep 2025 19:47:33 +0530 Subject: [PATCH] Load TOML Config - Read `toml` file in the config dir - Parse `toml` config in the file --- .gitignore | 6 ++++++ Cargo.toml | 10 ++++++++++ src/main.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore index ab951f8..d95268b 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,9 @@ Cargo.lock # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + + +# Added by cargo + +/target +/test diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..cada2f1 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "openweatherwidget" +version = "0.0.1" +edition = "2024" + +[dependencies] +toml = "0.9.6" +dirs = "6.0.0" +serde = { version = "1.0.225", features = ["derive"] } + diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..9460572 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,30 @@ +use std::fs; +use std::path::PathBuf; +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +struct General { + api_key: String, + city_id: String, +} + +#[derive(Debug, Deserialize)] +struct Config { + general: General, +} + +fn main() { + let mut config_file_path = PathBuf::from( + dirs::config_dir().expect("No config dir found") + ); + config_file_path.push("candywidgets/openweathermap.toml"); + + let toml_content = fs::read_to_string(&config_file_path) + .expect("Failed to read config file"); + + let config: Config = toml::from_str(&toml_content) + .expect("Failed to parse TOML"); + + println!("City ID: {}", config.general.city_id); + println!("API Key: {}", config.general.api_key); +}