Skip to main content

stellar_xdr/generated/
memo.rs

1#[allow(unused_imports, clippy::wildcard_imports)]
2use super::*;
3
4/// Memo is an XDR Union defined as:
5///
6/// ```text
7/// union Memo switch (MemoType type)
8/// {
9/// case MEMO_NONE:
10///     void;
11/// case MEMO_TEXT:
12///     string text<28>;
13/// case MEMO_ID:
14///     uint64 id;
15/// case MEMO_HASH:
16///     Hash hash; // the hash of what to pull from the content server
17/// case MEMO_RETURN:
18///     Hash retHash; // the hash of the tx you are rejecting
19/// };
20/// ```
21///
22// union with discriminant MemoType
23#[cfg_attr(feature = "serde", cfg_eval::cfg_eval)]
24#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
25#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
26#[cfg_attr(
27    all(feature = "serde", feature = "alloc"),
28    serde_with::serde_as,
29    derive(serde::Serialize, serde::Deserialize),
30    serde(rename_all = "snake_case")
31)]
32#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
33#[allow(clippy::large_enum_variant)]
34pub enum Memo {
35    None,
36    Text(StringM<28>),
37    Id(
38        #[cfg_attr(
39            all(feature = "serde", feature = "alloc"),
40            serde_as(as = "NumberOrString")
41        )]
42        u64,
43    ),
44    Hash(Hash),
45    Return(Hash),
46}
47
48#[cfg(feature = "alloc")]
49impl Default for Memo {
50    fn default() -> Self {
51        Self::None
52    }
53}
54
55impl Memo {
56    const _VARIANTS: &[MemoType] = &[
57        MemoType::None,
58        MemoType::Text,
59        MemoType::Id,
60        MemoType::Hash,
61        MemoType::Return,
62    ];
63    pub const VARIANTS: [MemoType; Self::_VARIANTS.len()] = {
64        let mut arr = [Self::_VARIANTS[0]; Self::_VARIANTS.len()];
65        let mut i = 1;
66        while i < Self::_VARIANTS.len() {
67            arr[i] = Self::_VARIANTS[i];
68            i += 1;
69        }
70        arr
71    };
72    const _VARIANTS_STR: &[&str] = &["None", "Text", "Id", "Hash", "Return"];
73    pub const VARIANTS_STR: [&'static str; Self::_VARIANTS_STR.len()] = {
74        let mut arr = [Self::_VARIANTS_STR[0]; Self::_VARIANTS_STR.len()];
75        let mut i = 1;
76        while i < Self::_VARIANTS_STR.len() {
77            arr[i] = Self::_VARIANTS_STR[i];
78            i += 1;
79        }
80        arr
81    };
82
83    #[must_use]
84    pub const fn name(&self) -> &'static str {
85        match self {
86            Self::None => "None",
87            Self::Text(_) => "Text",
88            Self::Id(_) => "Id",
89            Self::Hash(_) => "Hash",
90            Self::Return(_) => "Return",
91        }
92    }
93
94    #[must_use]
95    pub const fn discriminant(&self) -> MemoType {
96        #[allow(clippy::match_same_arms)]
97        match self {
98            Self::None => MemoType::None,
99            Self::Text(_) => MemoType::Text,
100            Self::Id(_) => MemoType::Id,
101            Self::Hash(_) => MemoType::Hash,
102            Self::Return(_) => MemoType::Return,
103        }
104    }
105
106    #[must_use]
107    pub const fn variants() -> [MemoType; Self::_VARIANTS.len()] {
108        Self::VARIANTS
109    }
110}
111
112impl Name for Memo {
113    #[must_use]
114    fn name(&self) -> &'static str {
115        Self::name(self)
116    }
117}
118
119impl Discriminant<MemoType> for Memo {
120    #[must_use]
121    fn discriminant(&self) -> MemoType {
122        Self::discriminant(self)
123    }
124}
125
126impl Variants<MemoType> for Memo {
127    fn variants() -> slice::Iter<'static, MemoType> {
128        Self::VARIANTS.iter()
129    }
130}
131
132impl Union<MemoType> for Memo {}
133
134impl ReadXdr for Memo {
135    #[cfg(feature = "std")]
136    fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
137        r.with_limited_depth(|r| {
138            let dv: MemoType = <MemoType as ReadXdr>::read_xdr(r)?;
139            #[allow(clippy::match_same_arms, clippy::match_wildcard_for_single_variants)]
140            let v = match dv {
141                MemoType::None => Self::None,
142                MemoType::Text => Self::Text(StringM::<28>::read_xdr(r)?),
143                MemoType::Id => Self::Id(u64::read_xdr(r)?),
144                MemoType::Hash => Self::Hash(Hash::read_xdr(r)?),
145                MemoType::Return => Self::Return(Hash::read_xdr(r)?),
146                #[allow(unreachable_patterns)]
147                _ => return Err(Error::Invalid),
148            };
149            Ok(v)
150        })
151    }
152}
153
154impl WriteXdr for Memo {
155    #[cfg(feature = "std")]
156    fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
157        w.with_limited_depth(|w| {
158            self.discriminant().write_xdr(w)?;
159            #[allow(clippy::match_same_arms)]
160            match self {
161                Self::None => ().write_xdr(w)?,
162                Self::Text(v) => v.write_xdr(w)?,
163                Self::Id(v) => v.write_xdr(w)?,
164                Self::Hash(v) => v.write_xdr(w)?,
165                Self::Return(v) => v.write_xdr(w)?,
166            };
167            Ok(())
168        })
169    }
170}