radix_common/data/scrypto/model/
own.rs

1use crate::data::scrypto::ScryptoCustomValueKind;
2use crate::types::NodeId;
3use crate::*;
4
5use radix_rust::copy_u8_array;
6#[cfg(not(feature = "alloc"))]
7use sbor::rust::fmt;
8use sbor::rust::prelude::*;
9use sbor::*;
10
11#[cfg_attr(feature = "fuzzing", derive(::arbitrary::Arbitrary))]
12#[derive(Clone, Copy, PartialEq, Eq, Hash)]
13pub struct Own(pub NodeId);
14
15impl Own {
16    pub fn to_vec(&self) -> Vec<u8> {
17        self.0.to_vec()
18    }
19
20    pub fn as_node_id(&self) -> &NodeId {
21        &self.0
22    }
23}
24
25impl TryFrom<&[u8]> for Own {
26    type Error = ParseOwnError;
27
28    fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
29        match slice.len() {
30            NodeId::LENGTH => Ok(Self(NodeId(copy_u8_array(slice)))),
31            _ => Err(ParseOwnError::InvalidLength(slice.len())),
32        }
33    }
34}
35
36//========
37// error
38//========
39
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub enum ParseOwnError {
42    InvalidLength(usize),
43}
44
45#[cfg(not(feature = "alloc"))]
46impl std::error::Error for ParseOwnError {}
47
48#[cfg(not(feature = "alloc"))]
49impl fmt::Display for ParseOwnError {
50    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51        write!(f, "{:?}", self)
52    }
53}
54
55//========
56// binary
57//========
58
59well_known_scrypto_custom_type!(
60    Own,
61    ScryptoCustomValueKind::Own,
62    Type::Own,
63    NodeId::LENGTH,
64    OWN_TYPE,
65    own_type_data
66);
67
68//======
69// text
70//======
71
72impl fmt::Debug for Own {
73    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
74        write!(f, "Own({})", hex::encode(self.0))
75    }
76}