Something went wrong. Try again.
simple & configurable world clock app
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283use std::{fs, path};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]pub struct ClockInfo { pub name: Option<String>, #[serde(with = "crate::tz_serializer")] pub timezone: chrono_tz::Tz, pub color_code: Option<String>,}
impl ClockInfo { pub fn time(&self) -> chrono::NaiveDateTime { let current_time = chrono::Utc::now(); let adjusted = current_time.with_timezone(&self.timezone); adjusted.naive_local() }}
impl Default for ClockInfo { fn default() -> Self { Self { name: None, timezone: chrono_tz::Tz::UTC, color_code: None, } }}
#[derive(Debug, Serialize, Deserialize)]pub struct ConfigFile { pub clocks: Vec<ClockInfo>,}
impl ConfigFile { pub fn get_path() -> Option<path::PathBuf> { const CONFIG_FILE_NAME: &'static str = "config.toml"; let dirs = directories::ProjectDirs::from("uk", "reddust", "iceclock")?; Some(dirs.config_dir().join(CONFIG_FILE_NAME)) }
pub fn load_file() -> Option<Self> { let path = Self::get_path()?; let text = fs::read_to_string(path).ok()?; match toml::from_str(text.as_str()) { Ok(cfg) => cfg, Err(e) => { log::error!("Failed to load config! {}", e); None } } }
pub fn save_file(&self) { let path = match Self::get_path() { Some(p) => p, None => return, };
if let Some(dir) = path.parent() { log::info!("Storing config in: {}", dir.display()); let _ = fs::create_dir_all(dir); }
if let Ok(text) = toml::to_string_pretty(self) { let _ = fs::write(path, text); } }}
impl Default for ConfigFile { fn default() -> Self { Self { clocks: vec![ClockInfo { name: None, timezone: chrono_tz::Tz::UTC, color_code: None, }], } }}