Skip to main content

things3_cloud/
dirs.rs

1use std::fs;
2use std::path::PathBuf;
3
4const APP_NAME: &str = "things3";
5const LEGACY_APP_NAME: &str = "things-cli";
6
7fn xdg_state_home() -> PathBuf {
8    if let Ok(custom) = std::env::var("XDG_STATE_HOME") {
9        return PathBuf::from(custom);
10    }
11    dirs::home_dir()
12        .unwrap_or_else(|| PathBuf::from("."))
13        .join(".local")
14        .join("state")
15}
16
17pub fn app_state_dir() -> PathBuf {
18    let state_home = xdg_state_home();
19    let target = state_home.join(APP_NAME);
20    let legacy = state_home.join(LEGACY_APP_NAME);
21
22    if target.exists() || !legacy.exists() {
23        return target;
24    }
25
26    if fs::rename(&legacy, &target).is_ok() {
27        return target;
28    }
29
30    target
31}
32
33pub fn append_log_dir() -> PathBuf {
34    app_state_dir().join("append-log")
35}
36
37pub fn auth_file_path() -> PathBuf {
38    app_state_dir().join("auth.json")
39}