1use crate::logs::{Log, LogLevel};
3use directories::ProjectDirs;
4use std::{
5 fs::create_dir_all,
6 path::{Path, PathBuf},
7};
8
9#[derive(Debug, Clone)]
10pub struct CatshDirs {
11 config_dir: PathBuf,
12 cache_dir: PathBuf,
13 data_dir: PathBuf,
14}
15
16impl CatshDirs {
17 pub fn load() -> Self {
18 if let Some(catsh_dirs) = ProjectDirs::from("com", "catsh", "catsh") {
19 Self {
20 config_dir: catsh_dirs.config_dir().to_owned(),
21 cache_dir: catsh_dirs.cache_dir().to_owned(),
22 data_dir: catsh_dirs.data_dir().to_owned(),
23 }
24 } else {
25 Self {
26 config_dir: PathBuf::new(),
27 cache_dir: PathBuf::new(),
28 data_dir: PathBuf::new(),
29 }
30 }
31 }
32
33 pub fn verify(self) -> Self {
34 if !self.config_dir.exists() {
35 match create_dir_all(self.config_dir.to_owned()) {
36 Ok(_) => {
37 let _ = Log::new(LogLevel::Ok, 0, "Created directory");
38 }
39 Err(_) => {
40 Log::new(LogLevel::Error, 1, "Error for create directory")
41 .show();
42 }
43 }
44 } else {
45 Log::new(LogLevel::Ok, 0, "The directory exist");
46 }
47
48 if !self.cache_dir.exists() {
49 match create_dir_all(self.cache_dir.to_owned()) {
50 Ok(_) => {
51 let _ = Log::new(LogLevel::Ok, 0, "Created directory");
52 }
53 Err(_) => {
54 Log::new(LogLevel::Error, 1, "Error for create directory")
55 .show();
56 }
57 }
58 } else {
59 Log::new(LogLevel::Ok, 0, "The directory exist");
60 }
61
62 if !self.data_dir.exists() {
63 match create_dir_all(self.data_dir.to_owned()) {
64 Ok(_) => {
65 let _ = Log::new(LogLevel::Ok, 0, "Created data directory");
66 }
67 Err(_) => {
68 Log::new(
69 LogLevel::Error,
70 1,
71 "Error for create data directory",
72 )
73 .show();
74 }
75 }
76 } else {
77 Log::new(LogLevel::Ok, 0, "The directory exist");
78 }
79 self
80 }
81
82 pub fn config_dir(&self) -> &Path {
83 self.config_dir.as_path()
84 }
85
86 pub fn cache_dir(&self) -> &Path {
87 self.cache_dir.as_path()
88 }
89
90 pub fn data_dir(&self) -> &Path {
91 self.data_dir.as_path()
92 }
93}
94
95#[cfg(test)]
96mod tests {
97 use std::env;
98
99 use super::*;
100
101 #[test]
102 fn test_configdir_load() {
103 let dirs = CatshDirs::load();
104
105 #[cfg(target_os = "linux")]
107 assert_eq!(
108 dirs.config_dir.to_str().unwrap(),
109 format!("{}/.config/catsh", env::var("HOME").unwrap()).as_str()
110 );
111
112 #[cfg(target_os = "windows")]
113 assert_eq!(
114 dirs.config_dir.to_str().unwrap(),
115 format!("{}\\catsh\\catsh\\config", env::var("APPDATA").unwrap())
116 .as_str()
117 );
118
119 #[cfg(target_os = "macos")]
120 assert_eq!(
121 dirs.config_dir.to_str().unwrap(),
122 format!(
123 "{}/Library/Application Support/com.catsh.catsh",
124 env::var("HOME").unwrap()
125 )
126 .as_str()
127 );
128 }
129
130 #[test]
131 fn test_datadir_load() {
132 let dirs = CatshDirs::load();
133
134 #[cfg(target_os = "linux")]
136 assert_eq!(
137 dirs.data_dir.to_str().unwrap(),
138 format!("{}/.local/share/catsh", env::var("HOME").unwrap())
139 .as_str()
140 );
141
142 #[cfg(target_os = "windows")]
143 assert_eq!(
144 dirs.data_dir.to_str().unwrap(),
145 format!(
146 "{}\\AppData\\Roaming\\catsh\\catsh\\data",
147 env::var("USERPROFILE").unwrap()
148 )
149 .as_str()
150 );
151
152 #[cfg(target_os = "macos")]
153 assert_eq!(
154 dirs.data_dir.to_str().unwrap(),
155 format!(
156 "{}/Library/Application Support/com.catsh.catsh",
157 env::var("HOME").unwrap()
158 )
159 .as_str()
160 );
161 }
162
163 #[test]
164 fn test_cachedir_load() {
165 let dirs = CatshDirs::load();
166
167 #[cfg(target_os = "linux")]
169 assert_eq!(
170 dirs.cache_dir.to_str().unwrap(),
171 format!("{}/.cache/catsh", env::var("HOME").unwrap()).as_str()
172 );
173
174 #[cfg(target_os = "windows")]
175 assert_eq!(
176 dirs.cache_dir.to_str().unwrap(),
177 format!(
178 "{}\\AppData\\Local\\catsh\\catsh\\cache",
179 env::var("USERPROFILE").unwrap()
180 )
181 .as_str()
182 );
183
184 #[cfg(target_os = "macos")]
185 assert_eq!(
186 dirs.cache_dir.to_str().unwrap(),
187 format!(
188 "{}/Library/Caches/com.catsh.catsh",
189 env::var("HOME").unwrap()
190 )
191 .as_str()
192 );
193 }
194}