use crate::*;
pub struct SborFixedEnumVariant<const DISCRIMINATOR: u8, T> {
pub fields: T,
}
impl<const DISCRIMINATOR: u8, T> SborFixedEnumVariant<DISCRIMINATOR, T> {
pub fn new(fields: T) -> Self {
Self { fields }
}
pub fn discriminator() -> u8 {
DISCRIMINATOR
}
pub fn for_encoding(fields: &T) -> SborFixedEnumVariant<DISCRIMINATOR, &T> {
SborFixedEnumVariant { fields }
}
pub fn into_fields(self) -> T {
self.fields
}
}
impl<X: CustomValueKind, const DISCRIMINATOR: u8, T: SborTuple<X>> Categorize<X>
for SborFixedEnumVariant<DISCRIMINATOR, T>
{
fn value_kind() -> ValueKind<X> {
ValueKind::Enum
}
}
impl<X: CustomValueKind, const DISCRIMINATOR: u8, T: SborTuple<X>> SborEnum<X>
for SborFixedEnumVariant<DISCRIMINATOR, T>
{
fn get_length(&self) -> usize {
self.fields.get_length()
}
fn get_discriminator(&self) -> u8 {
DISCRIMINATOR
}
}
impl<
X: CustomValueKind,
E: Encoder<X>,
const DISCRIMINATOR: u8,
T: Encode<X, E> + SborTuple<X>,
> Encode<X, E> for SborFixedEnumVariant<DISCRIMINATOR, T>
{
fn encode_value_kind(&self, encoder: &mut E) -> Result<(), EncodeError> {
encoder.write_value_kind(Self::value_kind())
}
fn encode_body(&self, encoder: &mut E) -> Result<(), EncodeError> {
encoder.write_discriminator(DISCRIMINATOR)?;
self.fields.encode_body(encoder)
}
}
impl<
X: CustomValueKind,
D: Decoder<X>,
const DISCRIMINATOR: u8,
T: Decode<X, D> + SborTuple<X>,
> Decode<X, D> for SborFixedEnumVariant<DISCRIMINATOR, T>
{
#[inline]
fn decode_body_with_value_kind(
decoder: &mut D,
value_kind: ValueKind<X>,
) -> Result<Self, DecodeError> {
decoder.check_preloaded_value_kind(value_kind, Self::value_kind())?;
decoder.read_expected_discriminator(DISCRIMINATOR)?;
let fields = T::decode_body_with_value_kind(decoder, ValueKind::Tuple)?;
Ok(Self { fields })
}
}
pub trait IsSborFixedEnumVariant<F> {
const DISCRIMINATOR: u8;
fn new(fields: F) -> Self;
fn into_fields(self) -> F;
}
impl<const DISCRIMINATOR: u8, F> IsSborFixedEnumVariant<F>
for SborFixedEnumVariant<DISCRIMINATOR, F>
{
const DISCRIMINATOR: u8 = DISCRIMINATOR;
fn new(fields: F) -> Self {
Self::new(fields)
}
fn into_fields(self) -> F {
self.fields
}
}
pub trait SborEnumVariantFor<TEnum: SborEnum<X>, X: CustomValueKind> {
const DISCRIMINATOR: u8;
const IS_FLATTENED: bool;
type VariantFields: SborTuple<X>;
fn from_variant_fields(variant_fields: Self::VariantFields) -> Self;
type VariantFieldsRef<'a>: SborTuple<X>
where
Self: 'a,
Self::VariantFields: 'a;
fn as_variant_fields_ref(&self) -> Self::VariantFieldsRef<'_>;
type OwnedVariant: IsSborFixedEnumVariant<Self::VariantFields>;
type BorrowedVariant<'a>: IsSborFixedEnumVariant<Self::VariantFieldsRef<'a>>
where
Self: 'a,
Self::VariantFields: 'a;
fn as_encodable_variant<'a>(&'a self) -> Self::BorrowedVariant<'a> {
Self::BorrowedVariant::new(self.as_variant_fields_ref())
}
fn from_decoded_variant(variant: Self::OwnedVariant) -> Self
where
Self: core::marker::Sized,
{
Self::from_variant_fields(variant.into_fields())
}
fn into_enum(self) -> TEnum;
}