use serde::{Deserialize, Serialize};
use serde_json::Value;
pub use crate::domain::{
BailVerdict, BooleanPCard, CordisCoeffects, DynamicPCard, InvariantPhase,
MCard as DomainMCard, MCardPayload, PCardDefinition, PrimitivePCard,
};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MCard {
pub hash: Option<String>,
pub content: Value,
pub g_time: Option<u64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PCard {
pub abstract_spec: Value,
pub concrete_impl: Value,
#[serde(skip_serializing_if = "Option::is_none")]
pub balanced_exp: Option<Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VCard {
pub v_pre: Option<Value>,
pub p_card: Option<Value>,
pub v_post: Option<Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AMCardSignature {
pub name: String,
pub inputs: Value,
pub outputs: Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AMCardInvariants {
#[serde(default)]
pub preconditions: Vec<String>,
#[serde(default)]
pub postconditions: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AMCard {
#[serde(default)]
pub context: Option<String>,
#[serde(default)]
pub goal: Option<String>,
pub signature: AMCardSignature,
pub invariants: AMCardInvariants,
}
pub fn parse_manifest(file_path: &str) -> Result<Value, String> {
crate::yaml::parse_to_mcards(file_path)
.map_err(|e| e.to_string())
.and_then(|json_str| {
serde_json::from_str(&json_str)
.map_err(|e| format!("Failed to deserialize normalized AST: {}", e))
})
}
pub fn parse_mcard(file_path: &str) -> Result<MCard, String> {
let ast = parse_manifest(file_path)?;
serde_json::from_value(ast)
.map_err(|e| format!("Failed to cast AST to MCard: {}", e))
}
pub fn validate_a_mcard_schema(val: &Value) -> Result<(), String> {
let root = if let Some(clm) = val.get("clm") { clm } else { val };
let abstract_spec = if let Some(spec) = root.get("abstract_spec") {
spec
} else {
if root.get("signature").is_some() {
root
} else {
return Err("Missing 'abstract_spec' block or direct 'signature' block".to_string());
}
};
let signature = abstract_spec.get("signature")
.ok_or_else(|| "Missing 'signature' block".to_string())?;
let _name = signature.get("name")
.and_then(|v| v.as_str())
.ok_or_else(|| "signature block is missing string 'name'".to_string())?;
let _inputs = signature.get("inputs")
.ok_or_else(|| "signature block is missing 'inputs'".to_string())?;
let _outputs = signature.get("outputs")
.ok_or_else(|| "signature block is missing 'outputs'".to_string())?;
let invariants = abstract_spec.get("invariants")
.ok_or_else(|| "Missing 'invariants' block".to_string())?;
if let Some(pre) = invariants.get("preconditions") {
if !pre.is_array() {
return Err("invariants.preconditions must be an array of strings".to_string());
}
for item in pre.as_array().unwrap() {
if !item.is_string() {
return Err("invariants.preconditions items must be strings".to_string());
}
}
}
if let Some(post) = invariants.get("postconditions") {
if !post.is_array() {
return Err("invariants.postconditions must be an array of strings".to_string());
}
for item in post.as_array().unwrap() {
if !item.is_string() {
return Err("invariants.postconditions items must be strings".to_string());
}
}
}
Ok(())
}
pub fn parse_a_mcard(file_path: &str) -> Result<AMCard, String> {
let ast = parse_manifest(file_path)?;
validate_a_mcard_schema(&ast)?;
let root = if let Some(clm) = ast.get("clm") { clm } else { &ast };
let abstract_spec = root.get("abstract_spec").unwrap_or(root);
serde_json::from_value(abstract_spec.clone())
.map_err(|e| format!("Failed to cast AST to AMCard: {}", e))
}