use core::str::FromStr;
use derive_more::{Display, IsVariant, TryUnwrap, Unwrap};
use smol_str::SmolStr;
#[cfg_attr(
feature = "quickcheck",
derive(::quickcheck_richderive::Arbitrary),
quickcheck(arbitrary = "crate::quickcheck_helpers::strings::container_format")
)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Display, IsVariant, Unwrap, TryUnwrap)]
#[display("{}", self.as_str())]
#[unwrap(ref, ref_mut)]
#[try_unwrap(ref, ref_mut)]
#[non_exhaustive]
pub enum Format {
Mov,
#[is_variant(ignore)]
Mp4,
Mkv,
Webm,
Avi,
Flv,
MpegTs,
Ogg,
Asf,
Rm,
Wmv,
Mxf,
Gxf,
Threegp,
Other(SmolStr),
}
impl Default for Format {
#[inline]
fn default() -> Self {
Self::Other(SmolStr::new_inline(""))
}
}
impl Format {
#[inline(always)]
pub const fn is_mp4(&self) -> bool {
matches!(self, Self::Mp4)
}
pub fn as_str(&self) -> &str {
match self {
Self::Mov => "mov",
Self::Mp4 => "mp4",
Self::Mkv => "mkv",
Self::Webm => "webm",
Self::Avi => "avi",
Self::Flv => "flv",
Self::MpegTs => "mpegts",
Self::Ogg => "ogg",
Self::Asf => "asf",
Self::Rm => "rm",
Self::Wmv => "wmv",
Self::Mxf => "mxf",
Self::Gxf => "gxf",
Self::Threegp => "3gp",
Self::Other(s) => s.as_str(),
}
}
#[inline(always)]
pub const fn as_extension(&self) -> &'static str {
match self {
Self::Mov => "mov",
Self::Mp4 => "mp4",
Self::Mkv => "mkv",
Self::Webm => "webm",
Self::Avi => "avi",
Self::Flv => "flv",
Self::MpegTs => "ts",
Self::Ogg => "ogv",
Self::Asf => "asf",
Self::Rm => "rm",
Self::Wmv => "wmv",
Self::Mxf => "mxf",
Self::Gxf => "gxf",
Self::Threegp => "3gp",
Self::Other(_) => "",
}
}
pub fn other(slug: impl AsRef<str>) -> Self {
Self::Other(crate::parse::fold_owned(slug.as_ref()))
}
}
roster!(
Format,
"container format",
[
Mov, Mp4, Mkv, Webm, Avi, Flv, MpegTs, Ogg, Asf, Rm, Wmv, Mxf, Gxf,
Threegp
],
escape: Other
);
impl FromStr for Format {
type Err = core::convert::Infallible;
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"mov" => Self::Mov,
b"mp4" => Self::Mp4,
b"mkv" => Self::Mkv,
b"webm" => Self::Webm,
b"avi" => Self::Avi,
b"flv" => Self::Flv,
b"mpegts" => Self::MpegTs,
b"ogg" => Self::Ogg,
b"asf" => Self::Asf,
b"rm" => Self::Rm,
b"wmv" => Self::Wmv,
b"mxf" => Self::Mxf,
b"gxf" => Self::Gxf,
b"3gp" => Self::Threegp,
_ => Self::other(s),
})
}
}
#[cfg(test)]
mod tests;