use std::path::Path;
use std::process::ExitCode;
use geopackage::{GeoPackage, Severity};
use crate::error::Result;
pub fn run(path: &Path, strict: bool) -> Result<ExitCode> {
let gpkg = GeoPackage::open_read_only_lenient(path)?;
let findings = gpkg.validate()?;
println!("{}", path.display());
if findings.is_empty() {
println!(" no findings (this crate's checks; the OGC ETS is the authority)");
return Ok(ExitCode::SUCCESS);
}
for finding in &findings {
println!(" {}: {finding}", finding.severity());
if let Some(repair) = finding.repair() {
println!(" repair: {repair}");
}
}
let errors = count(&findings, Severity::Error);
let warnings = count(&findings, Severity::Warning);
let advisories = count(&findings, Severity::Advisory);
println!(
"\n {}, {}, {}",
plural(errors, "error", "errors"),
plural(warnings, "warning", "warnings"),
plural(advisories, "advisory", "advisories")
);
let failed = if strict {
errors + warnings > 0
} else {
errors > 0
};
Ok(if failed {
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
})
}
fn count(findings: &[geopackage::Finding], severity: Severity) -> usize {
findings
.iter()
.filter(|finding| finding.severity() == severity)
.count()
}
fn plural(count: usize, one: &str, many: &str) -> String {
if count == 1 {
format!("{count} {one}")
} else {
format!("{count} {many}")
}
}