From b4162a7fc0b6dbb9c2ad94c8b36063e7b56622ff Mon Sep 17 00:00:00 2001 From: Candifloss Date: Sat, 11 Oct 2025 16:32:30 +0530 Subject: [PATCH] Update `general` config - Refine `general` dection of config - Add pre-defined `api_version` values - Add default values --- owm_widg_config/src/general.rs | 40 ++++++++++++++++++++++++++++++++-- weatherd/src/main.rs | 13 +++++------ widget/src/main.rs | 7 +++--- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/owm_widg_config/src/general.rs b/owm_widg_config/src/general.rs index 3f05f4a..a3c1c5a 100644 --- a/owm_widg_config/src/general.rs +++ b/owm_widg_config/src/general.rs @@ -1,7 +1,43 @@ use serde::Deserialize; +const DEFAULT_CACHE_FILE: &str = "~/.cache/candydesktop/owm_widget.json"; + +#[derive(Debug, Deserialize, PartialEq)] +pub enum ApiVersion { + #[serde(rename = "free_2.5")] + Free25, + #[serde(rename = "onecall_3.0")] + OneCall30, + // Add other versions in the future: + // ... +} + +impl Default for ApiVersion { + fn default() -> Self { + Self::Free25 + } +} + +fn default_api_version() -> ApiVersion { + ApiVersion::default() +} + +fn default_cache_file() -> String { + DEFAULT_CACHE_FILE.to_string() +} + #[derive(Debug, Deserialize)] pub struct General { - pub api_version: String, // "free_2.5", "onecall_3.0", etc. - pub cache_file: Option, // Default: "~/.cache/candydesktop/owm_widget.json" + #[serde(default = "default_api_version")] + pub api_version: ApiVersion, + + #[serde(default = "default_cache_file")] + pub cache_file: String, +} + +impl General { + #[must_use] + pub fn api_version(&self) -> &ApiVersion { + &self.api_version + } } diff --git a/weatherd/src/main.rs b/weatherd/src/main.rs index 15946d4..2a5dec3 100644 --- a/weatherd/src/main.rs +++ b/weatherd/src/main.rs @@ -1,5 +1,6 @@ use owm_rs::free_api_v25::{current::WeatherResponse, query::QueryParams}; use owm_widg_config::config::Config; +use owm_widg_config::general::ApiVersion; use reqwest::blocking; use std::error::Error; use std::{fs, path::PathBuf, thread, time::Duration}; @@ -29,8 +30,8 @@ fn fetch_and_cache() -> Result<(), Box> { // Deserialize whole config let cfg: Config = toml::from_str(&toml_str)?; - match cfg.general.api_version.as_str() { - "free_2.5" => { + match cfg.general.api_version() { + ApiVersion::Free25 => { let query = QueryParams::from(cfg.query_params); let url = query.weather_url()?; @@ -38,11 +39,7 @@ fn fetch_and_cache() -> Result<(), Box> { let json_str = serde_json::to_string_pretty(&resp)?; // resolve cache path - let cache_path = expand_tilde( - cfg.general - .cache_file - .unwrap_or_else(|| "~/.cache/candydesktop/owm_widget.json".into()), - ); + let cache_path = expand_tilde(cfg.general.cache_file); if let Some(parent) = cache_path.parent() { fs::create_dir_all(parent)?; @@ -52,7 +49,7 @@ fn fetch_and_cache() -> Result<(), Box> { println!("weatherd: updated {}", cache_path.display()); } other => { - eprintln!("Unsupported api_version: {other}"); + return Err(format!("Unsupported api_version: {other:?}").into()); } } diff --git a/widget/src/main.rs b/widget/src/main.rs index cbe8335..8923086 100644 --- a/widget/src/main.rs +++ b/widget/src/main.rs @@ -1,5 +1,6 @@ use owm_rs::free_api_v25::current::WeatherResponse; use owm_widg_config::config::Config; +use owm_widg_config::general::ApiVersion; use std::fs; fn main() -> Result<(), Box> { @@ -13,8 +14,8 @@ fn main() -> Result<(), Box> { // Deserialize whole config let cfg: Config = toml::from_str(&toml_str)?; - match cfg.general.api_version.as_str() { - "free_2.5" => { + match cfg.general.api_version() { + ApiVersion::Free25 => { // Read json data from cache file let cache_path = dirs::cache_dir() .ok_or("No cache dir found")? @@ -49,7 +50,7 @@ fn main() -> Result<(), Box> { } } other => { - return Err(format!("Unsupported api_version: {other}").into()); + return Err(format!("Unsupported api_version: {other:?}").into()); } }