Skip to main content

things3_cloud/
dirs.rs

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