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
#[cfg(feature = "radix_engine_fuzzing")]
use arbitrary::Arbitrary;
use sbor::rust::convert::TryFrom;
#[cfg(not(feature = "alloc"))]
use sbor::rust::fmt;
use sbor::rust::vec::Vec;
use sbor::*;

use crate::data::manifest::*;
use crate::*;

#[cfg_attr(feature = "radix_engine_fuzzing", derive(Arbitrary))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[must_use]
pub struct ManifestProof(pub u32);

//========
// error
//========

/// Represents an error when parsing ManifestProof.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParseManifestProofError {
    InvalidLength,
}

#[cfg(not(feature = "alloc"))]
impl std::error::Error for ParseManifestProofError {}

#[cfg(not(feature = "alloc"))]
impl fmt::Display for ParseManifestProofError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

//========
// binary
//========

impl TryFrom<&[u8]> for ManifestProof {
    type Error = ParseManifestProofError;

    fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
        if slice.len() != 4 {
            return Err(Self::Error::InvalidLength);
        }
        Ok(Self(u32::from_le_bytes(slice.try_into().unwrap())))
    }
}

impl ManifestProof {
    pub fn to_vec(&self) -> Vec<u8> {
        self.0.to_le_bytes().to_vec()
    }
}

manifest_type!(ManifestProof, ManifestCustomValueKind::Proof, 4);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn manifest_proof_fail() {
        let proof = ManifestProof(37);
        let mut proof_vec = proof.to_vec();

        assert!(ManifestProof::try_from(proof_vec.as_slice()).is_ok());

        // malform encoded vector
        proof_vec.push(0);
        let proof_out = ManifestProof::try_from(proof_vec.as_slice());
        assert!(matches!(
            proof_out,
            Err(ParseManifestProofError::InvalidLength)
        ));

        #[cfg(not(feature = "alloc"))]
        println!("Manifest Proof error: {}", proof_out.unwrap_err());
    }

    #[test]
    fn manifest_proof_encode_decode_fail() {
        let mut buf = Vec::new();
        let mut encoder = VecEncoder::<ManifestCustomValueKind>::new(&mut buf, 1);
        let malformed_value: u8 = 1; // use u8 instead of u32 should inovke an error
        encoder.write_slice(&malformed_value.to_le_bytes()).unwrap();

        let mut decoder = VecDecoder::<ManifestCustomValueKind>::new(&buf, 1);
        let proof_output = decoder
            .decode_deeper_body_with_value_kind::<ManifestProof>(ManifestProof::value_kind());

        // expecting 4 bytes, found only 1, so Buffer Underflow error should occur
        assert!(matches!(
            proof_output,
            Err(DecodeError::BufferUnderflow { .. })
        ));
    }
}