1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
use std::{collections::HashMap, net::SocketAddr, path::PathBuf};
use just_enough_vcs::{
lib::{
data::{
local::{
LocalWorkspace,
cached_sheet::CachedSheet,
latest_file_data::LatestFileData,
latest_info::LatestInfo,
local_sheet::{LocalSheet, LocalSheetData},
workspace_analyzer::{AnalyzeResult, AnalyzeResultPure},
workspace_config::LocalConfig,
},
member::MemberId,
sheet::{SheetData, SheetName},
vault::vault_config::VaultUuid,
},
env::current_local_path,
},
utils::cfg_file::config::ConfigFile,
};
use crate::systems::cmd::errors::CmdPrepareError;
/// Temporarily enter a directory to execute a block of code, then return to the original directory
macro_rules! entry_dir {
($path:expr, $block:block) => {{
let original_dir = std::env::current_dir().unwrap();
std::env::set_current_dir($path).unwrap();
let result = $block;
std::env::set_current_dir(original_dir).unwrap();
result
}};
}
#[derive(Default)]
pub struct LocalWorkspaceReader {
cached_sheet: HashMap<SheetName, SheetData>,
current_dir: Option<PathBuf>,
workspace_dir: Option<PathBuf>,
latest_file_data: HashMap<MemberId, LatestFileData>,
latest_info: Option<LatestInfo>,
local_config: Option<LocalConfig>,
local_sheet_data: HashMap<(MemberId, SheetName), LocalSheetData>,
local_workspace: Option<LocalWorkspace>,
}
impl LocalWorkspaceReader {
// Get the current directory
pub fn current_dir(&mut self) -> Result<&PathBuf, CmdPrepareError> {
if self.current_dir.is_none() {
let current_dir = std::env::current_dir()?;
self.current_dir = Some(current_dir);
}
Ok(self.current_dir.as_ref().unwrap())
}
// Get the workspace directory
pub fn workspace_dir(&mut self) -> Result<&PathBuf, CmdPrepareError> {
if self.workspace_dir.is_none() {
let workspace_dir = current_local_path();
self.workspace_dir = workspace_dir;
return match &self.workspace_dir {
Some(d) => Ok(d),
None => Err(CmdPrepareError::LocalWorkspaceNotFound),
};
}
Ok(self.workspace_dir.as_ref().unwrap())
}
// Get the local configuration
pub async fn local_config(&mut self) -> Result<&LocalConfig, CmdPrepareError> {
if self.local_config.is_none() {
let workspace_dir = self.workspace_dir()?;
let local_config = entry_dir!(workspace_dir, {
LocalConfig::read()
.await
.map_err(|_| CmdPrepareError::LocalConfigNotFound)?
});
self.local_config = Some(local_config)
}
Ok(self.local_config.as_ref().unwrap())
}
// Current account
pub async fn current_account(&mut self) -> Result<MemberId, CmdPrepareError> {
Ok(self.local_config().await?.current_account())
}
// Whether it is in host mode
pub async fn is_host_mode(&mut self) -> Result<bool, CmdPrepareError> {
Ok(self.local_config().await?.is_host_mode())
}
// Whether the workspace is stained
pub async fn workspace_stained(&mut self) -> Result<bool, CmdPrepareError> {
Ok(self.local_config().await?.stained())
}
// Stain UUID
pub async fn stained_uuid(&mut self) -> Result<Option<VaultUuid>, CmdPrepareError> {
Ok(self.local_config().await?.stained_uuid())
}
// Upstream address
pub async fn upstream_addr(&mut self) -> Result<SocketAddr, CmdPrepareError> {
Ok(self.local_config().await?.upstream_addr())
}
// Currently used sheet
pub async fn sheet_in_use(&mut self) -> Result<&Option<SheetName>, CmdPrepareError> {
Ok(self.local_config().await?.sheet_in_use())
}
// Get the sheet name in use, or error if none
pub async fn sheet_name(&mut self) -> Result<SheetName, CmdPrepareError> {
match self.local_config().await?.sheet_in_use() {
Some(name) => Ok(name.clone()),
None => Err(CmdPrepareError::NoSheetInUse),
}
}
// Current draft folder
pub async fn current_draft_folder(&mut self) -> Result<Option<PathBuf>, CmdPrepareError> {
Ok(self.local_config().await?.current_draft_folder())
}
// Get the local workspace
pub async fn local_workspace(&mut self) -> Result<&LocalWorkspace, CmdPrepareError> {
if self.local_workspace.is_none() {
let workspace_dir = self.workspace_dir()?.clone();
let local_config = self.local_config().await?.clone();
let Some(local_workspace) = entry_dir!(&workspace_dir, {
LocalWorkspace::init_current_dir(local_config)
}) else {
return Err(CmdPrepareError::LocalWorkspaceNotFound);
};
self.local_workspace = Some(local_workspace);
}
Ok(self.local_workspace.as_ref().unwrap())
}
// Get the latest information
pub async fn latest_info(&mut self) -> Result<&LatestInfo, CmdPrepareError> {
if self.latest_info.is_none() {
let local_dir = self.workspace_dir()?.clone();
let local_config = self.local_config().await?.clone();
let latest_info = entry_dir!(&local_dir, {
match LatestInfo::read_from(LatestInfo::latest_info_path(
&local_dir,
&local_config.current_account(),
))
.await
{
Ok(info) => info,
Err(_) => {
return Err(CmdPrepareError::LatestInfoNotFound);
}
}
});
self.latest_info = Some(latest_info);
}
Ok(self.latest_info.as_ref().unwrap())
}
// Get the latest file data
pub async fn latest_file_data(
&mut self,
account: &MemberId,
) -> Result<&LatestFileData, CmdPrepareError> {
if !self.latest_file_data.contains_key(account) {
let local_dir = self.workspace_dir()?;
let latest_file_data_path =
match entry_dir!(&local_dir, { LatestFileData::data_path(account) }) {
Ok(p) => p,
Err(_) => return Err(CmdPrepareError::LatestFileDataNotExist(account.clone())),
};
let latest_file_data = LatestFileData::read_from(&latest_file_data_path).await?;
self.latest_file_data
.insert(account.clone(), latest_file_data);
}
Ok(self.latest_file_data.get(account).unwrap())
}
// Get the cached sheet
pub async fn cached_sheet(
&mut self,
sheet_name: &SheetName,
) -> Result<&SheetData, CmdPrepareError> {
if !self.cached_sheet.contains_key(sheet_name) {
let workspace_dir = self.workspace_dir()?;
let cached_sheet = entry_dir!(&workspace_dir, {
match CachedSheet::cached_sheet_data(sheet_name).await {
Ok(data) => data,
Err(_) => return Err(CmdPrepareError::CachedSheetNotFound(sheet_name.clone())),
}
});
self.cached_sheet.insert(sheet_name.clone(), cached_sheet);
}
Ok(self.cached_sheet.get(sheet_name).unwrap())
}
// Get the local sheet data
pub async fn local_sheet_data(
&mut self,
account: &MemberId,
sheet_name: &SheetName,
) -> Result<&LocalSheetData, CmdPrepareError> {
let key = (account.clone(), sheet_name.clone());
if !self.local_sheet_data.contains_key(&key) {
let workspace_dir = self.workspace_dir()?.clone();
let local_workspace = self.local_workspace().await?;
let path = entry_dir!(&workspace_dir, {
local_workspace.local_sheet_path(account, sheet_name)
});
let local_sheet_data = match LocalSheetData::read_from(path).await {
Ok(data) => data,
Err(_) => {
return Err(CmdPrepareError::LocalSheetNotFound(
account.clone(),
sheet_name.clone(),
));
}
};
self.local_sheet_data.insert(key.clone(), local_sheet_data);
}
Ok(self.local_sheet_data.get(&key).unwrap())
}
// Clone and get the local sheet
pub async fn local_sheet_cloned(
&mut self,
account: &MemberId,
sheet_name: &SheetName,
) -> Result<LocalSheet<'_>, CmdPrepareError> {
let local_sheet_data = self.local_sheet_data(account, sheet_name).await?.clone();
Ok(LocalSheet::new(
self.local_workspace().await?,
account.clone(),
sheet_name.clone(),
local_sheet_data,
))
}
// Analyze local status
pub async fn analyze_local_status(&mut self) -> Result<AnalyzeResultPure, CmdPrepareError> {
let Ok(analyzed) = AnalyzeResult::analyze_local_status(self.local_workspace().await?).await
else {
return Err(CmdPrepareError::LocalStatusAnalyzeFailed);
};
Ok(analyzed.into())
}
// Pop the local configuration (take ownership if cached)
pub async fn pop_local_config(&mut self) -> Result<LocalConfig, CmdPrepareError> {
if let Some(local_config) = self.local_config.take() {
Ok(local_config)
} else {
let workspace_dir = self.workspace_dir()?;
let local_config = entry_dir!(workspace_dir, {
LocalConfig::read()
.await
.map_err(|_| CmdPrepareError::LocalConfigNotFound)?
});
Ok(local_config)
}
}
// Pop the local workspace (take ownership if cached)
pub async fn pop_local_workspace(&mut self) -> Result<LocalWorkspace, CmdPrepareError> {
if let Some(local_workspace) = self.local_workspace.take() {
Ok(local_workspace)
} else {
let workspace_dir = self.workspace_dir()?.clone();
let local_config = self.local_config().await?.clone();
let Some(local_workspace) = entry_dir!(&workspace_dir, {
LocalWorkspace::init_current_dir(local_config)
}) else {
return Err(CmdPrepareError::LocalWorkspaceNotFound);
};
Ok(local_workspace)
}
}
// Pop the latest information (take ownership if cached)
pub async fn pop_latest_info(&mut self) -> Result<LatestInfo, CmdPrepareError> {
if let Some(latest_info) = self.latest_info.take() {
Ok(latest_info)
} else {
let local_dir = self.workspace_dir()?.clone();
let local_config = self.local_config().await?.clone();
let latest_info = entry_dir!(&local_dir, {
match LatestInfo::read_from(LatestInfo::latest_info_path(
&local_dir,
&local_config.current_account(),
))
.await
{
Ok(info) => info,
Err(_) => {
return Err(CmdPrepareError::LatestInfoNotFound);
}
}
});
Ok(latest_info)
}
}
// Pop the latest file data for a specific account (take ownership if cached)
pub async fn pop_latest_file_data(
&mut self,
account: &MemberId,
) -> Result<LatestFileData, CmdPrepareError> {
if let Some(latest_file_data) = self.latest_file_data.remove(account) {
Ok(latest_file_data)
} else {
let local_dir = self.workspace_dir()?;
let latest_file_data_path =
match entry_dir!(&local_dir, { LatestFileData::data_path(account) }) {
Ok(p) => p,
Err(_) => return Err(CmdPrepareError::LatestFileDataNotExist(account.clone())),
};
let latest_file_data = LatestFileData::read_from(&latest_file_data_path).await?;
Ok(latest_file_data)
}
}
// Pop the cached sheet for a specific sheet name (take ownership if cached)
pub async fn pop_cached_sheet(
&mut self,
sheet_name: &SheetName,
) -> Result<SheetData, CmdPrepareError> {
if let Some(cached_sheet) = self.cached_sheet.remove(sheet_name) {
Ok(cached_sheet)
} else {
let workspace_dir = self.workspace_dir()?;
let cached_sheet = entry_dir!(&workspace_dir, {
match CachedSheet::cached_sheet_data(sheet_name).await {
Ok(data) => data,
Err(_) => return Err(CmdPrepareError::CachedSheetNotFound(sheet_name.clone())),
}
});
Ok(cached_sheet)
}
}
// Pop the local sheet data for a specific account and sheet name (take ownership if cached)
pub async fn pop_local_sheet_data(
&mut self,
account: &MemberId,
sheet_name: &SheetName,
) -> Result<LocalSheetData, CmdPrepareError> {
let key = (account.clone(), sheet_name.clone());
if let Some(local_sheet_data) = self.local_sheet_data.remove(&key) {
Ok(local_sheet_data)
} else {
let workspace_dir = self.workspace_dir()?.clone();
let local_workspace = self.local_workspace().await?;
let path = entry_dir!(&workspace_dir, {
local_workspace.local_sheet_path(account, sheet_name)
});
let local_sheet_data = match LocalSheetData::read_from(path).await {
Ok(data) => data,
Err(_) => {
return Err(CmdPrepareError::LocalSheetNotFound(
account.clone(),
sheet_name.clone(),
));
}
};
Ok(local_sheet_data)
}
}
}
|