use std::collections::HashMap;
use std::io::Write;
use std::path::PathBuf;
use ifc::validate::express::{ExpressError, ExpressRules};
use ifc::validate::{validate_with_options, Budget, Path, Report, Severity, ValidateOptions};
use ifc::Schema;
use crate::cli::{ReportFormat, ValidateArgs};
use crate::error::{CliError, CliResult, Outcome};
use crate::input::{self, Loaded};
use crate::json::Json;
use crate::sarif::{self, Level};
struct Item {
severity: Severity,
rule: String,
path: String,
entity: Option<u64>,
attribute_index: Option<usize>,
attribute_name: Option<String>,
line: Option<usize>,
message: String,
}
struct Checked {
schema: String,
conformant: bool,
truncated: bool,
passed: bool,
errors: usize,
evaluation_errors: usize,
warnings: usize,
indeterminate: usize,
unsupported: usize,
items: Vec<Item>,
}
struct ExpressFiles {
files: Vec<(PathBuf, String)>,
parsed: HashMap<(usize, String), Result<ExpressRules, ExpressError>>,
}
impl ExpressFiles {
fn read(paths: &[PathBuf]) -> CliResult<Self> {
let files = paths
.iter()
.map(|path| {
let bytes = std::fs::read(path).map_err(|error| CliError::Io {
path: path.display().to_string(),
detail: error.to_string(),
})?;
let text = String::from_utf8(bytes).unwrap_or_else(|error| {
error.into_bytes().iter().map(|&b| char::from(b)).collect()
});
Ok((path.clone(), text))
})
.collect::<CliResult<_>>()?;
Ok(Self {
files,
parsed: HashMap::new(),
})
}
fn rules_for(&mut self, schema: &Schema) -> CliResult<ExpressRules> {
let mut declaring: Vec<(usize, ExpressRules)> = Vec::new();
let mut invalid = None;
let mut disagreeing = None;
for (index, (path, text)) in self.files.iter().enumerate() {
let outcome = self
.parsed
.entry((index, schema.name().to_owned()))
.or_insert_with(|| ExpressRules::parse(text, schema));
match outcome {
Ok(rules) => declaring.push((index, rules.clone())),
Err(ExpressError::SchemaMismatch { .. }) => {}
Err(error @ ExpressError::TableMismatch { .. }) => {
disagreeing.get_or_insert_with(|| format!("{}: {error}", path.display()));
}
Err(error) => {
invalid.get_or_insert_with(|| format!("{}: {error}", path.display()));
}
}
}
if let Some(detail) = invalid {
return Err(CliError::InvalidExpress(detail));
}
if let Some(detail) = disagreeing {
return Err(CliError::ExpressMismatch(detail));
}
let names = |indices: &mut dyn Iterator<Item = usize>| {
indices
.map(|index| self.files[index].0.display().to_string())
.collect::<Vec<_>>()
.join(", ")
};
match declaring.len() {
1 => Ok(declaring.remove(0).1),
0 => Err(CliError::ExpressMismatch(format!(
"no --express file declares {} (given: {})",
schema.name(),
names(&mut (0..self.files.len()))
))),
_ => Err(CliError::ExpressMismatch(format!(
"more than one --express file declares {}: {}",
schema.name(),
names(&mut declaring.iter().map(|(index, _)| *index))
))),
}
}
}
pub(crate) fn run(args: &ValidateArgs, out: &mut impl Write) -> CliResult<Outcome> {
let budget = args
.max_findings
.map_or(Budget::DEFAULT, |max_findings| Budget { max_findings });
let files: Vec<String> = args
.files
.iter()
.map(|file| file.display().to_string())
.collect();
let mut express = ExpressFiles::read(&args.express)?;
let results: Vec<CliResult<Checked>> = args
.files
.iter()
.map(|file| {
let loaded = input::read(file, args.input.input_layout)?;
check(&loaded, budget, args, &mut express)
})
.collect();
match args.format {
ReportFormat::Human => human(&files, &results, !args.express.is_empty(), out),
ReportFormat::Json => json(&files, &results, out),
ReportFormat::Sarif => sarif(&files, &results, out),
}
.map_err(|error| CliError::stdout(&error))?;
Ok(verdict(&results))
}
fn verdict(results: &[CliResult<Checked>]) -> Outcome {
if results.iter().any(Result::is_err) {
Outcome::Incomplete
} else if results.iter().flatten().any(|checked| !checked.passed) {
Outcome::Findings
} else {
Outcome::Clean
}
}
fn check(
loaded: &Loaded,
budget: Budget,
args: &ValidateArgs,
express: &mut ExpressFiles,
) -> CliResult<Checked> {
let schema = input::declared_schema(&loaded.model)?;
let rules = if express.files.is_empty() {
None
} else {
Some(express.rules_for(schema)?)
};
let mut options = ValidateOptions::new().budget(budget);
if let Some(rules) = &rules {
options = options.express(rules);
}
let report: Report = validate_with_options(&loaded.model, schema, &options);
let summary = report.summary();
let line = |id: u64| loaded.lines.as_ref().and_then(|lines| lines.line(id));
let items = report
.sorted()
.into_iter()
.filter(|finding| args.include_unsupported || finding.severity != Severity::Unsupported)
.map(|finding| {
let (entity, attribute_index, attribute_name) = match &finding.path {
Path::Entity(id) => (Some(id.0), None, None),
Path::Attribute {
entity,
index,
name,
} => (Some(entity.0), Some(*index), name.clone()),
_ => (None, None, None),
};
Item {
severity: finding.severity,
rule: finding.rule.clone(),
path: finding.path.to_string(),
entity,
attribute_index,
attribute_name,
line: entity.and_then(line),
message: finding.message.clone(),
}
})
.collect();
let conformant = report.is_conformant();
let truncated = report.is_truncated();
Ok(Checked {
schema: loaded
.model
.header()
.schema_token()
.unwrap_or_default()
.to_owned(),
conformant,
truncated,
passed: conformant && !truncated && !(args.deny_warnings && summary.warnings > 0),
errors: summary.errors,
evaluation_errors: summary.evaluation_errors,
warnings: summary.warnings,
indeterminate: summary.indeterminate,
unsupported: summary.unsupported,
items,
})
}
fn plural(count: usize, one: &str, many: &str) -> String {
format!("{count} {}", if count == 1 { one } else { many })
}
fn human(
files: &[String],
results: &[CliResult<Checked>],
express: bool,
out: &mut impl Write,
) -> std::io::Result<()> {
for (file, result) in files.iter().zip(results) {
let checked = match result {
Ok(checked) => checked,
Err(error) => {
eprintln!("openbim-ifc: {file}: not validated: {error}");
continue;
}
};
for item in &checked.items {
let at = match item.line {
Some(line) => format!("{file}:{line}"),
None => file.clone(),
};
let target = if item.entity.is_some() {
format!(" {}", item.path)
} else {
String::new()
};
writeln!(
out,
"{at}: {} [{}]{target}: {}",
item.severity, item.rule, item.message
)?;
}
let verdict = match (checked.passed, checked.conformant) {
(true, _) => "conformant",
(false, true) if checked.truncated => "verdict not final (report truncated)",
(false, true) => "fails on warnings",
(false, false) => "not conformant",
};
let truncated = if checked.truncated {
" (truncated: counts are lower bounds)"
} else {
""
};
let indeterminate = if express {
format!(", {} indeterminate", checked.indeterminate)
} else {
String::new()
};
writeln!(
out,
"{file}: {}: {verdict}: {}, {}, {}{indeterminate}; {} not evaluated{truncated}",
checked.schema,
plural(checked.errors, "error", "errors"),
plural(
checked.evaluation_errors,
"evaluation error",
"evaluation errors"
),
plural(checked.warnings, "warning", "warnings"),
plural(checked.unsupported, "rule", "rules"),
)?;
}
Ok(())
}
fn json(
files: &[String],
results: &[CliResult<Checked>],
out: &mut impl Write,
) -> std::io::Result<()> {
let reports = files
.iter()
.zip(results)
.map(|(file, result)| match result {
Ok(checked) => Json::object([
("file", Json::str(file.clone())),
("schema", Json::str(checked.schema.clone())),
("passed", Json::Bool(checked.passed)),
("conformant", Json::Bool(checked.conformant)),
("truncated", Json::Bool(checked.truncated)),
(
"summary",
Json::object([
("errors", Json::uint(checked.errors as u64)),
(
"evaluation_errors",
Json::uint(checked.evaluation_errors as u64),
),
("warnings", Json::uint(checked.warnings as u64)),
("indeterminate", Json::uint(checked.indeterminate as u64)),
("unsupported", Json::uint(checked.unsupported as u64)),
]),
),
(
"findings",
Json::Array(
checked
.items
.iter()
.map(|item| {
Json::object([
("severity", Json::str(item.severity.to_string())),
("rule", Json::str(item.rule.clone())),
("path", Json::str(item.path.clone())),
("entity", Json::opt_uint(item.entity)),
(
"attribute_index",
Json::opt_uint(item.attribute_index.map(|i| i as u64)),
),
("attribute_name", Json::opt_str(item.attribute_name.clone())),
("line", Json::opt_uint(item.line.map(|l| l as u64))),
("message", Json::str(item.message.clone())),
])
})
.collect(),
),
),
]),
Err(error) => Json::object([
("file", Json::str(file.clone())),
(
"error",
Json::object([
("kind", Json::str(error.kind())),
("message", Json::str(error.to_string())),
]),
),
]),
})
.collect();
let document = Json::object([
("tool", Json::str("openbim-ifc")),
("version", Json::str(env!("CARGO_PKG_VERSION"))),
("passed", Json::Bool(verdict(results) == Outcome::Clean)),
("files", Json::Array(reports)),
]);
out.write_all(document.pretty().as_bytes())
}
fn sarif(
files: &[String],
results: &[CliResult<Checked>],
out: &mut impl Write,
) -> std::io::Result<()> {
let mut run = sarif::Run {
files: files.to_vec(),
..sarif::Run::default()
};
for (index, result) in results.iter().enumerate() {
match result {
Ok(checked) => {
run.findings
.extend(checked.items.iter().map(|item| sarif::Finding {
rule: item.rule.clone(),
level: match item.severity {
Severity::Error | Severity::EvaluationError => Level::Error,
Severity::Warning => Level::Warning,
_ => Level::Note,
},
message: item.message.clone(),
file: index,
line: item.line,
entity: item.entity.map(|_| item.path.clone()),
is_attribute: item.attribute_index.is_some(),
}));
}
Err(error) => run.failures.push((index, error.to_string())),
}
}
out.write_all(run.to_json().pretty().as_bytes())
}