Load TOML Config
- Read `toml` file in the config dir - Parse `toml` config in the file
This commit is contained in:
parent
721d491bd3
commit
8661ea87db
6
.gitignore
vendored
6
.gitignore
vendored
@ -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
10
Cargo.toml
Normal 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
30
src/main.rs
Normal 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);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user