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 TypeDefVariant<T: Form = MetaForm> {
#[cfg_attr(
feature = "serde",
serde(skip_serializing_if = "Vec::is_empty", default)
)]
pub variants: Vec<Variant<T>>,
}
impl IntoPortable for TypeDefVariant {
type Output = TypeDefVariant<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
TypeDefVariant {
variants: registry.map_into_portable(self.variants),
}
}
}
impl<T> TypeDefVariant<T>
where
T: Form,
{
pub fn new<I>(variants: I) -> Self
where
I: IntoIterator<Item = Variant<T>>,
{
Self {
variants: variants.into_iter().collect(),
}
}
}
impl<T> TypeDefVariant<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 variants(&self) -> &[Variant<T>] {
&self.variants
}
}
#[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(any(feature = "std", feature = "decode"), derive(scale::Decode))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Encode)]
pub struct Variant<T: Form = MetaForm> {
pub name: T::String,
#[cfg_attr(
feature = "serde",
serde(skip_serializing_if = "Vec::is_empty", default)
)]
pub fields: Vec<Field<T>>,
pub index: u8,
#[cfg_attr(
feature = "serde",
serde(skip_serializing_if = "Vec::is_empty", default)
)]
pub docs: Vec<T::String>,
}
impl IntoPortable for Variant {
type Output = Variant<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
Variant {
name: self.name.into(),
fields: registry.map_into_portable(self.fields),
index: self.index,
docs: self.docs.into_iter().map(Into::into).collect(),
}
}
}
impl<T> Variant<T>
where
T: Form,
{
pub fn new(name: T::String, fields: Vec<Field<T>>, index: u8, docs: Vec<T::String>) -> Self {
Self {
name,
fields,
index,
docs,
}
}
}
impl<T> Variant<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 name(&self) -> &T::String {
&self.name
}
#[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
}
#[deprecated(
since = "2.5.0",
note = "Prefer to access the fields directly; this getter will be removed in the next major version"
)]
pub fn index(&self) -> u8 {
self.index
}
#[deprecated(
since = "2.5.0",
note = "Prefer to access the fields directly; this getter will be removed in the next major version"
)]
pub fn docs(&self) -> &[T::String] {
&self.docs
}
}