radix_common/data/manifest/model/
manifest_address_reservation.rs

1use crate::data::manifest::*;
2use crate::internal_prelude::*;
3use crate::*;
4
5#[cfg_attr(
6    feature = "fuzzing",
7    derive(::arbitrary::Arbitrary, ::serde::Serialize, ::serde::Deserialize)
8)]
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10#[must_use]
11pub struct ManifestAddressReservation(pub u32);
12
13labelled_resolvable_with_identity_impl!(ManifestAddressReservation, resolver_output: Self);
14
15//========
16// error
17//========
18
19/// Represents an error when parsing ManifestAddressReservation.
20#[derive(Debug, Clone, PartialEq, Eq)]
21pub enum ParseManifestAddressReservationError {
22    InvalidLength,
23}
24
25#[cfg(not(feature = "alloc"))]
26impl std::error::Error for ParseManifestAddressReservationError {}
27
28#[cfg(not(feature = "alloc"))]
29impl fmt::Display for ParseManifestAddressReservationError {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        write!(f, "{:?}", self)
32    }
33}
34
35//========
36// binary
37//========
38
39impl TryFrom<&[u8]> for ManifestAddressReservation {
40    type Error = ParseManifestAddressReservationError;
41
42    fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
43        if slice.len() != 4 {
44            return Err(Self::Error::InvalidLength);
45        }
46        Ok(Self(u32::from_le_bytes(slice.try_into().unwrap())))
47    }
48}
49
50impl ManifestAddressReservation {
51    pub fn to_vec(&self) -> Vec<u8> {
52        self.0.to_le_bytes().to_vec()
53    }
54}
55
56manifest_type!(
57    ManifestAddressReservation,
58    ManifestCustomValueKind::AddressReservation,
59    4
60);
61scrypto_describe_for_manifest_type!(
62    ManifestAddressReservation,
63    OWN_GLOBAL_ADDRESS_RESERVATION_TYPE,
64    own_global_address_reservation_type_data,
65);
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn manifest_address_reservation_fail() {
73        let address = ManifestAddressReservation(0);
74        let mut address_vec = address.to_vec();
75
76        assert!(ManifestAddressReservation::try_from(address_vec.as_slice()).is_ok());
77
78        // malform encoded vector
79        address_vec.push(0);
80        let address_out = ManifestAddressReservation::try_from(address_vec.as_slice());
81        assert_matches!(
82            address_out,
83            Err(ParseManifestAddressReservationError::InvalidLength)
84        );
85
86        #[cfg(not(feature = "alloc"))]
87        println!(
88            "Manifest Address Reservation error: {}",
89            address_out.unwrap_err()
90        );
91    }
92
93    #[test]
94    fn manifest_address_reservation_encode_decode_fail() {
95        let mut buf = Vec::new();
96        let mut encoder = VecEncoder::<ManifestCustomValueKind>::new(&mut buf, 1);
97        let malformed_value: u8 = 1; // use u8 instead of u32 should inovke an error
98        encoder.write_slice(&malformed_value.to_le_bytes()).unwrap();
99
100        let mut decoder = VecDecoder::<ManifestCustomValueKind>::new(&buf, 1);
101        let addr_output = decoder.decode_deeper_body_with_value_kind::<ManifestAddressReservation>(
102            ManifestAddressReservation::value_kind(),
103        );
104
105        // expecting 4 bytes, found only 1, so Buffer Underflow error should occur
106        assert_matches!(addr_output, Err(DecodeError::BufferUnderflow { .. }));
107    }
108}