use musli::{Decode, Decoder};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum TypeTag {
Unknown = 0b0,
Fixed8 = 0b1000_0000,
Fixed8Next = 0b1111_1111,
OptionNone = 0b0111_1110,
OptionSome = 0b0111_1111,
Fixed16 = 0b0001_0010,
Fixed32 = 0b0001_0100,
Fixed64 = 0b0001_0110,
Fixed128 = 0b0001_1000,
Continuation = 0b0001_1010,
Prefixed = 0b0010_0000,
Sequence = 0b0010_0010,
Pair = 0b0100_0000,
PairSequence = 0b0010_0100,
}
impl TypeTag {
pub(crate) const FIXED8_BYTE: u8 = TypeTag::Fixed8 as u8;
pub(crate) const FIXED16_BYTE: u8 = TypeTag::Fixed16 as u8;
pub(crate) const FIXED32_BYTE: u8 = TypeTag::Fixed32 as u8;
pub(crate) const FIXED64_BYTE: u8 = TypeTag::Fixed64 as u8;
pub(crate) const FIXED128_BYTE: u8 = TypeTag::Fixed128 as u8;
pub(crate) const CONTINUATION_BYTE: u8 = TypeTag::Continuation as u8;
pub(crate) const PREFIXED_BYTE: u8 = TypeTag::Prefixed as u8;
pub(crate) const SEQUENCE_BYTE: u8 = TypeTag::Sequence as u8;
pub(crate) const PAIR_BYTE: u8 = TypeTag::Pair as u8;
pub(crate) const PAIR_SEQUENCE_BYTE: u8 = TypeTag::PairSequence as u8;
pub(crate) const OPTION_SOME_BYTE: u8 = TypeTag::OptionSome as u8;
pub(crate) const OPTION_NONE_BYTE: u8 = TypeTag::OptionNone as u8;
}
impl<'de> Decode<'de> for TypeTag {
fn decode<D>(decoder: D) -> Result<Self, D::Error>
where
D: Decoder<'de>,
{
Ok(match decoder.decode_u8()? {
Self::FIXED8_BYTE => Self::Fixed8,
Self::FIXED16_BYTE => Self::Fixed16,
Self::FIXED32_BYTE => Self::Fixed32,
Self::FIXED64_BYTE => Self::Fixed64,
Self::FIXED128_BYTE => Self::Fixed128,
Self::CONTINUATION_BYTE => Self::Continuation,
Self::PREFIXED_BYTE => Self::Prefixed,
Self::SEQUENCE_BYTE => Self::Sequence,
Self::PAIR_BYTE => Self::Pair,
Self::PAIR_SEQUENCE_BYTE => Self::PairSequence,
Self::OPTION_SOME_BYTE => Self::OptionSome,
Self::OPTION_NONE_BYTE => Self::OptionNone,
_ => Self::Unknown,
})
}
}