use crate::prelude::vec::Vec;
use crate::{
form::{Form, MetaForm, PortableForm},
Field, IntoPortable, Registry,
};
use derive_more::From;
use scale::Encode;
#[cfg(feature = "serde")]
use serde::{de::DeserializeOwned, Deserialize, Serialize};
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(
serialize = "T::Type: Serialize, T::String: Serialize",
deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned",
))
)]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[cfg_attr(any(feature = "std", feature = "decode"), derive(scale::Decode))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, From, Encode)]
pub struct TypeDefComposite<T: Form = MetaForm> {
#[cfg_attr(
feature = "serde",
serde(skip_serializing_if = "Vec::is_empty", default)
)]
pub fields: Vec<Field<T>>,
}
impl IntoPortable for TypeDefComposite {
type Output = TypeDefComposite<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
TypeDefComposite {
fields: registry.map_into_portable(self.fields),
}
}
}
impl<T> TypeDefComposite<T>
where
T: Form,
{
pub fn new<I>(fields: I) -> Self
where
I: IntoIterator<Item = Field<T>>,
{
Self {
fields: fields.into_iter().collect(),
}
}
}
impl<T> TypeDefComposite<T>
where
T: Form,
{
#[deprecated(
since = "2.5.0",
note = "Prefer to access the fields directly; this getter will be removed in the next major version"
)]
pub fn fields(&self) -> &[Field<T>] {
&self.fields
}
}