Capture editor file dates and make desktop ZIP import observable

This commit is contained in:
gloamxun committed 2026-12-02 10:07:43 +08:00
1 parent 0ea71d57a9
commit fbc339f5c4
11 files changed
+231 -72

No files matched your search

+14 -1
View File
@@ -255,7 +255,7 @@ pub struct FileModificationTime {
}
impl FileModificationTime {
fn from_system_time(time: SystemTime) -> Result<Self, String> {
pub fn from_system_time(time: SystemTime) -> Result<Self, String> {
let nanos = match time.duration_since(SystemTime::UNIX_EPOCH) {
Ok(duration) => duration.as_nanos() as i128,
Err(error) => -(error.duration().as_nanos() as i128),
@@ -268,6 +268,19 @@ impl FileModificationTime {
})
}
pub fn to_system_time(self) -> Option<SystemTime> {
if !self.valid() {
return None;
}
let seconds = std::time::Duration::from_secs(self.unix_seconds.unsigned_abs());
let whole = if self.unix_seconds < 0 {
SystemTime::UNIX_EPOCH.checked_sub(seconds)?
} else {
SystemTime::UNIX_EPOCH.checked_add(seconds)?
};
whole.checked_add(std::time::Duration::from_nanos(self.nanoseconds.into()))
}
fn valid(self) -> bool {
self.nanoseconds < 1_000_000_000
}
@@ -158,6 +158,15 @@ impl ProjectSnapshot {
pub fn with_external_resources(
self: &Arc<Self>,
resources: &BTreeMap<PathBuf, Arc<[u8]>>,
) -> Result<Arc<Self>, String> {
self.with_external_resources_and_dates(resources, &BTreeMap::new())
}
/// Attach dates captured with newly observed external resource bytes.
pub fn with_external_resources_and_dates(
self: &Arc<Self>,
resources: &BTreeMap<PathBuf, Arc<[u8]>>,
dates: &BTreeMap<PathBuf, FileModificationTime>,
) -> Result<Arc<Self>, String> {
if resources.keys().all(|path| self.files.contains_key(path)) {
return Ok(self.clone());
@@ -175,6 +184,9 @@ impl ProjectSnapshot {
}
next.files
.insert(path.clone(), Self::capture(path.clone(), data.clone()));
if let Some(&date) = dates.get(path) {
next.file_modifications.insert(path.clone(), date);
}
next.directories
.extend(path.ancestors().skip(1).map(Path::to_path_buf));
}