use derive_more::{Display, IsVariant};
#[cfg_attr(
feature = "quickcheck",
derive(::quickcheck_richderive::Arbitrary),
quickcheck(arbitrary = "crate::quickcheck_helpers::coded::channel_order")
)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Display, IsVariant)]
#[display("{}", self.as_str())]
#[repr(u32)]
pub enum ChannelOrder {
#[default]
Unspecified = 0,
Native = 1,
Custom = 2,
Ambisonic = 3,
}
impl ChannelOrder {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn as_str(&self) -> &'static str {
match self {
Self::Unspecified => "unspecified",
Self::Native => "native",
Self::Custom => "custom",
Self::Ambisonic => "ambisonic",
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn to_u32(&self) -> u32 {
*self as u32
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn from_u32(v: u32) -> Self {
match v {
1 => Self::Native,
2 => Self::Custom,
3 => Self::Ambisonic,
_ => Self::Unspecified,
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn try_from_u32(v: u32) -> Option<Self> {
match v {
0 => Some(Self::Unspecified),
1 => Some(Self::Native),
2 => Some(Self::Custom),
3 => Some(Self::Ambisonic),
_ => None,
}
}
}
roster!(
ChannelOrder,
"channel order",
[Unspecified, Native, Custom, Ambisonic]
);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, thiserror::Error)]
#[error("not a channel-order name")]
#[non_exhaustive]
pub struct ParseChannelOrderError;
impl core::str::FromStr for ChannelOrder {
type Err = ParseChannelOrderError;
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"unspecified" => Self::Unspecified,
b"native" => Self::Native,
b"custom" => Self::Custom,
b"ambisonic" => Self::Ambisonic,
_ => return Err(ParseChannelOrderError),
})
}
}
#[cfg(test)]
mod tests;