Skip to main content

aprender_contracts_cli/commands/
validate.rs

1use std::path::Path;
2
3use provable_contracts::error::Severity;
4use provable_contracts::schema::{parse_contract, validate_contract};
5
6pub fn run(path: &Path) -> Result<(), Box<dyn std::error::Error>> {
7    let contract = parse_contract(path)?;
8    let violations = validate_contract(&contract);
9
10    let errors: Vec<_> = violations
11        .iter()
12        .filter(|v| v.severity == Severity::Error)
13        .collect();
14    let warnings: Vec<_> = violations
15        .iter()
16        .filter(|v| v.severity == Severity::Warning)
17        .collect();
18
19    for v in &violations {
20        println!("{v}");
21    }
22
23    println!("\n{} error(s), {} warning(s)", errors.len(), warnings.len());
24
25    if errors.is_empty() {
26        println!("Contract is valid.");
27        Ok(())
28    } else {
29        Err(format!("Contract has {} validation error(s)", errors.len()).into())
30    }
31}