use std::path::Path;
use anyhow::Result;
use comply::report::{Report, Status};
use comply::spdx;
use crate::cli::shared::print_json_report;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub(crate) enum Format {
#[default]
Plain,
Json,
Lines,
Quiet,
}
#[derive(Debug, Clone, Copy, Default)]
#[expect(
clippy::struct_excessive_bools,
reason = "mirrors the four independent output flags on the clap variants"
)]
pub(crate) struct Flags {
pub(crate) quiet: bool,
pub(crate) json: bool,
pub(crate) plain: bool,
pub(crate) lines: bool,
}
impl Format {
#[allow(clippy::missing_const_for_fn)]
pub(crate) fn resolve(flags: Flags, default: Self) -> Self {
if flags.quiet {
Self::Quiet
} else if flags.json {
Self::Json
} else if flags.lines {
Self::Lines
} else if flags.plain {
Self::Plain
} else {
default
}
}
}
pub(crate) fn emit(report: &Report, root: &Path, format: Format) -> Result<()> {
match format {
Format::Quiet => Ok(()),
Format::Json => print_json_report(report),
Format::Plain => {
print!("{report}");
Ok(())
}
Format::Lines => {
for line in lines_of(report, root) {
println!("{line}");
}
Ok(())
}
}
}
fn lines_of(report: &Report, root: &Path) -> Vec<String> {
let missing: Vec<&str> = report
.licenses()
.missing
.iter()
.map(String::as_str)
.collect();
let mut out = Vec::new();
for result in report.iter() {
let shown = root.join(&result.path).display().to_string();
match &result.status {
Status::Ok | Status::Skipped => {}
Status::ReadError => out.push(format!("{shown}: read error")),
Status::InvalidExpression(expression) => {
out.push(format!(
"{shown}: invalid SPDX License Expression '{expression}'"
));
out.push(format!("{shown}: no license identifier"));
}
_ => {
if result.license.is_none() {
out.push(format!("{shown}: no license identifier"));
}
if result.copyrights.is_empty() {
out.push(format!("{shown}: no copyright notice"));
}
}
}
if let Some(expression) = &result.license
&& let Ok(parsed) = spdx::parse(expression)
{
for id in parsed.identifiers() {
if missing.contains(&id.as_str()) {
out.push(format!("{shown}: missing license '{id}'"));
}
}
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use comply::license::LicenseAudit;
fn root() -> &'static Path {
Path::new("/project")
}
#[test]
fn quiet_beats_every_other_flag() {
let all = Flags {
quiet: true,
json: true,
plain: true,
lines: true,
};
assert_eq!(Format::resolve(all, Format::Plain), Format::Quiet);
}
#[test]
fn each_flag_selects_its_own_format() {
let json = Flags {
json: true,
..Flags::default()
};
let lines = Flags {
lines: true,
..Flags::default()
};
let plain = Flags {
plain: true,
..Flags::default()
};
assert_eq!(Format::resolve(json, Format::Plain), Format::Json);
assert_eq!(Format::resolve(lines, Format::Plain), Format::Lines);
assert_eq!(Format::resolve(plain, Format::Lines), Format::Plain);
}
#[test]
fn with_no_flags_each_command_keeps_its_own_default() {
assert_eq!(
Format::resolve(Flags::default(), Format::Plain),
Format::Plain
);
assert_eq!(
Format::resolve(Flags::default(), Format::Lines),
Format::Lines
);
}
#[test]
fn an_invalid_expression_is_named_in_the_line_output() {
let report = Report::builder()
.add_result(
"bad.rs",
Status::InvalidExpression("MIT AND AND GPL".to_string()),
vec!["2026 Acme".to_string()],
Some("MIT AND AND GPL".to_string()),
)
.build();
assert_eq!(
lines_of(&report, root()),
vec![
"/project/bad.rs: invalid SPDX License Expression 'MIT AND AND GPL'".to_string(),
"/project/bad.rs: no license identifier".to_string(),
]
);
}
#[test]
fn a_file_missing_both_declarations_produces_two_lines_licence_first() {
let report = Report::builder()
.add_result("bad.py", Status::MissingCopyright, vec![], None)
.build();
assert_eq!(
lines_of(&report, root()),
vec![
"/project/bad.py: no license identifier".to_string(),
"/project/bad.py: no copyright notice".to_string(),
]
);
}
#[test]
fn a_referenced_licence_with_no_file_is_attributed_to_the_file_naming_it() {
let report = Report::builder()
.add_ok_with_header(
"nolicfile.py",
vec!["2026 A".to_string()],
Some("Apache-2.0".to_string()),
)
.licenses(LicenseAudit {
missing: vec!["Apache-2.0".to_string()],
..LicenseAudit::default()
})
.build();
assert_eq!(
lines_of(&report, root()),
vec!["/project/nolicfile.py: missing license 'Apache-2.0'".to_string()]
);
}
#[test]
fn compliant_and_skipped_files_produce_no_lines() {
let report = Report::builder()
.add_ok_with_header("a.rs", vec!["2026 A".to_string()], Some("MIT".to_string()))
.add_skipped("LICENSE")
.build();
assert!(lines_of(&report, root()).is_empty());
}
#[test]
fn a_read_error_says_so_rather_than_blaming_the_header() {
let report = Report::builder()
.add_result("locked.rs", Status::ReadError, vec![], None)
.build();
assert_eq!(
lines_of(&report, root()),
vec!["/project/locked.rs: read error".to_string()]
);
}
#[test]
fn every_format_emits_without_error() {
let report = Report::builder()
.add_result("bad.py", Status::MissingCopyright, vec![], None)
.build();
for format in [Format::Plain, Format::Json, Format::Lines, Format::Quiet] {
assert!(emit(&report, root(), format).is_ok(), "{format:?}");
}
}
}