use std::collections::BTreeMap;
use std::path::PathBuf;
use crate::Exit;
use crate::analysis::findings::Finding;
use crate::analysis::result::FailureReason;
use crate::cli::OutputFormat;
use crate::cli::check::{CheckOutcome, ProviderUse, render};
pub(super) fn outcome() -> CheckOutcome {
CheckOutcome {
tool_findings: Vec::new(),
llm_findings: Vec::new(),
failures: BTreeMap::new(),
provider_uses: Vec::new(),
exit: Exit::Clean,
}
}
pub(super) fn outcome_failing(failures: Vec<(&str, FailureReason)>) -> CheckOutcome {
let failures: BTreeMap<PathBuf, FailureReason> = failures
.into_iter()
.map(|(path, reason)| (PathBuf::from(path), reason))
.collect();
CheckOutcome {
exit: if failures.is_empty() {
Exit::Clean
} else {
Exit::Unanalyzed
},
failures,
..outcome()
}
}
pub(super) fn outcome_with_tool_findings(tool_findings: Vec<Finding>) -> CheckOutcome {
CheckOutcome {
exit: if tool_findings.is_empty() {
Exit::Clean
} else {
Exit::FoundIssues
},
tool_findings,
..outcome()
}
}
pub(super) fn rendered(outcome: &CheckOutcome, format: OutputFormat) -> String {
let mut buf: Vec<u8> = Vec::new();
render::render_to(&mut buf, outcome, format).expect("render");
String::from_utf8(buf).expect("utf8")
}
pub(super) fn rendered_json(outcome: &CheckOutcome) -> serde_json::Value {
serde_json::from_str(&rendered(outcome, OutputFormat::Json)).expect("valid JSON")
}
pub(super) fn provider_use(index: usize, model: &str, location: &str, files: usize) -> ProviderUse {
ProviderUse {
index,
model: model.to_owned(),
location: location.to_owned(),
files,
}
}