rstask_core/
config.rs

1use std::env;
2use std::path::PathBuf;
3
4/// Configuration for the rstask application
5#[derive(Debug, Clone)]
6pub struct Config {
7    /// Path to the git repository
8    pub repo: PathBuf,
9    /// Path to the rstask local state file
10    pub state_file: PathBuf,
11    /// Path to the IDs file
12    pub ids_file: PathBuf,
13    /// Context from environment variable
14    pub ctx_from_env_var: Option<String>,
15}
16
17impl Config {
18    /// Creates a new Config from environment variables
19    pub fn new() -> Self {
20        let ctx_from_env_var = env::var("RSTASK_CONTEXT").ok();
21
22        let home = home::home_dir()
23            .or_else(|| env::var("HOME").ok().map(PathBuf::from))
24            .expect("Could not determine home directory");
25
26        let default_repo = home.join(".rstask");
27        let repo = env::var("RSTASK_GIT_REPO")
28            .map(PathBuf::from)
29            .unwrap_or(default_repo);
30
31        let state_file = repo.join(".git").join("rstask").join("state.bin");
32        let ids_file = repo.join(".git").join("rstask").join("ids.bin");
33
34        Config {
35            repo,
36            state_file,
37            ids_file,
38            ctx_from_env_var,
39        }
40    }
41}
42
43impl Default for Config {
44    fn default() -> Self {
45        Self::new()
46    }
47}