rutd_core/config/
path.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4use shellexpand::tilde;
5
6/// Default path constants
7pub const DEFAULT_ROOT_DIR: &str = "~/.rutd";
8pub const DEFAULT_TASKS_DIR: &str = "tasks";
9pub const DEFAULT_ACTIVE_FILE: &str = "active_task.toml";
10pub const DEFAULT_LOG_FILE: &str = "rutd.log";
11
12/// Path configuration management
13#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
14pub struct PathConfig {
15    /// Root directory path
16    pub root_dir: PathBuf,
17    /// Tasks directory path
18    pub tasks_dir: PathBuf,
19    /// Active task file path
20    pub active_task_file: PathBuf,
21    /// Log file path
22    pub log_file: PathBuf,
23}
24
25impl Default for PathConfig {
26    fn default() -> Self {
27        let root_dir = PathBuf::from(tilde(DEFAULT_ROOT_DIR).as_ref());
28        let tasks_dir = PathBuf::from(DEFAULT_TASKS_DIR);
29        let active_task_file = PathBuf::from(DEFAULT_ACTIVE_FILE);
30        let log_file = PathBuf::from(DEFAULT_LOG_FILE);
31
32        Self {
33            root_dir,
34            tasks_dir,
35            active_task_file,
36            log_file,
37        }
38    }
39}
40
41impl PathConfig {
42    pub fn root_path(&self) -> PathBuf {
43        self.root_dir.clone()
44    }
45
46    pub fn task_dir_path(&self) -> PathBuf {
47        self.root_dir.join(&self.tasks_dir)
48    }
49
50    pub fn active_task_file_path(&self) -> PathBuf {
51        self.root_dir.join(&self.active_task_file)
52    }
53
54    pub fn log_file_path(&self) -> PathBuf {
55        self.root_dir.join(&self.log_file)
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use std::path::Path;
62
63    use super::*;
64
65    #[test]
66    fn test_default_path_config() {
67        let config = PathConfig::default();
68
69        // Check root directory
70        assert!(config.root_dir.to_string_lossy().contains(".rutd"));
71
72        // Check tasks directory is "tasks"
73        assert_eq!(config.tasks_dir, PathBuf::from("tasks"));
74
75        // Check active task file is "active_task.toml"
76        assert_eq!(config.active_task_file, PathBuf::from("active_task.toml"));
77
78        // Check log file is "rutd.log"
79        assert_eq!(config.log_file, PathBuf::from("rutd.log"));
80    }
81
82    #[test]
83    fn test_task_dir_path() {
84        let config = PathConfig {
85            // Set a specific root directory for testing
86            root_dir: PathBuf::from("/test/root"),
87            ..Default::default()
88        };
89
90        // Check task directory path
91        let task_dir = config.task_dir_path();
92        assert_eq!(task_dir, Path::new("/test/root/tasks"));
93    }
94
95    #[test]
96    fn test_active_task_file_path() {
97        let config = PathConfig {
98            // Set a specific root directory for testing
99            root_dir: PathBuf::from("/test/root"),
100            ..Default::default()
101        };
102
103        // Check active task file path
104        let active_task_file = config.active_task_file_path();
105        assert_eq!(active_task_file, Path::new("/test/root/active_task.toml"));
106    }
107
108    #[test]
109    fn test_log_file_path() {
110        let config = PathConfig {
111            // Set a specific root directory for testing
112            root_dir: PathBuf::from("/test/root"),
113            ..Default::default()
114        };
115
116        // Check log file path
117        let log_file = config.log_file_path();
118        assert_eq!(log_file, Path::new("/test/root/rutd.log"));
119    }
120
121    #[test]
122    fn test_custom_paths() {
123        // Create a custom path configuration
124        let config = PathConfig {
125            root_dir: PathBuf::from("/custom/root"),
126            tasks_dir: PathBuf::from("custom_tasks"),
127            active_task_file: PathBuf::from("custom_active.toml"),
128            log_file: PathBuf::from("custom.log"),
129        };
130
131        // Check paths
132        assert_eq!(
133            config.task_dir_path(),
134            Path::new("/custom/root/custom_tasks")
135        );
136        assert_eq!(
137            config.active_task_file_path(),
138            Path::new("/custom/root/custom_active.toml")
139        );
140        assert_eq!(config.log_file_path(), Path::new("/custom/root/custom.log"));
141    }
142}