use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use super::facts::{EntryKind, ProjectFacts};
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum FactQuestion {
IsFile(Vec<u8>),
IsDir(Vec<u8>),
Link(Vec<u8>),
Canonical(Vec<u8>),
ListDir(Vec<u8>),
Config(Vec<u8>),
ConfigField(Vec<u8>, String),
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum FactAnswer {
Bool(bool),
Path(Option<Vec<u8>>),
Digest([u8; 16]),
}
pub type FactAnswers = BTreeMap<FactQuestion, FactAnswer>;
impl FactQuestion {
pub fn answer(&self, facts: &dyn ProjectFacts) -> FactAnswer {
match self {
Self::IsFile(path) => FactAnswer::Bool(facts.is_file(path)),
Self::IsDir(path) => FactAnswer::Bool(facts.is_dir(path)),
Self::Link(path) => FactAnswer::Path(facts.symlink_target(path).map(<[u8]>::to_vec)),
Self::Canonical(path) => FactAnswer::Path(facts.canonical(path)),
Self::ListDir(path) => listing_answer(&facts.list_dir(path)),
Self::Config(path) => config_answer(facts.config_bytes(path).as_deref()),
Self::ConfigField(path, name) => {
config_field_answer(path, facts.config_bytes(path).as_deref(), name)
}
}
}
}
pub fn listing_answer(entries: &[super::facts::DirEntry]) -> FactAnswer {
let mut hasher = blake3::Hasher::new();
hasher.update(b"aft listing 1");
for entry in entries {
hasher.update(&(entry.name.len() as u64).to_le_bytes());
hasher.update(&entry.name);
hasher.update(&[match entry.kind {
EntryKind::Regular => 0,
EntryKind::Directory => 1,
EntryKind::Symlink => 2,
}]);
}
FactAnswer::Digest(truncate(&hasher.finalize()))
}
pub fn config_answer(bytes: Option<&[u8]>) -> FactAnswer {
let mut hasher = blake3::Hasher::new();
match bytes {
Some(bytes) => {
hasher.update(b"aft config 1 present");
hasher.update(bytes);
}
None => {
hasher.update(b"aft config 1 absent");
}
}
FactAnswer::Digest(truncate(&hasher.finalize()))
}
pub fn config_field_answer(path: &[u8], bytes: Option<&[u8]>, name: &str) -> FactAnswer {
let projected = bytes
.and_then(|bytes| super::resolution_facts::project(path, bytes))
.and_then(|fields| fields.get(name).cloned());
match projected {
Some(digest) => {
let hash = blake3::hash(digest.as_bytes());
FactAnswer::Digest(truncate(&hash))
}
None if bytes.is_none() => config_answer(None),
None => config_answer(bytes),
}
}
fn truncate(hash: &blake3::Hash) -> [u8; 16] {
let mut digest = [0; 16];
digest.copy_from_slice(&hash.as_bytes()[..16]);
digest
}
impl aft_store::heap::HeapBytes for FactQuestion {
fn heap_bytes(&self, seen: &mut aft_store::heap::Seen) -> usize {
match self {
Self::IsFile(path)
| Self::IsDir(path)
| Self::Link(path)
| Self::Canonical(path)
| Self::ListDir(path)
| Self::Config(path) => path.heap_bytes(seen),
Self::ConfigField(path, name) => path.heap_bytes(seen) + name.heap_bytes(seen),
}
}
}
impl aft_store::heap::HeapBytes for FactAnswer {
fn heap_bytes(&self, seen: &mut aft_store::heap::Seen) -> usize {
match self {
Self::Bool(_) | Self::Digest(_) => 0,
Self::Path(path) => path.heap_bytes(seen),
}
}
}