eosio_scale_info/ty/
variant.rs1use crate::prelude::vec::Vec;
16
17use crate::{
18 form::{
19 Form,
20 MetaForm,
21 PortableForm,
22 },
23 Field,
24 IntoPortable,
25 Registry,
26};
27use derive_more::From;
28use scale::Encode;
29#[cfg(feature = "serde")]
30use serde::{
31 de::DeserializeOwned,
32 Deserialize,
33 Serialize,
34};
35
36#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
75#[cfg_attr(
76 feature = "serde",
77 serde(bound(
78 serialize = "T::Type: Serialize, T::String: Serialize",
79 deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned",
80 ))
81)]
82#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
83#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
84#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, From, Encode)]
85pub struct TypeDefVariant<T: Form = MetaForm> {
86 #[cfg_attr(
88 feature = "serde",
89 serde(skip_serializing_if = "Vec::is_empty", default)
90 )]
91 variants: Vec<Variant<T>>,
92}
93
94impl IntoPortable for TypeDefVariant {
95 type Output = TypeDefVariant<PortableForm>;
96
97 fn into_portable(self, registry: &mut Registry) -> Self::Output {
98 TypeDefVariant {
99 variants: registry.map_into_portable(self.variants),
100 }
101 }
102}
103
104impl TypeDefVariant {
105 pub fn new<I>(variants: I) -> Self
107 where
108 I: IntoIterator<Item = Variant>,
109 {
110 Self {
111 variants: variants.into_iter().collect(),
112 }
113 }
114}
115
116impl<T> TypeDefVariant<T>
117where
118 T: Form,
119{
120 pub fn variants(&self) -> &[Variant<T>] {
122 &self.variants
123 }
124}
125
126#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
142#[cfg_attr(
143 feature = "serde",
144 serde(bound(
145 serialize = "T::Type: Serialize, T::String: Serialize",
146 deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned",
147 ))
148)]
149#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
150#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Encode)]
151pub struct Variant<T: Form = MetaForm> {
152 name: T::String,
154 #[cfg_attr(
156 feature = "serde",
157 serde(skip_serializing_if = "Vec::is_empty", default)
158 )]
159 fields: Vec<Field<T>>,
160 index: u8,
166 #[cfg_attr(
168 feature = "serde",
169 serde(skip_serializing_if = "Vec::is_empty", default)
170 )]
171 docs: Vec<T::String>,
172}
173
174impl IntoPortable for Variant {
175 type Output = Variant<PortableForm>;
176
177 fn into_portable(self, registry: &mut Registry) -> Self::Output {
178 Variant {
179 name: self.name.into_portable(registry),
180 fields: registry.map_into_portable(self.fields),
181 index: self.index,
182 docs: registry.map_into_portable(self.docs),
183 }
184 }
185}
186
187impl Variant {
188 pub(crate) fn new(
190 name: &'static str,
191 fields: Vec<Field<MetaForm>>,
192 index: u8,
193 docs: Vec<&'static str>,
194 ) -> Self {
195 Self {
196 name,
197 fields,
198 index,
199 docs,
200 }
201 }
202}
203
204impl<T> Variant<T>
205where
206 T: Form,
207{
208 pub fn name(&self) -> &T::String {
210 &self.name
211 }
212
213 pub fn fields(&self) -> &[Field<T>] {
215 &self.fields
216 }
217
218 pub fn index(&self) -> u8 {
220 self.index
221 }
222
223 pub fn docs(&self) -> &[T::String] {
225 &self.docs
226 }
227}