use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SourceFormat {
Zpl,
Epl,
Tspl,
Dpl,
Xml,
Json,
Pdf,
Png,
Bmp,
Gif,
Jpeg,
Jpg,
Url,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum TargetFormat {
Zpl,
Epl,
Tspl,
Dpl,
Xml,
Json,
Pdf,
Png,
Bmp,
Gif,
Jpeg,
}
impl SourceFormat {
pub const ALL: &'static [SourceFormat] = &[
Self::Zpl,
Self::Epl,
Self::Tspl,
Self::Dpl,
Self::Xml,
Self::Json,
Self::Pdf,
Self::Png,
Self::Bmp,
Self::Gif,
Self::Jpeg,
Self::Jpg,
Self::Url,
];
pub fn wire_token(self) -> &'static str {
match self {
Self::Jpg => "jpeg",
other => other.name(),
}
}
pub fn media_type(self) -> &'static str {
match self {
Self::Zpl | Self::Epl | Self::Tspl | Self::Dpl | Self::Url => "text/plain",
Self::Xml => "application/xml",
Self::Json => "application/json",
Self::Pdf => "application/pdf",
Self::Png => "image/png",
Self::Bmp => "image/bmp",
Self::Gif => "image/gif",
Self::Jpeg | Self::Jpg => "image/jpeg",
}
}
pub fn name(self) -> &'static str {
match self {
Self::Zpl => "zpl",
Self::Epl => "epl",
Self::Tspl => "tspl",
Self::Dpl => "dpl",
Self::Xml => "xml",
Self::Json => "json",
Self::Pdf => "pdf",
Self::Png => "png",
Self::Bmp => "bmp",
Self::Gif => "gif",
Self::Jpeg => "jpeg",
Self::Jpg => "jpg",
Self::Url => "url",
}
}
pub fn from_name(name: &str) -> Option<Self> {
Self::ALL
.iter()
.copied()
.find(|format| format.name() == name.to_ascii_lowercase())
}
}
impl TargetFormat {
pub const ALL: &'static [TargetFormat] = &[
Self::Zpl,
Self::Epl,
Self::Tspl,
Self::Dpl,
Self::Xml,
Self::Json,
Self::Pdf,
Self::Png,
Self::Bmp,
Self::Gif,
Self::Jpeg,
];
pub fn wire_token(self) -> &'static str {
match self {
Self::Zpl => "zpl",
Self::Epl => "epl",
Self::Tspl => "tspl",
Self::Dpl => "dpl",
Self::Xml => "xml",
Self::Json => "json",
Self::Pdf => "pdf",
Self::Png => "png",
Self::Bmp => "bmp",
Self::Gif => "gif",
Self::Jpeg => "jpeg",
}
}
pub fn from_name(name: &str) -> Option<Self> {
Self::ALL
.iter()
.copied()
.find(|format| format.wire_token() == name.to_ascii_lowercase())
}
}
impl fmt::Display for SourceFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
impl fmt::Display for TargetFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.wire_token())
}
}