Skip to main content

rstask_core/
config.rs

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