1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use std::borrow::Cow;

use anyhow::bail;
use bc_components::{Digest, DigestProvider};
use dcbor::prelude::*;

use crate::{Envelope, EnvelopeEncodable};

/// Represents an assertion.
///
/// Generally you do not create an instance of this type directly, but
/// instead use [`Envelope::new_assertion`], or the various functions
/// on ``Envelope`` that create assertions.
#[derive(Clone, Debug)]
pub struct Assertion {
    predicate: Envelope,
    object: Envelope,
    digest: Digest,
}

impl Assertion {
    /// Creates an assertion and calculates its digest.
    pub fn new(predicate: impl EnvelopeEncodable, object: impl EnvelopeEncodable) -> Self {
        let predicate = Envelope::new(predicate);
        let object = Envelope::new(object);
        let digest = Digest::from_digests(&[
            predicate.digest().into_owned(),
            object.digest().into_owned(),
        ]);
        Self {
            predicate,
            object,
            digest,
        }
    }

    /// Returns the predicate of the assertion.
    pub fn predicate(&self) -> Envelope {
        self.predicate.clone()
    }

    /// Returns the object of the assertion.
    pub fn object(&self) -> Envelope {
        self.object.clone()
    }

    /// Returns the digest of the assertion.
    pub fn digest_ref(&self) -> &Digest {
        &self.digest
    }
}

impl PartialEq for Assertion {
    fn eq(&self, other: &Self) -> bool {
        self.digest_ref() == other.digest_ref()
    }
}

impl Eq for Assertion {}

impl DigestProvider for Assertion {
    fn digest(&self) -> Cow<'_, Digest> {
        Cow::Borrowed(&self.digest)
    }
}

impl CBOREncodable for Assertion {
    fn cbor(&self) -> CBOR {
        let mut map = Map::new();
        map.insert(self.predicate.cbor(), self.object.cbor());
        map.cbor()
    }
}

impl From<Assertion> for CBOR {
    fn from(value: Assertion) -> Self {
        value.cbor()
    }
}

impl CBORDecodable for Assertion {
    fn from_cbor(cbor: &CBOR) -> anyhow::Result<Self> {
        if let CBORCase::Map(map) = cbor.case() {
            if map.len() != 1 {
                bail!("assertion map must have exactly one element")
            }
            let elem = map.iter().next().unwrap();
            let predicate = Envelope::from_cbor(elem.0)?;
            let object = Envelope::from_cbor(elem.1)?;
            return Ok(Self::new(predicate, object));
        }
        bail!("assertion must be a map")
    }
}

impl TryFrom<CBOR> for Assertion {
    type Error = anyhow::Error;

    fn try_from(value: CBOR) -> Result<Self, Self::Error> {
        Self::from_cbor(&value)
    }
}

impl CBORCodable for Assertion {}