use crate::snippets::audit::{AuditConfig, AuditReport, SnippetAccounting, audit};
use std::path::{Path, PathBuf};
pub(crate) fn configured_curated_paths(config_path: &Path) -> anyhow::Result<Vec<PathBuf>> {
let (_, resolved) = crate::bin_cli::helpers::load_config(config_path)?;
let root = config_path.parent().unwrap_or_else(|| Path::new("."));
let patterns: Vec<String> = resolved
.iter()
.filter_map(|krate| krate.e2e.as_ref()?.snippets.as_ref())
.flat_map(|snippets| snippets.curated_snippets.iter().cloned())
.collect();
Ok(
crate::e2e::snippets::coverage::resolve_curated_snippet_paths(root, &patterns)?
.into_iter()
.map(|relative| root.join(relative))
.collect(),
)
}
pub(crate) fn accounting_scope_line(config_path: Option<&Path>, enabled: bool, curated: usize) -> String {
match (config_path, enabled) {
(None, _) => "Curated accounting was NOT run - pass --config to tell a declared \
hand-authored snippet apart from an unaccounted coverage gap."
.to_string(),
(Some(_), false) => "Curated accounting was NOT run - no coverage ledger under the snippet roots records \
anything as alef-generated, so every file would read as unaccounted."
.to_string(),
(Some(_), true) => format!("Curated accounting: {curated} snippet(s) declared curated."),
}
}
#[derive(Debug)]
pub(crate) struct AuditOutcome {
pub report: AuditReport,
pub accounting_enabled: bool,
}
pub(crate) fn audit_outcome(
snippet_dirs: &[PathBuf],
docs_dirs: &[PathBuf],
require_frontmatter: bool,
config_path: Option<&Path>,
) -> anyhow::Result<AuditOutcome> {
let generated_paths = crate::snippets::gaps::coverage_ledger_references(snippet_dirs)?;
let curated_paths = config_path
.map(configured_curated_paths)
.transpose()?
.unwrap_or_default();
let accounting_enabled = config_path.is_some() && !generated_paths.is_empty();
let config = AuditConfig {
docs_dirs: docs_dirs.to_vec(),
snippet_dirs: snippet_dirs.to_vec(),
require_frontmatter,
include_base_paths: docs_dirs.to_vec(),
configured_references: generated_paths.clone(),
exclude: Vec::new(),
accounting: SnippetAccounting {
generated_paths,
curated_paths,
enabled: accounting_enabled,
},
};
Ok(AuditOutcome {
report: audit(&config),
accounting_enabled,
})
}
#[cfg(test)]
mod tests;