Load TOML Config

- Read `toml` file in the config dir
- Parse `toml` config in the file
This commit is contained in:
Candifloss 2025-09-18 19:47:33 +05:30
parent 721d491bd3
commit 8661ea87db
3 changed files with 46 additions and 0 deletions

6
.gitignore vendored
View File

@ -20,3 +20,9 @@ Cargo.lock
# and can be added to the global gitignore or merged into this file. For a more nuclear # 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. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
# Added by cargo
/target
/test

10
Cargo.toml Normal file
View File

@ -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"] }

30
src/main.rs Normal file
View File

@ -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);
}