#![forbid(unsafe_code)]
use alloc::format;
use alloc::string::String;
use cbcl_core::blame::ViolationError;
use cbcl_core::serializer::serialize;
use cbcl_core::sexpr::{Atom, SExpr};
use crate::parser;
use crate::state_exports::parse_and_install_dialect;
pub fn verify_message_shape_str(input: &str) -> Result<String, String> {
let frame = parser::parse(input).map_err(|e| format!("parse error: {e}"))?;
let items = match &frame {
SExpr::List(items) => items,
_ => {
return Err(String::from(
"expected (verify-shape <dialect-or-chain> <performative> <message>)",
))
}
};
if items.len() != 4 || !matches!(&items[0], SExpr::Atom(Atom::Symbol(s)) if s == "verify-shape")
{
return Err(String::from(
"expected (verify-shape <dialect-or-chain> <performative> <message>)",
));
}
let performative = match &items[2] {
SExpr::Atom(Atom::Symbol(s)) => s.clone(),
_ => return Err(String::from("performative must be a symbol")),
};
let dialect_sexpr = &items[1];
let message_sexpr = &items[3];
let registry = parse_and_install_dialect(dialect_sexpr)?;
for d in registry.iter() {
for shape in &d.shapes {
if shape.performative != performative {
continue;
}
if let Err(violation) = shape.check(message_sexpr) {
let blame = ViolationError::from_shape_violation(&violation, None, None, None)
.with_dialect_context(
&d.name,
d.author.as_deref(),
d.hash.as_deref(),
Some(&performative),
);
return Err(serialize(&blame.to_sexpr()));
}
}
}
Ok(String::from("ok"))
}
#[cfg(test)]
mod tests {
use super::*;
const D: &str = "(define d (cbcl) @a (extend vote (to choice) (tell to :choice choice)) (shape vote (require :choice string)))";
#[test]
fn shape_verdicts() {
assert_eq!(
verify_message_shape_str(&format!("(verify-shape {D} vote (vote @r :choice \"x\"))"))
.unwrap(),
"ok"
);
let blame =
verify_message_shape_str(&format!("(verify-shape {D} vote (vote @r :choice 1))"))
.unwrap_err();
assert!(blame.contains("choice"), "{blame}");
assert!(verify_message_shape_str(&format!("(verify-shape {D} vote (vote @r))")).is_err());
assert_eq!(
verify_message_shape_str(&format!("(verify-shape {D} other (other @r))")).unwrap(),
"ok"
);
assert!(verify_message_shape_str("(verify-shape")
.unwrap_err()
.starts_with("parse error"));
}
}