forge
Explore
Log in

Don't you dare go Hollow

utils/watcher: Extract Props

Browse files at this commit

0a33dca5fd8821ef612d66372eee4bfbf6b36373Ivan MolodetskikhParent: b3ae3ad
src/utils/watcher.rsmodified+29 -17
@@ -21,13 +21,23 @@ struct WatcherInner {
2121 path: ConfigPath,
2222
2323 /// Last observed props of the watched file.
24- ///
25- /// Equality on this means the file did not change.
24+ last_props: Option<Props>,
25+}
26+
27+/// Properties of the watched file.
28+///
29+/// Equality on this means the file did not change.
30+#[derive(Debug, PartialEq, Eq)]
31+struct Props {
32+ /// Modification time of the watched file.
33+ mtime: SystemTime,
34+
35+ /// Canonical form of the watched path.
2636 ///
2737 /// We store the absolute path in addition to mtime to account for symlinked configs where the
2838 /// symlink target may change without mtime. This is common on nix where everything is a
2939 /// symlink to /nix/store, which keeps no mtime (= 1970-01-01).
30- last_props: Option<(SystemTime, PathBuf)>,
40+ canonical: PathBuf,
3141 }
3242
3343 #[derive(Debug, PartialEq, Eq)]
@@ -88,30 +98,32 @@ impl Watcher {
8898 }
8999 }
90100
91-fn see_path(path: &Path) -> io::Result<(SystemTime, PathBuf)> {
92- let canon = path.canonicalize()?;
93- let mtime = canon.metadata()?.modified()?;
94- Ok((mtime, canon))
95-}
101+impl Props {
102+ fn from_path(path: &Path) -> io::Result<Self> {
103+ let canonical = path.canonicalize()?;
104+ let mtime = canonical.metadata()?.modified()?;
105+ Ok(Self { mtime, canonical })
106+ }
96107
97-fn see(config_path: &ConfigPath) -> io::Result<(SystemTime, PathBuf)> {
98- match config_path {
99- ConfigPath::Explicit(path) => see_path(path),
100- ConfigPath::Regular {
101- user_path,
102- system_path,
103- } => see_path(user_path).or_else(|_| see_path(system_path)),
108+ fn from_config_path(config_path: &ConfigPath) -> io::Result<Self> {
109+ match config_path {
110+ ConfigPath::Explicit(path) => Self::from_path(path),
111+ ConfigPath::Regular {
112+ user_path,
113+ system_path,
114+ } => Self::from_path(user_path).or_else(|_| Self::from_path(system_path)),
115+ }
104116 }
105117 }
106118
107119 impl WatcherInner {
108120 pub fn new(path: ConfigPath) -> Self {
109- let last_props = see(&path).ok();
121+ let last_props = Props::from_config_path(&path).ok();
110122 Self { path, last_props }
111123 }
112124
113125 pub fn check(&mut self) -> CheckResult {
114- if let Ok(new_props) = see(&self.path) {
126+ if let Ok(new_props) = Props::from_config_path(&self.path) {
115127 if self.last_props.as_ref() != Some(&new_props) {
116128 self.last_props = Some(new_props);
117129 CheckResult::Changed