use crate::config::FormatFromStrError;
use std::fmt::{self, Display, Formatter};
use std::str::FromStr;
#[non_exhaustive]
#[derive(Copy, Debug)]
#[derive_const(Clone, Default, Eq, PartialEq)]
pub enum Format {
Png,
#[default]
Raw,
Wave,
Webp,
}
impl Format {
#[inline]
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Png => "png",
Self::Raw => "raw",
Self::Wave => "wave",
Self::Webp => "webp",
}
}
#[inline]
#[must_use]
pub const fn file_extension(self) -> &'static str {
match self {
Self::Png => ".png",
Self::Raw => ".zlk",
Self::Wave => ".wave",
Self::Webp => ".webp",
}
}
}
impl Display for Format {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
const impl FromStr for Format {
type Err = FormatFromStrError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.eq_ignore_ascii_case("png") {
return Ok(Self::Png);
}
if s.eq_ignore_ascii_case("raw") {
return Ok(Self::Raw);
}
if s.eq_ignore_ascii_case("wave") {
return Ok(Self::Wave);
}
if s.eq_ignore_ascii_case("webp") {
return Ok(Self::Webp);
}
Err(FormatFromStrError(()))
}
}