use derive_more::{Display, IsVariant};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Display, IsVariant)]
#[display("{}", self.as_str())]
#[cfg_attr(
feature = "quickcheck",
derive(::quickcheck_richderive::Arbitrary),
quickcheck(arbitrary = "crate::quickcheck_helpers::coded::bit_rate_mode")
)]
pub enum BitRateMode {
#[default]
Cbr,
Vbr,
Abr,
}
impl BitRateMode {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn as_str(&self) -> &'static str {
match self {
Self::Cbr => "cbr",
Self::Vbr => "vbr",
Self::Abr => "abr",
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn to_u32(&self) -> u32 {
match self {
Self::Cbr => 0,
Self::Vbr => 1,
Self::Abr => 2,
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn from_u32(v: u32) -> Self {
match v {
0 => Self::Cbr,
1 => Self::Vbr,
2 => Self::Abr,
_ => Self::Cbr,
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn try_from_u32(v: u32) -> Option<Self> {
match v {
0 => Some(Self::Cbr),
1 => Some(Self::Vbr),
2 => Some(Self::Abr),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, thiserror::Error)]
#[error("not a bit-rate-mode name")]
#[non_exhaustive]
pub struct ParseBitRateModeError;
impl core::str::FromStr for BitRateMode {
type Err = ParseBitRateModeError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut buf = [0u8; crate::parse::FOLD_CAP];
let folded = crate::parse::fold(s, &mut buf).unwrap_or(s.as_bytes());
Ok(match folded {
b"cbr" => Self::Cbr,
b"vbr" => Self::Vbr,
b"abr" => Self::Abr,
_ => return Err(ParseBitRateModeError),
})
}
}
#[cfg(test)]
mod tests;