1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::env::var;
use std::path::PathBuf;

#[cfg(windows)]
fn base() -> PathBuf {
    match var("APPDATA") {
        Ok(val) => PathBuf::new(val),
        _ => PathBuf::new(""),
    }
}

#[cfg(any(target_os = "macos", target_os = "ios"))]
fn base() -> PathBuf {
    match var("HOME") {
        Ok(val) => PathBuf::from(val).join("Library").join("Application Support"),
        _ => PathBuf::from(""),
    }
}

#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))]
fn base() -> PathBuf {
    match var("XDG_CONFIG_HOME") {
        Ok(val) => PathBuf::from(val),
        _ => match var("HOME") {
            Ok(val) => PathBuf::from(val).join(".config"),
            _ => PathBuf::from(""),
        },
    }
}

pub fn get(appname: &str) -> PathBuf {
    let mut dir = base();
    dir.push(appname);

    match ::std::fs::create_dir_all(&dir) {
        Ok(()) => dir,
        _ => panic!("Unable to create config directory {}", dir.display()),
    }
}