Skip to main content

aft/inspect/
freshness.rs

1use std::path::Path;
2
3use crate::cache_freshness::{self, FileFreshness, FreshnessVerdict};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum ContributionFreshness {
7    Fresh {
8        metadata_changed: bool,
9        freshness: FileFreshness,
10    },
11    Stale,
12    Deleted,
13}
14
15impl ContributionFreshness {
16    pub fn is_fresh(self) -> bool {
17        matches!(self, ContributionFreshness::Fresh { .. })
18    }
19
20    pub fn freshness(self) -> Option<FileFreshness> {
21        match self {
22            ContributionFreshness::Fresh { freshness, .. } => Some(freshness),
23            ContributionFreshness::Stale | ContributionFreshness::Deleted => None,
24        }
25    }
26}
27
28pub fn verify_contribution_file(path: &Path, cached: &FileFreshness) -> ContributionFreshness {
29    match cache_freshness::verify_file(path, cached) {
30        FreshnessVerdict::HotFresh => ContributionFreshness::Fresh {
31            metadata_changed: false,
32            freshness: *cached,
33        },
34        FreshnessVerdict::ContentFresh {
35            new_mtime,
36            new_size,
37        } => ContributionFreshness::Fresh {
38            metadata_changed: true,
39            freshness: FileFreshness {
40                mtime: new_mtime,
41                size: new_size,
42                content_hash: cached.content_hash,
43            },
44        },
45        FreshnessVerdict::Stale => ContributionFreshness::Stale,
46        FreshnessVerdict::Deleted => ContributionFreshness::Deleted,
47    }
48}
49
50pub fn contribution_is_fresh(path: &Path, cached: &FileFreshness) -> bool {
51    verify_contribution_file(path, cached).is_fresh()
52}