From 1932ad1187f12eba963f0978a83affaf6d979dd1 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Tue, 7 Jan 2025 14:51:33 +0100 Subject: [PATCH] os struct --- src/lib.rs | 5 ++++- src/os.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/os.rs diff --git a/src/lib.rs b/src/lib.rs index b6c49bc..8d4ef6a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,11 @@ pub mod cli; -pub mod parsing; pub mod models; +pub mod os; +pub mod parsing; pub mod table; +const APP_NAME: &'static str = "ECB-rates"; + pub mod ecb_url { pub const TODAY: &'static str = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; diff --git a/src/os.rs b/src/os.rs new file mode 100644 index 0000000..bca17d6 --- /dev/null +++ b/src/os.rs @@ -0,0 +1,47 @@ +use std::env; +use std::path::PathBuf; + +use crate::APP_NAME; + +pub enum Os { + Windows, + Mac, + Unix, +} + +impl Os { + pub fn get_current() -> Option { + let os_str = env::consts::OS; + if os_str == "windows" { + Some(Os::Windows) + } else if os_str == "macos" { + Some(Os::Mac) + } else if os_str == "linux" || os_str.contains("bsd") { + Some(Os::Unix) + } else { + None + } + } + + pub fn get_config_path(&self) -> Result { + let config_home = match self { + Os::Windows => PathBuf::from(env::var("APPDATA")?), + Os::Mac => { + let mut pb = PathBuf::from(env::var("HOME")?); + pb.push("Library"); + pb.push("Application Support"); + pb + } + Os::Unix => match env::var("XDG_CONFIG_HOME") { + Ok(k) => PathBuf::from(k), + Err(_) => { + let mut home = PathBuf::from(env::var("HOME")?); + home.push(".config"); + home + } + }, + }; + + Ok(config_home.join(APP_NAME)) + } +}