Skip to main content

aster/config/
paths.rs

1use etcetera::{choose_app_strategy, AppStrategy, AppStrategyArgs};
2use std::path::PathBuf;
3
4pub struct Paths;
5
6impl Paths {
7    fn get_dir(dir_type: DirType) -> PathBuf {
8        if let Ok(test_root) = std::env::var("ASTER_PATH_ROOT") {
9            let base = PathBuf::from(test_root);
10            match dir_type {
11                DirType::Config => base.join("config"),
12                DirType::Data => base.join("data"),
13                DirType::State => base.join("state"),
14            }
15        } else {
16            let strategy = choose_app_strategy(AppStrategyArgs {
17                top_level_domain: "Block".to_string(),
18                author: "Block".to_string(),
19                app_name: "aster".to_string(),
20            })
21            .expect("aster requires a home dir");
22
23            match dir_type {
24                DirType::Config => strategy.config_dir(),
25                DirType::Data => strategy.data_dir(),
26                DirType::State => strategy.state_dir().unwrap_or(strategy.data_dir()),
27            }
28        }
29    }
30
31    pub fn config_dir() -> PathBuf {
32        Self::get_dir(DirType::Config)
33    }
34
35    pub fn data_dir() -> PathBuf {
36        Self::get_dir(DirType::Data)
37    }
38
39    pub fn state_dir() -> PathBuf {
40        Self::get_dir(DirType::State)
41    }
42
43    pub fn in_state_dir(subpath: &str) -> PathBuf {
44        Self::state_dir().join(subpath)
45    }
46
47    pub fn in_config_dir(subpath: &str) -> PathBuf {
48        Self::config_dir().join(subpath)
49    }
50
51    pub fn in_data_dir(subpath: &str) -> PathBuf {
52        Self::data_dir().join(subpath)
53    }
54}
55
56enum DirType {
57    Config,
58    Data,
59    State,
60}