Update general
config
- Refine `general` dection of config - Add pre-defined `api_version` values - Add default values
This commit is contained in:
parent
ebbbf7533f
commit
b4162a7fc0
@ -1,7 +1,43 @@
|
|||||||
use serde::Deserialize;
|
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)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct General {
|
pub struct General {
|
||||||
pub api_version: String, // "free_2.5", "onecall_3.0", etc.
|
#[serde(default = "default_api_version")]
|
||||||
pub cache_file: Option<String>, // Default: "~/.cache/candydesktop/owm_widget.json"
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use owm_rs::free_api_v25::{current::WeatherResponse, query::QueryParams};
|
use owm_rs::free_api_v25::{current::WeatherResponse, query::QueryParams};
|
||||||
use owm_widg_config::config::Config;
|
use owm_widg_config::config::Config;
|
||||||
|
use owm_widg_config::general::ApiVersion;
|
||||||
use reqwest::blocking;
|
use reqwest::blocking;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::{fs, path::PathBuf, thread, time::Duration};
|
use std::{fs, path::PathBuf, thread, time::Duration};
|
||||||
@ -29,8 +30,8 @@ fn fetch_and_cache() -> Result<(), Box<dyn Error>> {
|
|||||||
// Deserialize whole config
|
// Deserialize whole config
|
||||||
let cfg: Config = toml::from_str(&toml_str)?;
|
let cfg: Config = toml::from_str(&toml_str)?;
|
||||||
|
|
||||||
match cfg.general.api_version.as_str() {
|
match cfg.general.api_version() {
|
||||||
"free_2.5" => {
|
ApiVersion::Free25 => {
|
||||||
let query = QueryParams::from(cfg.query_params);
|
let query = QueryParams::from(cfg.query_params);
|
||||||
|
|
||||||
let url = query.weather_url()?;
|
let url = query.weather_url()?;
|
||||||
@ -38,11 +39,7 @@ fn fetch_and_cache() -> Result<(), Box<dyn Error>> {
|
|||||||
let json_str = serde_json::to_string_pretty(&resp)?;
|
let json_str = serde_json::to_string_pretty(&resp)?;
|
||||||
|
|
||||||
// resolve cache path
|
// resolve cache path
|
||||||
let cache_path = expand_tilde(
|
let cache_path = expand_tilde(cfg.general.cache_file);
|
||||||
cfg.general
|
|
||||||
.cache_file
|
|
||||||
.unwrap_or_else(|| "~/.cache/candydesktop/owm_widget.json".into()),
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Some(parent) = cache_path.parent() {
|
if let Some(parent) = cache_path.parent() {
|
||||||
fs::create_dir_all(parent)?;
|
fs::create_dir_all(parent)?;
|
||||||
@ -52,7 +49,7 @@ fn fetch_and_cache() -> Result<(), Box<dyn Error>> {
|
|||||||
println!("weatherd: updated {}", cache_path.display());
|
println!("weatherd: updated {}", cache_path.display());
|
||||||
}
|
}
|
||||||
other => {
|
other => {
|
||||||
eprintln!("Unsupported api_version: {other}");
|
return Err(format!("Unsupported api_version: {other:?}").into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use owm_rs::free_api_v25::current::WeatherResponse;
|
use owm_rs::free_api_v25::current::WeatherResponse;
|
||||||
use owm_widg_config::config::Config;
|
use owm_widg_config::config::Config;
|
||||||
|
use owm_widg_config::general::ApiVersion;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
@ -13,8 +14,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
// Deserialize whole config
|
// Deserialize whole config
|
||||||
let cfg: Config = toml::from_str(&toml_str)?;
|
let cfg: Config = toml::from_str(&toml_str)?;
|
||||||
|
|
||||||
match cfg.general.api_version.as_str() {
|
match cfg.general.api_version() {
|
||||||
"free_2.5" => {
|
ApiVersion::Free25 => {
|
||||||
// Read json data from cache file
|
// Read json data from cache file
|
||||||
let cache_path = dirs::cache_dir()
|
let cache_path = dirs::cache_dir()
|
||||||
.ok_or("No cache dir found")?
|
.ok_or("No cache dir found")?
|
||||||
@ -49,7 +50,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
other => {
|
other => {
|
||||||
return Err(format!("Unsupported api_version: {other}").into());
|
return Err(format!("Unsupported api_version: {other:?}").into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user