From acff7a48733d672e51746912143dc96c960e0545 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 31 Dec 2023 14:29:20 +0000 Subject: [PATCH] Make this program build on Windows! I tried it out of pure curiosity. As I expected, the 'xdg' crate didn't work (I already knew it was documented to not work on Windows), but everything else was fine, and it compiles and runs! --- Cargo.toml | 2 ++ src/config.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index c425f21..ca75ce1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,6 @@ reqwest = { version = "0.11.23", features = ["blocking"] } serde = { version = "1.0.193", features = ["derive"] } serde_json = "1.0.108" unicode-width = "0.1.5" + +[target.'cfg(unix)'.dependencies] xdg = "2.5.2" diff --git a/src/config.rs b/src/config.rs index 70e1ab4..e6c5010 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,8 +1,15 @@ use std::path::PathBuf; +#[cfg(windows)] +use std::str::FromStr; + #[derive(Debug)] pub enum ConfigError { + #[cfg(unix)] XDG(xdg::BaseDirectoriesError), + + #[cfg(windows)] + Env(std::env::VarError), } impl std::fmt::Display for ConfigError { @@ -10,22 +17,46 @@ impl std::fmt::Display for ConfigError { Result<(), std::fmt::Error> { match self { + #[cfg(unix)] ConfigError::XDG(e) => { e.fmt(f) }, + + #[cfg(windows)] + ConfigError::Env(e) => { + // FIXME: how _should_ we include the information + // about what environment variable? + write!(f, "%APPDATA%: {}", e) + }, } } } +#[cfg(unix)] impl From for ConfigError { fn from(err: xdg::BaseDirectoriesError) -> Self { ConfigError::XDG(err) } } +#[cfg(windows)] +impl From for ConfigError { + fn from(err: std::env::VarError) -> Self { + ConfigError::Env(err) + } +} + +#[cfg(windows)] +impl From for ConfigError { + fn from(_: std::convert::Infallible) -> Self { + panic!("I thought you said it was infallible?"); + } +} + pub struct ConfigLocation { dir: PathBuf, } impl ConfigLocation { + #[cfg(unix)] pub fn default() -> Result { let base_dirs = xdg::BaseDirectories::with_prefix("mastodonochrome")?; Ok(ConfigLocation { @@ -33,6 +64,17 @@ impl ConfigLocation { }) } + #[cfg(windows)] + pub fn default() -> Result { + let appdata = std::env::var("APPDATA")?; + let mut dir = PathBuf::from_str(&appdata)?; + dir.push("mastodonochrome"); + + Ok(ConfigLocation { + dir: dir, + }) + } + pub fn from_pathbuf(dir: PathBuf) -> Self { ConfigLocation { dir: dir, -- 2.30.2