aprender_contracts_cli/commands/
validate.rs1use std::path::Path;
2
3use provable_contracts::error::Severity;
4use provable_contracts::schema::{validate_artifact, ArtifactKind};
5
6pub fn run(path: &Path) -> Result<(), Box<dyn std::error::Error>> {
15 let (kind, violations) = validate_artifact(path)?;
16
17 let errors: Vec<_> = violations
18 .iter()
19 .filter(|v| v.severity == Severity::Error)
20 .collect();
21 let warnings: Vec<_> = violations
22 .iter()
23 .filter(|v| v.severity == Severity::Warning)
24 .collect();
25
26 for v in &violations {
27 println!("{v}");
28 }
29
30 println!("\n{} error(s), {} warning(s)", errors.len(), warnings.len());
31
32 if errors.is_empty() {
33 println!("{} is valid.", noun(kind));
34 Ok(())
35 } else {
36 Err(format!("{} has {} validation error(s)", noun(kind), errors.len()).into())
37 }
38}
39
40fn noun(kind: ArtifactKind) -> &'static str {
44 match kind {
45 ArtifactKind::Contract => "Contract",
46 ArtifactKind::Binding => "Binding registry (kind: binding)",
47 ArtifactKind::PublishManifest => "Publish manifest (kind: publish-manifest)",
48 }
49}