use std::collections::BTreeMap;
use crate::types::op::OpPath;
use crate::types::tree::ContentHash;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ReadOutcome {
Absent,
Bytes(ContentHash),
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Reads {
outcomes: BTreeMap<OpPath, ReadOutcome>,
}
impl Reads {
pub fn record(&mut self, path: OpPath, outcome: ReadOutcome) {
self.outcomes.entry(path).or_insert(outcome);
}
#[must_use]
pub fn shared(&self, path: &OpPath, declared: &[OpPath]) -> Option<ReadOutcome> {
if declared.contains(path) {
self.outcomes.get(path).cloned()
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
type TestResult = Result<(), Box<dyn std::error::Error>>;
fn path(text: &str) -> Result<OpPath, Box<dyn std::error::Error>> {
Ok(OpPath::new(text)?)
}
#[test]
fn shared_needs_declared_and_read() -> TestResult {
let settings = path(".claude/settings.json")?;
let other = path(".claude/other.json")?;
let mut reads = Reads::default();
let hash = ContentHash::of_bytes(b"{}");
reads.record(settings.clone(), ReadOutcome::Bytes(hash.clone()));
reads.record(other.clone(), ReadOutcome::Absent);
let declared = [settings.clone(), path(".claude/unread.json")?];
assert_eq!(
reads.shared(&settings, &declared),
Some(ReadOutcome::Bytes(hash))
);
assert_eq!(reads.shared(&path(".claude/unread.json")?, &declared), None);
assert_eq!(reads.shared(&other, &declared), None);
Ok(())
}
#[test]
fn reads_keeps_the_first_outcome_of_a_path() -> TestResult {
let settings = path(".claude/settings.json")?;
let mut reads = Reads::default();
reads.record(settings.clone(), ReadOutcome::Absent);
reads.record(
settings.clone(),
ReadOutcome::Bytes(ContentHash::of_bytes(b"x")),
);
assert_eq!(
reads.shared(&settings, std::slice::from_ref(&settings)),
Some(ReadOutcome::Absent)
);
Ok(())
}
#[test]
fn reads_key_is_the_normalised_path() -> TestResult {
let mut reads = Reads::default();
reads.record(path("./.claude//settings.json")?, ReadOutcome::Absent);
let plain = path(".claude/settings.json")?;
assert_eq!(
reads.shared(&plain, std::slice::from_ref(&plain)),
Some(ReadOutcome::Absent)
);
Ok(())
}
}