2023-07-13 03:12:21 +00:00
|
|
|
mod animation;
|
|
|
|
mod image;
|
|
|
|
pub(crate) mod mimes;
|
|
|
|
mod video;
|
2023-07-12 04:11:23 +00:00
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
use std::str::FromStr;
|
2023-07-12 04:11:23 +00:00
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
pub(crate) use animation::{AnimationFormat, AnimationInput, AnimationOutput};
|
|
|
|
pub(crate) use image::{ImageFormat, ImageInput, ImageOutput};
|
|
|
|
pub(crate) use video::{InternalVideoFormat, OutputVideoFormat, VideoFormat};
|
2023-07-12 04:11:23 +00:00
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub(crate) struct PrescribedFormats {
|
|
|
|
pub(crate) image: Option<ImageFormat>,
|
|
|
|
pub(crate) animation: Option<AnimationFormat>,
|
|
|
|
pub(crate) video: Option<OutputVideoFormat>,
|
|
|
|
pub(crate) allow_audio: bool,
|
2023-07-12 04:11:23 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub(crate) enum InputFile {
|
|
|
|
Image(ImageInput),
|
|
|
|
Animation(AnimationInput),
|
|
|
|
Video(VideoFormat),
|
2023-07-12 04:11:23 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub(crate) enum OutputFile {
|
|
|
|
Image(ImageOutput),
|
|
|
|
Animation(AnimationOutput),
|
|
|
|
Video(OutputVideoFormat),
|
2023-07-12 04:11:23 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
#[derive(Clone, Copy, Debug, serde::Serialize, serde::Deserialize)]
|
|
|
|
pub(crate) enum InternalFormat {
|
|
|
|
Image(ImageFormat),
|
|
|
|
Animation(AnimationFormat),
|
|
|
|
Video(InternalVideoFormat),
|
2023-07-12 04:11:23 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
#[derive(Clone, Copy, Debug, serde::Serialize, serde::Deserialize)]
|
|
|
|
pub(crate) enum ProcessableFormat {
|
|
|
|
Image(ImageFormat),
|
|
|
|
Animation(AnimationFormat),
|
2023-07-12 04:11:23 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
#[derive(
|
|
|
|
Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
|
|
|
|
)]
|
|
|
|
pub(crate) enum InputProcessableFormat {
|
2023-07-12 04:11:23 +00:00
|
|
|
#[serde(rename = "apng")]
|
|
|
|
Apng,
|
|
|
|
#[serde(rename = "avif")]
|
|
|
|
Avif,
|
|
|
|
#[serde(rename = "gif")]
|
|
|
|
Gif,
|
2023-07-13 03:12:21 +00:00
|
|
|
#[serde(rename = "jpeg")]
|
|
|
|
Jpeg,
|
|
|
|
#[serde(rename = "jxl")]
|
|
|
|
Jxl,
|
|
|
|
#[serde(rename = "png")]
|
|
|
|
Png,
|
2023-07-12 04:11:23 +00:00
|
|
|
#[serde(rename = "webp")]
|
|
|
|
Webp,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InputFile {
|
|
|
|
const fn file_extension(&self) -> &'static str {
|
|
|
|
match self {
|
2023-07-13 03:12:21 +00:00
|
|
|
Self::Image(ImageInput { format, .. }) => format.file_extension(),
|
|
|
|
Self::Animation(AnimationInput { format }) => format.file_extension(),
|
2023-07-12 04:11:23 +00:00
|
|
|
Self::Video(format) => format.file_extension(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn build_output(&self, prescribed: &PrescribedFormats) -> OutputFile {
|
|
|
|
match (self, prescribed) {
|
2023-07-13 03:12:21 +00:00
|
|
|
(InputFile::Image(input), PrescribedFormats { image, .. }) => {
|
|
|
|
OutputFile::Image(input.build_output(*image))
|
|
|
|
}
|
|
|
|
(InputFile::Animation(input), PrescribedFormats { animation, .. }) => {
|
|
|
|
OutputFile::Animation(input.build_output(*animation))
|
|
|
|
}
|
2023-07-12 04:11:23 +00:00
|
|
|
(
|
|
|
|
InputFile::Video(input),
|
|
|
|
PrescribedFormats {
|
2023-07-13 03:12:21 +00:00
|
|
|
video, allow_audio, ..
|
2023-07-12 04:11:23 +00:00
|
|
|
},
|
2023-07-13 03:12:21 +00:00
|
|
|
) => OutputFile::Video(input.build_output(*video, *allow_audio)),
|
2023-07-12 04:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
pub(crate) const fn internal_format(&self) -> InternalFormat {
|
2023-07-12 04:11:23 +00:00
|
|
|
match self {
|
2023-07-13 03:12:21 +00:00
|
|
|
Self::Image(ImageInput { format, .. }) => InternalFormat::Image(*format),
|
|
|
|
Self::Animation(AnimationInput { format }) => InternalFormat::Animation(*format),
|
|
|
|
Self::Video(format) => InternalFormat::Video(format.internal_format()),
|
2023-07-12 04:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
impl OutputFile {
|
|
|
|
const fn file_extension(&self) -> &'static str {
|
2023-07-12 04:11:23 +00:00
|
|
|
match self {
|
2023-07-13 03:12:21 +00:00
|
|
|
Self::Image(ImageOutput { format, .. }) => format.file_extension(),
|
|
|
|
Self::Animation(AnimationOutput { format, .. }) => format.file_extension(),
|
|
|
|
Self::Video(format) => format.file_extension(),
|
2023-07-12 04:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
pub(crate) const fn internal_format(&self) -> InternalFormat {
|
2023-07-12 04:11:23 +00:00
|
|
|
match self {
|
2023-07-13 03:12:21 +00:00
|
|
|
Self::Image(ImageOutput { format, .. }) => InternalFormat::Image(*format),
|
|
|
|
Self::Animation(AnimationOutput { format, .. }) => InternalFormat::Animation(*format),
|
|
|
|
Self::Video(format) => InternalFormat::Video(format.internal_format()),
|
2023-07-12 04:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
impl InternalFormat {
|
|
|
|
pub(crate) fn media_type(self) -> mime::Mime {
|
2023-07-12 04:11:23 +00:00
|
|
|
match self {
|
2023-07-13 03:12:21 +00:00
|
|
|
Self::Image(format) => format.media_type(),
|
|
|
|
Self::Animation(format) => format.media_type(),
|
|
|
|
Self::Video(format) => format.media_type(),
|
2023-07-12 04:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
pub(crate) const fn file_extension(self) -> &'static str {
|
2023-07-12 04:11:23 +00:00
|
|
|
match self {
|
2023-07-13 03:12:21 +00:00
|
|
|
Self::Image(format) => format.file_extension(),
|
|
|
|
Self::Animation(format) => format.file_extension(),
|
|
|
|
Self::Video(format) => format.file_extension(),
|
2023-07-12 04:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
pub(crate) const fn processable_format(self) -> Option<ProcessableFormat> {
|
2023-07-12 04:11:23 +00:00
|
|
|
match self {
|
2023-07-13 03:12:21 +00:00
|
|
|
Self::Image(format) => Some(ProcessableFormat::Image(format)),
|
|
|
|
Self::Animation(format) => Some(ProcessableFormat::Animation(format)),
|
|
|
|
Self::Video(_) => None,
|
2023-07-12 04:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
impl ProcessableFormat {
|
|
|
|
pub(crate) const fn file_extension(self) -> &'static str {
|
2023-07-12 04:11:23 +00:00
|
|
|
match self {
|
2023-07-13 03:12:21 +00:00
|
|
|
Self::Image(format) => format.file_extension(),
|
|
|
|
Self::Animation(format) => format.file_extension(),
|
2023-07-12 04:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
pub(crate) const fn coalesce(self) -> bool {
|
|
|
|
matches!(self, Self::Animation(_))
|
2023-07-12 04:11:23 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
pub(crate) fn magick_format(self) -> &'static str {
|
2023-07-12 04:11:23 +00:00
|
|
|
match self {
|
2023-07-13 03:12:21 +00:00
|
|
|
Self::Image(format) => format.magick_format(),
|
|
|
|
Self::Animation(format) => format.magick_format(),
|
2023-07-12 04:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
impl FromStr for InputProcessableFormat {
|
|
|
|
type Err = String;
|
2023-07-12 04:11:23 +00:00
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match s {
|
|
|
|
"apng" => Ok(Self::Apng),
|
|
|
|
"avif" => Ok(Self::Avif),
|
|
|
|
"gif" => Ok(Self::Gif),
|
|
|
|
"jpeg" => Ok(Self::Jpeg),
|
|
|
|
"jpg" => Ok(Self::Jpeg),
|
|
|
|
"jxl" => Ok(Self::Jxl),
|
|
|
|
"png" => Ok(Self::Png),
|
|
|
|
"webp" => Ok(Self::Webp),
|
|
|
|
otherwise => Err(format!("Invalid format: {otherwise}")),
|
2023-07-12 04:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 03:12:21 +00:00
|
|
|
impl std::fmt::Display for InputProcessableFormat {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2023-07-12 04:11:23 +00:00
|
|
|
match self {
|
2023-07-13 03:12:21 +00:00
|
|
|
Self::Apng => write!(f, "apng"),
|
|
|
|
Self::Avif => write!(f, "avif"),
|
|
|
|
Self::Gif => write!(f, "gif"),
|
|
|
|
Self::Jpeg => write!(f, "jpeg"),
|
|
|
|
Self::Jxl => write!(f, "jxl"),
|
|
|
|
Self::Png => write!(f, "png"),
|
|
|
|
Self::Webp => write!(f, "webp"),
|
2023-07-12 04:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|