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
use std::fs;
use std::path::Path;
use std::time::SystemTime;
use crate::{RResult, Resolver, ResolverError};
impl Resolver {
fn get_last_modified_time(path: &Path) -> RResult<SystemTime> {
fs::metadata(path)
.map_err(ResolverError::Io)?
.modified()
.map_err(ResolverError::Io)
}
#[tracing::instrument]
pub fn need_update(&self, path: &Path) -> RResult<bool> {
if !path.is_file() {
Ok(false)
} else if let Some(last_read_time) = self.cache.file_snapshot.get(path) {
let last_modified_time = Self::get_last_modified_time(path)?;
if *last_read_time > last_modified_time {
return Ok(false);
}
let now_time = SystemTime::now();
let duration = now_time.duration_since(last_modified_time).map_err(|_| {
ResolverError::UnexpectedValue(format!(
"Compare SystemTime failed in {}",
path.display()
))
})?;
Ok(duration > self.duration)
} else {
Ok(true)
}
}
#[tracing::instrument]
pub fn read_to_string(&self, path: &Path) -> RResult<String> {
let now_time = SystemTime::now();
let content = fs::read_to_string(path).map_err(ResolverError::Io);
self.cache
.file_snapshot
.insert(path.to_path_buf(), now_time);
content
}
}