1use std::{
2 env, fs,
3 path::{Path, PathBuf},
4};
5
6use crate::errors::ConfigError;
7
8pub const ASSETS_DIR_NAME: &str = "assets";
9pub const CACHE_DIR_NAME: &str = "cache";
10pub const DEFAULT_GRAPH_DB_NAME: &str = "default.db";
11
12#[derive(Clone, Debug, PartialEq, Eq)]
13pub struct Workspace {
14 base_dir: PathBuf,
15}
16
17impl Workspace {
18 pub fn new(base_dir: impl Into<PathBuf>) -> Self {
19 Self {
20 base_dir: base_dir.into(),
21 }
22 }
23
24 pub fn from_current_dir() -> Self {
25 Self::new(env::current_dir().unwrap())
26 }
27
28 pub fn base_dir(&self) -> &Path {
29 &self.base_dir
30 }
31
32 pub fn ensure_gen_dir(&self) -> PathBuf {
33 let gen_path = self.base_dir.join(".gen");
34 ensure_dir(&gen_path);
35 let assets = gen_path.join(ASSETS_DIR_NAME);
36 ensure_dir(&assets);
37 gen_path
38 }
39
40 pub fn find_gen_dir(&self) -> Option<PathBuf> {
41 let mut cur_dir = self.base_dir.as_path();
42 let mut gen_path = cur_dir.join(".gen");
43 while !gen_path.is_dir() {
44 cur_dir = cur_dir.parent()?;
45 gen_path = cur_dir.join(".gen");
46 }
47 Some(gen_path)
48 }
49
50 pub fn repo_root(&self) -> Result<PathBuf, ConfigError> {
51 let gen_dir = self
52 .find_gen_dir()
53 .ok_or(ConfigError::GenDirectoryNotFound)?;
54
55 gen_dir
56 .parent()
57 .map(Path::to_path_buf)
58 .ok_or(ConfigError::RepoRootNotFound)
59 }
60
61 pub fn gen_db_path(&self) -> Result<PathBuf, ConfigError> {
62 self.find_gen_dir()
63 .map(|dir| dir.join("gen.db"))
64 .ok_or(ConfigError::GenDirectoryNotFound)
65 }
66
67 pub fn graph_db_path(&self) -> Result<PathBuf, ConfigError> {
68 self.find_gen_dir()
69 .map(|dir| dir.join(DEFAULT_GRAPH_DB_NAME))
70 .ok_or(ConfigError::GenDirectoryNotFound)
71 }
72
73 pub fn asset_dir(&self) -> Result<PathBuf, ConfigError> {
74 Ok(self
75 .find_gen_dir()
76 .ok_or(ConfigError::GenDirectoryNotFound)?
77 .join(ASSETS_DIR_NAME))
78 }
79
80 pub fn find_cache_dir(&self) -> Option<PathBuf> {
85 self.find_gen_dir().map(|dir| dir.join(CACHE_DIR_NAME))
86 }
87
88 pub fn ensure_cache_dir(&self) -> Result<PathBuf, ConfigError> {
93 let dir = self
94 .find_cache_dir()
95 .ok_or(ConfigError::GenDirectoryNotFound)?;
96 ensure_dir(&dir);
97 Ok(dir)
98 }
99
100 pub fn find_search_index(&self) -> Option<PathBuf> {
101 self.find_gen_dir().map(|d| d.join("search_index"))
102 }
103
104 pub fn ensure_search_index(&self) -> Result<PathBuf, ConfigError> {
105 let dir = self
106 .find_gen_dir()
107 .ok_or(ConfigError::GenDirectoryNotFound)?
108 .join("search_index");
109 ensure_dir(&dir);
110 Ok(dir)
111 }
112}
113
114fn ensure_dir(path: &Path) {
115 if !path.is_dir() {
116 fs::create_dir_all(path).unwrap();
117 }
118}
119
120#[cfg(test)]
121mod tests {
122 use std::fs;
123
124 use tempfile::tempdir;
125
126 use super::*;
127
128 #[test]
129 fn ensure_gen_dir_creates_directory() {
130 let tmp_dir = tempdir().unwrap();
131 let tmp_dir_path = tmp_dir.path().to_path_buf();
132 let workspace = Workspace::new(&tmp_dir_path);
133
134 let gen_dir = workspace.ensure_gen_dir();
135
136 assert_eq!(gen_dir, tmp_dir_path.join(".gen"));
137 assert!(gen_dir.is_dir());
138 }
139
140 #[test]
141 fn find_gen_dir_walks_up_tree() {
142 let tmp_dir = tempdir().unwrap();
143 let tmp_dir_path = tmp_dir.path().to_path_buf();
144 let root_workspace = Workspace::new(&tmp_dir_path);
145 let gen_dir = root_workspace.ensure_gen_dir();
146
147 let nested_dir = tmp_dir_path.join("nested").join("deep");
148 fs::create_dir_all(&nested_dir).unwrap();
149 let nested_workspace = Workspace::new(&nested_dir);
150
151 assert_eq!(nested_workspace.find_gen_dir(), Some(gen_dir));
152 }
153
154 #[test]
155 fn repo_root_returns_parent_of_gen_dir() {
156 let tmp_dir = tempdir().unwrap();
157 let tmp_dir_path = tmp_dir.path().to_path_buf();
158 let workspace = Workspace::new(&tmp_dir_path);
159 workspace.ensure_gen_dir();
160
161 assert_eq!(workspace.repo_root().unwrap(), tmp_dir_path);
162 }
163
164 #[test]
165 fn repo_root_errors_when_missing_gen_dir() {
166 let tmp_dir = tempdir().unwrap();
167 let tmp_dir_path = tmp_dir.path().to_path_buf();
168 let workspace = Workspace::new(&tmp_dir_path);
169
170 assert_eq!(
171 Err(ConfigError::GenDirectoryNotFound),
172 workspace.repo_root()
173 );
174 }
175
176 #[test]
177 fn gen_db_path_resolves_inside_gen_dir() {
178 let tmp_dir = tempdir().unwrap();
179 let tmp_dir_path = tmp_dir.path().to_path_buf();
180 let workspace = Workspace::new(&tmp_dir_path);
181 let gen_dir = workspace.ensure_gen_dir();
182
183 assert_eq!(workspace.gen_db_path().unwrap(), gen_dir.join("gen.db"));
184 }
185
186 #[test]
187 fn test_graph_db_path_resolves_inside_gen_dir() {
188 let tmp_dir = tempdir().unwrap();
189 let tmp_dir_path = tmp_dir.path().to_path_buf();
190 let workspace = Workspace::new(&tmp_dir_path);
191 let gen_dir = workspace.ensure_gen_dir();
192
193 assert_eq!(
194 workspace.graph_db_path().unwrap(),
195 gen_dir.join(DEFAULT_GRAPH_DB_NAME)
196 );
197 }
198
199 #[test]
200 fn asset_dir_creates_assets_directory() {
201 let tmp_dir = tempdir().unwrap();
202 let tmp_dir_path = tmp_dir.path().to_path_buf();
203 let workspace = Workspace::new(&tmp_dir_path);
204 workspace.ensure_gen_dir();
205
206 let asset_dir = workspace.asset_dir().unwrap();
207
208 assert_eq!(asset_dir, tmp_dir_path.join(".gen").join(ASSETS_DIR_NAME));
209 assert!(asset_dir.is_dir());
210 }
211
212 #[test]
213 fn test_ensure_cache_dir_creates_cache_directory_lazily() {
214 let tmp_dir = tempdir().unwrap();
215 let workspace = Workspace::new(tmp_dir.path());
216 let gen_dir = workspace.ensure_gen_dir();
217 let cache_dir = gen_dir.join(CACHE_DIR_NAME);
218 assert!(!cache_dir.exists());
219
220 assert_eq!(workspace.ensure_cache_dir().unwrap(), cache_dir);
221 assert!(cache_dir.is_dir());
222 }
223}