64 lines
2.1 KiB
Rust
64 lines
2.1 KiB
Rust
use owm_rs::free_api_v25::current::WeatherResponse;
|
|
use owm_widg_config::config::Config;
|
|
use owm_widg_config::general::ApiVersion;
|
|
use std::fs;
|
|
|
|
mod show_popup;
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let path = dirs::config_dir()
|
|
.ok_or("No config dir found")?
|
|
.join("candywidgets/openweathermap.toml");
|
|
|
|
let toml_str = fs::read_to_string(&path)
|
|
.map_err(|_| format!("Failed to read config: {}", path.display()))?;
|
|
|
|
let cfg: Config = toml::from_str(&toml_str)?;
|
|
|
|
match cfg.general.api_version() {
|
|
ApiVersion::Free25 => {
|
|
let cache_path = dirs::cache_dir()
|
|
.ok_or("No cache dir found")?
|
|
.join("candydesktop/owm_widget.json");
|
|
|
|
let json_data = fs::read_to_string(&cache_path)?;
|
|
let resp: WeatherResponse = serde_json::from_str(&json_data)?;
|
|
|
|
let city = resp.name.clone().unwrap_or_else(|| "Unknown".into());
|
|
let country = resp
|
|
.sys
|
|
.as_ref()
|
|
.and_then(|s| s.country.clone())
|
|
.unwrap_or_else(|| String::new());
|
|
|
|
let (weather_main, weather_description, icon) = if let Some(w) = resp.weather.first() {
|
|
(w.main.clone(), w.description.clone(), w.icon.clone())
|
|
} else {
|
|
("N/A".into(), "N/A".into(), String::new())
|
|
};
|
|
|
|
let temp = resp.main.as_ref().and_then(|m| m.temp).unwrap_or(0.0);
|
|
let temperature = format!("{temp:.1}");
|
|
let unit = match cfg.query_params.units.as_str() {
|
|
"metric" => 'C',
|
|
"imperial" => 'F',
|
|
"standard" => 'K',
|
|
_ => '?',
|
|
};
|
|
|
|
show_popup::show_popup(
|
|
city,
|
|
country,
|
|
weather_main,
|
|
weather_description,
|
|
icon,
|
|
temperature,
|
|
unit,
|
|
)?;
|
|
}
|
|
other => return Err(format!("Unsupported api_version: {other:?}").into()),
|
|
}
|
|
|
|
Ok(())
|
|
}
|