use serde::{Deserialize, Serialize};
use super::*;
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct FiringReceiptMCard(FiringReceiptMCardData);
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FiringReceiptMCardData {
pub card: MCardRecord,
pub net_id: String,
pub transition_id: String,
pub step_seq: usize,
pub fiber_state_trace: Vec<String>,
pub consumed_refs: BTreeMap<String, Vec<String>>,
pub produced_refs: BTreeMap<String, Vec<String>>,
pub pre_marking_hash: String,
pub post_marking_hash: String,
pub verdict: String,
}
impl std::ops::Deref for FiringReceiptMCard {
type Target = FiringReceiptMCardData;
fn deref(&self) -> &Self::Target { &self.0 }
}
impl<'de> Deserialize<'de> for FiringReceiptMCard {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let model = Self(FiringReceiptMCardData::deserialize(deserializer)?);
if model.verify() { Ok(model) } else { Err(serde::de::Error::custom("MCard view differs from its verified record")) }
}
}
impl FiringReceiptMCard {
const KIND: &'static str = "firing_receipt_mcard";
pub fn create(
net_id: &str,
transition_id: &str,
step_seq: usize,
fiber_state_trace: Vec<String>,
consumed_refs: BTreeMap<String, Vec<String>>,
produced_refs: BTreeMap<String, Vec<String>>,
pre_marking_hash: &str,
post_marking_hash: &str,
verdict: &str,
algorithm: Option<&str>,
timestamp: Option<&str>,
) -> Result<Self, String> {
let envelope = json!({
"consumed_refs": consumed_refs,
"fiber_state_trace": fiber_state_trace,
"kind": Self::KIND,
"net_id": net_id,
"post_marking_hash": post_marking_hash,
"pre_marking_hash": pre_marking_hash,
"produced_refs": produced_refs,
"step_seq": step_seq,
"transition_id": transition_id,
"verdict": verdict,
});
let card = record_from_canonical_json(&envelope, algorithm, timestamp)?;
Ok(Self(FiringReceiptMCardData {
card,
net_id: net_id.to_string(),
transition_id: transition_id.to_string(),
step_seq,
fiber_state_trace,
consumed_refs,
produced_refs,
pre_marking_hash: pre_marking_hash.to_string(),
post_marking_hash: post_marking_hash.to_string(),
verdict: verdict.to_string(),
}))
}
pub fn verify(&self) -> bool {
crate::cpn::mcard_token::token_models::verify_envelope(&self.card, self, Self::KIND)
}
pub fn qualified_hash(&self) -> String {
qualified_record_hash(&self.card).expect("immutable verified MCard")
}
}