os struct

This commit is contained in:
Love 2025-01-07 14:51:33 +01:00
parent 6c0639c6a3
commit 1932ad1187
2 changed files with 51 additions and 1 deletions

View File

@ -1,8 +1,11 @@
pub mod cli; pub mod cli;
pub mod parsing;
pub mod models; pub mod models;
pub mod os;
pub mod parsing;
pub mod table; pub mod table;
const APP_NAME: &'static str = "ECB-rates";
pub mod ecb_url { pub mod ecb_url {
pub const TODAY: &'static str = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; pub const TODAY: &'static str = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";

47
src/os.rs Normal file
View File

@ -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<Self> {
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<PathBuf, std::env::VarError> {
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))
}
}