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