cml_chain/assets/
mod.rs

1// This file was code-generated using an experimental CDDL to rust tool:
2// https://github.com/dcSpark/cddl-codegen
3
4pub mod cbor_encodings;
5pub mod serialization;
6pub mod utils;
7
8pub use utils::*; //{Value, AssetBundle, Mint, MultiAsset, Coin, PositiveCoin, NonZeroInt64};
9
10use cbor_encodings::AssetNameEncoding;
11use cml_core::error::*;
12
13use std::convert::TryFrom;
14
15/// Use TryFrom<&str> / TryInto<&str> for utf8 text conversion and RawBytesEncoding for direct bytes access
16#[derive(Clone, Debug, derivative::Derivative)]
17#[derivative(Eq, PartialEq, Ord, PartialOrd, Hash)]
18pub struct AssetName {
19    pub inner: Vec<u8>,
20    #[derivative(
21        PartialEq = "ignore",
22        Ord = "ignore",
23        PartialOrd = "ignore",
24        Hash = "ignore"
25    )]
26    pub encodings: Option<AssetNameEncoding>,
27}
28
29impl AssetName {
30    pub fn new(inner: Vec<u8>) -> Result<Self, DeserializeError> {
31        if inner.len() > 32 {
32            return Err(DeserializeError::new(
33                "AssetName",
34                DeserializeFailure::RangeCheck {
35                    found: inner.len() as isize,
36                    min: Some(0),
37                    max: Some(32),
38                },
39            ));
40        }
41        Ok(Self {
42            inner,
43            encodings: None,
44        })
45    }
46}
47
48impl TryFrom<Vec<u8>> for AssetName {
49    type Error = DeserializeError;
50
51    fn try_from(inner: Vec<u8>) -> Result<Self, Self::Error> {
52        AssetName::new(inner)
53    }
54}
55
56impl From<AssetName> for Vec<u8> {
57    fn from(wrapper: AssetName) -> Self {
58        wrapper.inner
59    }
60}
61
62impl serde::Serialize for AssetName {
63    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
64    where
65        S: serde::Serializer,
66    {
67        serializer.serialize_str(&hex::encode(self.inner.clone()))
68    }
69}
70
71impl<'de> serde::de::Deserialize<'de> for AssetName {
72    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
73    where
74        D: serde::de::Deserializer<'de>,
75    {
76        let s = <String as serde::de::Deserialize>::deserialize(deserializer)?;
77        hex::decode(&s)
78            .ok()
79            .and_then(|bytes| AssetName::new(bytes).ok())
80            .ok_or_else(|| {
81                serde::de::Error::invalid_value(
82                    serde::de::Unexpected::Str(&s),
83                    &"invalid hex bytes",
84                )
85            })
86    }
87}
88
89impl schemars::JsonSchema for AssetName {
90    fn schema_name() -> String {
91        String::from("AssetName")
92    }
93
94    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
95        String::json_schema(gen)
96    }
97
98    fn is_referenceable() -> bool {
99        String::is_referenceable()
100    }
101}