summaryrefslogtreecommitdiff
path: root/crates/vcs_data/src/data/local/local_sheet.rs
blob: f1a2159710f265fa175991e23a3d08a0108ad0f9 (plain)
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
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,
}

#[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 = "mapping")]
    pub(crate) mapping: HashMap<LocalFilePathBuf, LocalMappingMetadata>,

    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 = "hash")]
    pub(crate) hash_when_updated: String,

    /// Time when the file was downloaded to the local workspace
    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 = "version_desc")]
    pub(crate) version_desc_when_updated: VirtualFileVersionDescription,

    /// Version when the file was downloaded to the local workspace
    #[serde(rename = "version")]
    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
    pub(crate) last_modifiy_check_time: SystemTime,

    /// Latest modifiy check result
    pub(crate) last_modifiy_check_result: bool,
}

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_modifiy_check_time,
            last_modifiy_check_result,
        }
    }

    /// 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_modifiy_check_time
    }

    /// Setter for last_modifiy_check_time
    pub fn set_last_modifiy_check_time(&mut self, time: SystemTime) {
        self.last_modifiy_check_time = time;
    }

    /// Getter for last_modifiy_check_result
    pub fn last_modifiy_check_result(&self) -> bool {
        self.last_modifiy_check_result
    }

    /// Setter for last_modifiy_check_result
    pub fn set_last_modifiy_check_result(&mut self, result: bool) {
        self.last_modifiy_check_result = result;
    }
}

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_modifiy_check_time: SystemTime::now(),
            last_modifiy_check_result: false,
        }
    }
}

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<'a> LocalSheet<'a> {
    /// 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.data.mapping.contains_key(&path)
            || self.data.vfs.contains_key(&mapping.mapping_vfid)
        {
            return Err(Error::new(
                std::io::ErrorKind::AlreadyExists,
                "Mapping already exists",
            ));
        }

        self.data.mapping.insert(path, mapping);
        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.data.mapping.contains_key(&to) {
            return Err(Error::new(
                std::io::ErrorKind::AlreadyExists,
                "To path already exists.",
            ));
        }

        let Some(old_value) = self.data.mapping.remove(&from) else {
            return Err(Error::new(
                std::io::ErrorKind::NotFound,
                "From path is not found.",
            ));
        };

        self.data.mapping.insert(to, old_value);

        Ok(())
    }

    /// 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.data.mapping.get(&path) else {
            return Err(Error::new(
                std::io::ErrorKind::NotFound,
                "Path is not found.",
            ));
        };
        Ok(data)
    }

    /// Get muttable 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.data.mapping.get_mut(&path) else {
            return Err(Error::new(
                std::io::ErrorKind::NotFound,
                "Path is not found.",
            ));
        };
        Ok(data)
    }

    /// 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();

        self.data.vfs = HashMap::new();
        for (path, mapping) in self.data.mapping.iter() {
            self.data
                .vfs
                .insert(mapping.mapping_vfid.clone(), path.clone());
        }

        LocalSheetData::write_to(&self.data, path).await?;
        Ok(())
    }

    /// Get path by VirtualFileId
    pub fn path_by_id(&self, vfid: &VirtualFileId) -> Option<&PathBuf> {
        self.data.vfs.get(vfid)
    }
}