1use std::path::{Path, PathBuf};
2use std::time::Duration;
3use std::{io, time};
4
5pub fn is_stale<P: AsRef<Path>>(path: P, timeout: Duration) -> Result<bool, IsStaleError> {
7 let metadata = std::fs::metadata(path).map_err(IsStaleError::ReadMetadata)?;
8 let modification = metadata
9 .modified()
10 .map_err(IsStaleError::ReadModificationDate)?;
11 let duration = modification
12 .elapsed()
13 .map_err(IsStaleError::ElapsedSinceModified)?;
14
15 Ok(timeout < duration)
16}
17
18#[derive(Debug, thiserror::Error)]
20#[non_exhaustive]
21pub enum IsStaleError {
22 #[error("Failed to check if file is stale: unable to read metadata ({0})")]
25 ReadMetadata(io::Error),
26
27 #[error("Failed to check if file is stale: unable to read modification date ({0})")]
30 ReadModificationDate(io::Error),
31
32 #[error("Failed to check if file is stale: modification date is more recent than the current system time ({0})")]
36 ElapsedSinceModified(time::SystemTimeError),
37}
38
39pub fn base_cache_dir() -> Result<PathBuf, BaseCacheDirError> {
41 let cache = directories_next::ProjectDirs::from("com", "ilumeo", "rust-releases")
42 .ok_or(BaseCacheDirError)?;
43 let cache = cache.cache_dir();
44
45 Ok(cache.to_path_buf())
46}
47
48#[derive(Debug, thiserror::Error)]
50#[error("Unable to locate base cache folder")]
51pub struct BaseCacheDirError;