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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
use std::{collections::HashMap, io::Error, path::PathBuf, time::SystemTime};
use ::serde::{Deserialize, Serialize};
use cfg_file::{ConfigFile, config::ConfigFile};
use string_proc::format_path::format_path;
use crate::{
constants::CLIENT_FILE_LOCAL_SHEET_NOSET,
data::{
local::LocalWorkspace,
member::MemberId,
sheet::SheetName,
vault::virtual_file::{VirtualFileId, VirtualFileVersion, VirtualFileVersionDescription},
},
};
pub type LocalFilePathBuf = PathBuf;
pub type LocalSheetPathBuf = PathBuf;
/// # Local Sheet
/// Local sheet information, used to record metadata of actual local files,
/// to compare with upstream information for more optimized file submission,
/// and to determine whether files need to be updated or submitted.
pub struct LocalSheet<'a> {
pub(crate) local_workspace: &'a LocalWorkspace,
pub(crate) member: MemberId,
pub(crate) sheet_name: String,
pub(crate) data: LocalSheetData,
}
impl<'a> LocalSheet<'a> {
/// Create a new LocalSheet instance
pub fn new(
local_workspace: &'a LocalWorkspace,
member: MemberId,
sheet_name: String,
data: LocalSheetData,
) -> Self {
Self {
local_workspace,
member,
sheet_name,
data,
}
}
}
#[derive(Debug, Default, Serialize, Deserialize, ConfigFile, Clone)]
#[cfg_file(path = CLIENT_FILE_LOCAL_SHEET_NOSET)] // Do not use LocalSheet::write or LocalSheet::read
pub struct LocalSheetData {
/// Local file path to metadata mapping.
#[serde(rename = "map")]
pub(crate) mapping: HashMap<LocalFilePathBuf, LocalMappingMetadata>,
#[serde(rename = "vfs")]
pub(crate) vfs: HashMap<VirtualFileId, LocalFilePathBuf>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LocalMappingMetadata {
/// Hash value generated immediately after the file is downloaded to the local workspace
#[serde(rename = "base_hash")]
pub(crate) hash_when_updated: String,
/// Time when the file was downloaded to the local workspace
#[serde(rename = "time")]
pub(crate) time_when_updated: SystemTime,
/// Size of the file when downloaded to the local workspace
#[serde(rename = "size")]
pub(crate) size_when_updated: u64,
/// Version description when the file was downloaded to the local workspace
#[serde(rename = "desc")]
pub(crate) version_desc_when_updated: VirtualFileVersionDescription,
/// Version when the file was downloaded to the local workspace
#[serde(rename = "ver")]
pub(crate) version_when_updated: VirtualFileVersion,
/// Virtual file ID corresponding to the local path
#[serde(rename = "id")]
pub(crate) mapping_vfid: VirtualFileId,
/// Latest modifiy check time
#[serde(rename = "check_time")]
pub(crate) last_modify_check_time: SystemTime,
/// Latest modifiy check result
#[serde(rename = "modified")]
pub(crate) last_modify_check_result: bool,
/// Latest modifiy check hash result
#[serde(rename = "current_hash")]
pub(crate) last_modify_check_hash: Option<String>,
}
impl LocalSheetData {
/// Wrap LocalSheetData into LocalSheet with workspace, member, and sheet name
pub fn wrap_to_local_sheet<'a>(
self,
workspace: &'a LocalWorkspace,
member: MemberId,
sheet_name: SheetName,
) -> LocalSheet<'a> {
LocalSheet {
local_workspace: workspace,
member,
sheet_name,
data: self,
}
}
}
impl LocalMappingMetadata {
/// Create a new MappingMetaData instance
#[allow(clippy::too_many_arguments)]
pub fn new(
hash_when_updated: String,
time_when_updated: SystemTime,
size_when_updated: u64,
version_desc_when_updated: VirtualFileVersionDescription,
version_when_updated: VirtualFileVersion,
mapping_vfid: VirtualFileId,
last_modifiy_check_time: SystemTime,
last_modifiy_check_result: bool,
) -> Self {
Self {
hash_when_updated,
time_when_updated,
size_when_updated,
version_desc_when_updated,
version_when_updated,
mapping_vfid,
last_modify_check_time: last_modifiy_check_time,
last_modify_check_result: last_modifiy_check_result,
last_modify_check_hash: None,
}
}
/// Getter for hash_when_updated
pub fn hash_when_updated(&self) -> &String {
&self.hash_when_updated
}
/// Setter for hash_when_updated
pub fn set_hash_when_updated(&mut self, hash: String) {
self.hash_when_updated = hash;
}
/// Getter for date_when_updated
pub fn time_when_updated(&self) -> &SystemTime {
&self.time_when_updated
}
/// Setter for time_when_updated
pub fn set_time_when_updated(&mut self, time: SystemTime) {
self.time_when_updated = time;
}
/// Getter for size_when_updated
pub fn size_when_updated(&self) -> u64 {
self.size_when_updated
}
/// Setter for size_when_updated
pub fn set_size_when_updated(&mut self, size: u64) {
self.size_when_updated = size;
}
/// Getter for version_desc_when_updated
pub fn version_desc_when_updated(&self) -> &VirtualFileVersionDescription {
&self.version_desc_when_updated
}
/// Setter for version_desc_when_updated
pub fn set_version_desc_when_updated(&mut self, version_desc: VirtualFileVersionDescription) {
self.version_desc_when_updated = version_desc;
}
/// Getter for version_when_updated
pub fn version_when_updated(&self) -> &VirtualFileVersion {
&self.version_when_updated
}
/// Setter for version_when_updated
pub fn set_version_when_updated(&mut self, version: VirtualFileVersion) {
self.version_when_updated = version;
}
/// Getter for mapping_vfid
pub fn mapping_vfid(&self) -> &VirtualFileId {
&self.mapping_vfid
}
/// Setter for mapping_vfid
pub fn set_mapping_vfid(&mut self, vfid: VirtualFileId) {
self.mapping_vfid = vfid;
}
/// Getter for last_modifiy_check_time
pub fn last_modifiy_check_time(&self) -> &SystemTime {
&self.last_modify_check_time
}
/// Setter for last_modifiy_check_time
pub fn set_last_modifiy_check_time(&mut self, time: SystemTime) {
self.last_modify_check_time = time;
}
/// Getter for last_modifiy_check_result
pub fn last_modifiy_check_result(&self) -> bool {
self.last_modify_check_result
}
/// Setter for last_modifiy_check_result
pub fn set_last_modifiy_check_result(&mut self, result: bool) {
self.last_modify_check_result = result;
}
/// Getter for last_modifiy_check_hash
pub fn last_modifiy_check_hash(&self) -> &Option<String> {
&self.last_modify_check_hash
}
/// Setter for last_modifiy_check_hash
pub fn set_last_modifiy_check_hash(&mut self, hash: Option<String>) {
self.last_modify_check_hash = hash;
}
}
impl Default for LocalMappingMetadata {
fn default() -> Self {
Self {
hash_when_updated: Default::default(),
time_when_updated: SystemTime::now(),
size_when_updated: Default::default(),
version_desc_when_updated: Default::default(),
version_when_updated: Default::default(),
mapping_vfid: Default::default(),
last_modify_check_time: SystemTime::now(),
last_modify_check_result: false,
last_modify_check_hash: None,
}
}
}
mod instant_serde {
use serde::{self, Deserialize, Deserializer, Serializer};
use tokio::time::Instant;
pub fn serialize<S>(instant: &Instant, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u64(instant.elapsed().as_secs())
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Instant, D::Error>
where
D: Deserializer<'de>,
{
let secs = u64::deserialize(deserializer)?;
Ok(Instant::now() - std::time::Duration::from_secs(secs))
}
}
impl<'a> From<&'a LocalSheet<'a>> for &'a LocalSheetData {
fn from(sheet: &'a LocalSheet<'a>) -> Self {
&sheet.data
}
}
impl LocalSheetData {
/// Add mapping to local sheet data
pub fn add_mapping(
&mut self,
path: &LocalFilePathBuf,
mapping: LocalMappingMetadata,
) -> Result<(), std::io::Error> {
let path = format_path(path)?;
if self.mapping.contains_key(&path) || self.vfs.contains_key(&mapping.mapping_vfid) {
return Err(Error::new(
std::io::ErrorKind::AlreadyExists,
"Mapping already exists",
));
}
self.mapping.insert(path.clone(), mapping.clone());
self.vfs.insert(mapping.mapping_vfid.clone(), path);
Ok(())
}
/// Move mapping to other path
pub fn move_mapping(
&mut self,
from: &LocalFilePathBuf,
to: &LocalFilePathBuf,
) -> Result<(), std::io::Error> {
let from = format_path(from)?;
let to = format_path(to)?;
if self.mapping.contains_key(&to) {
return Err(Error::new(
std::io::ErrorKind::AlreadyExists,
"To path already exists.",
));
}
let Some(old_value) = self.mapping.remove(&from) else {
return Err(Error::new(
std::io::ErrorKind::NotFound,
"From path is not found.",
));
};
// Update vfs mapping
self.vfs.insert(old_value.mapping_vfid.clone(), to.clone());
self.mapping.insert(to, old_value);
Ok(())
}
/// Remove mapping from local sheet
pub fn remove_mapping(
&mut self,
path: &LocalFilePathBuf,
) -> Result<LocalMappingMetadata, std::io::Error> {
let path = format_path(path)?;
match self.mapping.remove(&path) {
Some(mapping) => {
self.vfs.remove(&mapping.mapping_vfid);
Ok(mapping)
}
None => Err(Error::new(
std::io::ErrorKind::NotFound,
"Path is not found.",
)),
}
}
/// Get immutable mapping data
pub fn mapping_data(
&self,
path: &LocalFilePathBuf,
) -> Result<&LocalMappingMetadata, std::io::Error> {
let path = format_path(path)?;
let Some(data) = self.mapping.get(&path) else {
return Err(Error::new(
std::io::ErrorKind::NotFound,
"Path is not found.",
));
};
Ok(data)
}
/// Get mutable mapping data
pub fn mapping_data_mut(
&mut self,
path: &LocalFilePathBuf,
) -> Result<&mut LocalMappingMetadata, std::io::Error> {
let path = format_path(path)?;
let Some(data) = self.mapping.get_mut(&path) else {
return Err(Error::new(
std::io::ErrorKind::NotFound,
"Path is not found.",
));
};
Ok(data)
}
/// Get path by VirtualFileId
pub fn path_by_id(&self, vfid: &VirtualFileId) -> Option<&PathBuf> {
self.vfs.get(vfid)
}
}
impl<'a> LocalSheet<'a> {
/// Add mapping to local sheet data
pub fn add_mapping(
&mut self,
path: &LocalFilePathBuf,
mapping: LocalMappingMetadata,
) -> Result<(), std::io::Error> {
self.data.add_mapping(path, mapping)
}
/// Move mapping to other path
pub fn move_mapping(
&mut self,
from: &LocalFilePathBuf,
to: &LocalFilePathBuf,
) -> Result<(), std::io::Error> {
self.data.move_mapping(from, to)
}
/// Remove mapping from local sheet
pub fn remove_mapping(
&mut self,
path: &LocalFilePathBuf,
) -> Result<LocalMappingMetadata, std::io::Error> {
self.data.remove_mapping(path)
}
/// Get immutable mapping data
pub fn mapping_data(
&self,
path: &LocalFilePathBuf,
) -> Result<&LocalMappingMetadata, std::io::Error> {
self.data.mapping_data(path)
}
/// Get mutable mapping data
pub fn mapping_data_mut(
&mut self,
path: &LocalFilePathBuf,
) -> Result<&mut LocalMappingMetadata, std::io::Error> {
self.data.mapping_data_mut(path)
}
/// Write the sheet to disk
pub async fn write(&mut self) -> Result<(), std::io::Error> {
let path = self
.local_workspace
.local_sheet_path(&self.member, &self.sheet_name);
self.write_to_path(path).await
}
/// Write the sheet to custom path
pub async fn write_to_path(&mut self, path: impl Into<PathBuf>) -> Result<(), std::io::Error> {
let path = path.into();
LocalSheetData::write_to(&self.data, path).await?;
Ok(())
}
/// Get path by VirtualFileId
pub fn path_by_id(&self, vfid: &VirtualFileId) -> Option<&PathBuf> {
self.data.path_by_id(vfid)
}
}
|