buildl_core/ports/stat_cache.rs
1//! The port through which Plan keeps the digests of files it has already hashed.
2//!
3//! Its own file because the record outlives a run, so it is stored somewhere, and storing is
4//! not this crate's to do. What is recorded, and when a record may be trusted, stay with the
5//! caller.
6//!
7//! Responsibilities: [`StatCache`].
8//!
9//! Non-responsibilities: implementing it, and the rule for reusing an entry. The first belongs
10//! to an adapter crate; the second is the caller's.
11//!
12//! This file holds only a trait definition, so it carries no logic to unit-test.
13
14use crate::error::Result;
15use crate::types::{SourcePath, StatEntry};
16
17/// Holds, for each file hashed before, its digest and the metadata it was hashed under.
18pub trait StatCache {
19 /// What was recorded for `path`, if anything.
20 fn entry(&self, path: &SourcePath) -> Option<StatEntry>;
21
22 /// Replaces the entry for `path`, in memory.
23 ///
24 /// An implementation may decline to keep an entry it cannot vouch for;
25 /// [`entry`](StatCache::entry) then returns `None` for that path.
26 fn record(&mut self, path: SourcePath, entry: StatEntry);
27
28 /// Persists every entry.
29 ///
30 /// # Errors
31 ///
32 /// Returns [`Error::Adapter`](crate::Error::Adapter), naming
33 /// [`PortName::StatCache`](crate::PortName::StatCache), when the entries cannot be stored.
34 fn commit(&mut self) -> Result<()>;
35}