scale_info/ty/
composite.rs1use crate::prelude::vec::Vec;
16
17use crate::{
18 form::{Form, MetaForm, PortableForm},
19 Field, IntoPortable, Registry,
20};
21use derive_more::From;
22use scale::Encode;
23#[cfg(feature = "serde")]
24use serde::{de::DeserializeOwned, Deserialize, Serialize};
25
26#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
53#[cfg_attr(
54 feature = "serde",
55 serde(bound(
56 serialize = "T::Type: Serialize, T::String: Serialize",
57 deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned",
58 ))
59)]
60#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
61#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
62#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
63#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, From, Encode)]
64pub struct TypeDefComposite<T: Form = MetaForm> {
65 #[cfg_attr(
67 feature = "serde",
68 serde(skip_serializing_if = "Vec::is_empty", default)
69 )]
70 pub fields: Vec<Field<T>>,
71}
72
73impl IntoPortable for TypeDefComposite {
74 type Output = TypeDefComposite<PortableForm>;
75
76 fn into_portable(self, registry: &mut Registry) -> Self::Output {
77 TypeDefComposite {
78 fields: registry.map_into_portable(self.fields),
79 }
80 }
81}
82
83impl<T> TypeDefComposite<T>
84where
85 T: Form,
86{
87 pub fn new<I>(fields: I) -> Self
89 where
90 I: IntoIterator<Item = Field<T>>,
91 {
92 Self {
93 fields: fields.into_iter().collect(),
94 }
95 }
96}
97
98impl<T> TypeDefComposite<T>
99where
100 T: Form,
101{
102 #[deprecated(
104 since = "2.5.0",
105 note = "Prefer to access the fields directly; this getter will be removed in the next major version"
106 )]
107 pub fn fields(&self) -> &[Field<T>] {
108 &self.fields
109 }
110}