macro_rules! formats {
{
$(
format = $format:ident
name = $name:literal
$(short_name = $short_name:literal)?
media_type = $media_type:literal
extension = $extension:literal
kind = $kind:ident
)*
} => {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum FileFormat {
$(
#[doc=concat!($name, $(" (", $short_name, ")",)? ".")]
#[doc=concat!("- Media type: `", $media_type, "`")]
#[doc=concat!("- Extension: `.", $extension, "`")]
#[doc=concat!("- Kind: [", stringify!($kind), "](crate::Kind::", stringify!($kind), ")")]
$format,
)*
}
impl crate::FileFormat {
pub const fn name(&self) -> &str {
match self {
$(
Self::$format => $name,
)*
}
}
pub const fn short_name(&self) -> Option<&str> {
match self {
$(
$(Self::$format => Some($short_name),)?
)*
_ => None,
}
}
pub const fn media_type(&self) -> &str {
match self {
$(
Self::$format => $media_type,
)*
}
}
pub const fn extension(&self) -> &str {
match self {
$(
Self::$format => $extension,
)*
}
}
pub const fn kind(&self) -> crate::Kind {
match self {
$(
Self::$format => crate::Kind::$kind,
)*
}
}
}
};
}
macro_rules! signatures {
{
$(
format = $format:ident
$(value = $($value:literal $(offset = $offset:literal)?),+)+
)*
} => {
impl crate::FileFormat {
#[allow(clippy::int_plus_one)]
pub(crate) fn from_signature(bytes: &[u8]) -> Option<Self> {
$(
if $($(bytes.len() >= $($offset +)? $value.len()
&& &bytes[$($offset)?..$($offset +)? $value.len()] == $value)&&*)||* {
return Some(Self::$format);
}
)*
None
}
}
};
}