use crate::{fed, tokens};
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Depth {
Rule,
Why,
All,
}
#[derive(Debug)]
pub struct Pack {
pub chain: Vec<PathBuf>,
pub text: String,
pub children: Vec<fed::Edge>,
pub cost: tokens::Count,
}
pub fn pack(root: &Path, dir: &Path, depth: Depth) -> std::io::Result<Pack> {
let chain = fed::chain(root, dir);
let mut text = String::new();
for spec in &chain {
let raw = std::fs::read_to_string(spec)?;
text.push_str(&match depth {
Depth::All => raw,
Depth::Rule | Depth::Why => crate::spec::rule_depth(&raw),
});
text.push('\n');
}
if depth == Depth::Why {
let why = dir.join("SPEC.why.md");
if why.is_file() {
text.push_str(&std::fs::read_to_string(why)?);
}
}
let own = chain.last().map_or(String::new(), |p| {
std::fs::read_to_string(p).unwrap_or_default()
});
Ok(Pack {
chain,
children: fed::edges(&own),
cost: tokens::count(&text),
text,
})
}
pub fn ceiling_for(root: &Path, node: &Path) -> Result<u64, String> {
let rel = node.strip_prefix(root).unwrap_or(node);
let key = if rel.as_os_str().is_empty() {
"SPEC.md".into()
} else {
rel.to_string_lossy().to_string()
};
Ok(tokens::Ceilings::load(root)?.for_path(&key))
}
#[derive(Debug, PartialEq, Eq)]
pub enum Verdict {
Fits { slack: u64 },
Over { by: u64 },
}
#[must_use]
pub fn verdict(cost: u64, budget: u64) -> Verdict {
if cost <= budget {
Verdict::Fits {
slack: budget - cost,
}
} else {
Verdict::Over { by: cost - budget }
}
}
#[cfg(test)]
#[path = "tests/lens.rs"]
mod tests;