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 {
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<xdg::BaseDirectoriesError> for ConfigError {
fn from(err: xdg::BaseDirectoriesError) -> Self {
ConfigError::XDG(err)
}
}
+#[cfg(windows)]
+impl From<std::env::VarError> for ConfigError {
+ fn from(err: std::env::VarError) -> Self {
+ ConfigError::Env(err)
+ }
+}
+
+#[cfg(windows)]
+impl From<std::convert::Infallible> 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<Self, ConfigError> {
let base_dirs = xdg::BaseDirectories::with_prefix("mastodonochrome")?;
Ok(ConfigLocation {
})
}
+ #[cfg(windows)]
+ pub fn default() -> Result<Self, ConfigError> {
+ 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,