utils/watcher: Extract Props
Browse files at this commit0a33dca5fd8821ef612d66372eee4bfbf6b36373Parent: b3ae3ad1 changed file +29 -17
- src/utils/watcher.rs +29 -17
src/utils/watcher.rsmodified+29 -17
| @@ -21,13 +21,23 @@ struct WatcherInner { | ||
| 21 | 21 | path: ConfigPath, |
| 22 | 22 | |
| 23 | 23 | /// 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. | |
| 26 | 36 | /// |
| 27 | 37 | /// We store the absolute path in addition to mtime to account for symlinked configs where the |
| 28 | 38 | /// symlink target may change without mtime. This is common on nix where everything is a |
| 29 | 39 | /// symlink to /nix/store, which keeps no mtime (= 1970-01-01). |
| 30 | - last_props: Option<(SystemTime, PathBuf)>, | |
| 40 | + canonical: PathBuf, | |
| 31 | 41 | } |
| 32 | 42 | |
| 33 | 43 | #[derive(Debug, PartialEq, Eq)] |
| @@ -88,30 +98,32 @@ impl Watcher { | ||
| 88 | 98 | } |
| 89 | 99 | } |
| 90 | 100 | |
| 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 | + } | |
| 96 | 107 | |
| 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 | + } | |
| 104 | 116 | } |
| 105 | 117 | } |
| 106 | 118 | |
| 107 | 119 | impl WatcherInner { |
| 108 | 120 | pub fn new(path: ConfigPath) -> Self { |
| 109 | - let last_props = see(&path).ok(); | |
| 121 | + let last_props = Props::from_config_path(&path).ok(); | |
| 110 | 122 | Self { path, last_props } |
| 111 | 123 | } |
| 112 | 124 | |
| 113 | 125 | 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) { | |
| 115 | 127 | if self.last_props.as_ref() != Some(&new_props) { |
| 116 | 128 | self.last_props = Some(new_props); |
| 117 | 129 | CheckResult::Changed |