1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4use shellexpand::tilde;
5
6pub 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#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
14pub struct PathConfig {
15 pub root_dir: PathBuf,
17 pub tasks_dir: PathBuf,
19 pub active_task_file: PathBuf,
21 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 assert!(config.root_dir.to_string_lossy().contains(".rutd"));
71
72 assert_eq!(config.tasks_dir, PathBuf::from("tasks"));
74
75 assert_eq!(config.active_task_file, PathBuf::from("active_task.toml"));
77
78 assert_eq!(config.log_file, PathBuf::from("rutd.log"));
80 }
81
82 #[test]
83 fn test_task_dir_path() {
84 let config = PathConfig {
85 root_dir: PathBuf::from("/test/root"),
87 ..Default::default()
88 };
89
90 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 root_dir: PathBuf::from("/test/root"),
100 ..Default::default()
101 };
102
103 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 root_dir: PathBuf::from("/test/root"),
113 ..Default::default()
114 };
115
116 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 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 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}