use std::{
error::Error,
fmt::{Display, Formatter}
};
use strum_macros::FromRepr;
pub(crate) mod optimizer;
pub(crate) mod codebook;
macro_rules! try_from_impl {
{ type Enum = $enum_type:ident($repr_type:ty); type Error = $error_type:ident } => {
#[doc = "The error type for fallible conversions from integers to a `"]
#[doc = stringify!($enum_type)]
#[doc = "`."]
#[derive(Debug)]
#[repr(transparent)]
pub struct $error_type($repr_type);
impl Display for $error_type {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl Error for $error_type {}
impl TryFrom<$repr_type> for $enum_type {
type Error = $error_type;
fn try_from(value: $repr_type) -> Result<Self, Self::Error> {
$enum_type::from_repr(value).ok_or($error_type(value))
}
}
impl $error_type {
pub const fn integer(&self) -> $repr_type {
self.0
}
}
}
}
#[derive(Debug, Eq, PartialEq, Clone, Copy, FromRepr)]
#[repr(u8)]
pub enum PacketType {
Audio = 0,
IdentificationHeader = 1,
CommentHeader = 3,
SetupHeader = 5
}
impl Display for PacketType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::Audio => "audio packet",
Self::IdentificationHeader => "identification header packet",
Self::CommentHeader => "comment header packet",
Self::SetupHeader => "setup header packet"
})
}
}
try_from_impl! {
type Enum = PacketType(u8);
type Error = TryPacketTypeFromInt
}
#[derive(Debug, Eq, PartialEq, Clone, Copy, FromRepr)]
#[repr(u8)]
enum VectorLookupType {
NoLookup = 0,
ImplicitlyPopulated = 1,
ExplicitlyPopulated = 2
}
try_from_impl! {
type Enum = VectorLookupType(u8);
type Error = TryVectorLookupTypeFromInt
}
#[derive(Debug, Eq, PartialEq, Clone, Copy, FromRepr)]
#[repr(u16)]
enum ResidueType {
Interleaved = 0,
Ordered = 1,
InterleavedVectors = 2
}
try_from_impl! {
type Enum = ResidueType(u16);
type Error = TryResidueTypeFromInt
}