use std::path::{Path, PathBuf};
use super::diagnostic::Diagnostic;
use super::{Boundary, DefinitionSet, SharedDefinitions};
#[derive(Clone, Copy, Default)]
pub struct GateOpts {
pub require_ids: bool,
pub want_raw: bool,
}
pub struct GateReport {
pub set: DefinitionSet,
pub raw: Option<DefinitionSet>,
pub shared: SharedDefinitions,
pub compiled: std::collections::BTreeMap<&'static str, usize>,
pub findings: Vec<Diagnostic>,
pub unparseable: Vec<(PathBuf, String)>,
pub skipped: Vec<PathBuf>,
}
impl GateReport {
pub fn errors(&self) -> usize {
self.findings.iter().filter(|f| f.is_error()).count()
}
pub fn warnings(&self) -> usize {
self.findings.iter().filter(|f| f.is_warning()).count()
}
pub fn notices(&self) -> Vec<String> {
let mut out = Vec::with_capacity(self.unparseable.len() + self.skipped.len());
for (path, error) in &self.unparseable {
out.push(format!(
"warning: {} is not readable JSON: {error}",
path.display()
));
}
for path in &self.skipped {
out.push(format!(
"note: {} is not a channel, workflow or connector — skipped",
path.display()
));
}
out
}
}
pub fn gate_directory(
dir: &Path,
boundary: &Boundary,
opts: GateOpts,
) -> Result<GateReport, String> {
let raw = if opts.want_raw {
Some(DefinitionSet::from_directory_raw(dir)?.0)
} else {
None
};
let (set, report) = DefinitionSet::from_directory(dir)?;
let mut findings = report.findings;
findings.extend(super::check(&set, boundary, opts.require_ids));
Ok(GateReport {
set,
raw,
shared: report.shared,
compiled: report.compiled,
findings,
unparseable: report.unparseable,
skipped: report.skipped,
})
}