1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use mime::{Mime, IMAGE_GIF, IMAGE_JPEG, IMAGE_PNG};
use serde::{Deserialize, Serialize};
use std::str::FromStr;

/// Output format of the processed image.
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
pub enum Encoding {
    /// Output the media as JPEG.
    #[serde(rename = "jpeg")]
    #[serde(alias = "jpg")]
    Jpeg,

    /// Output the media as a PNG file.
    #[serde(rename = "png")]
    Png,

    /// Encode the media with WebP.
    #[serde(rename = "webp")]
    WebP,

    /// Encode the media as a GIF.
    #[serde(rename = "gif")]
    Gif,
}

impl Default for Encoding {
    fn default() -> Self {
        Encoding::Jpeg
    }
}

impl Encoding {
    /// Get the `Mime` type.
    pub fn mime_type(self) -> Mime {
        match self {
            Encoding::Jpeg => IMAGE_JPEG,
            Encoding::Png => IMAGE_PNG,
            Encoding::WebP => Mime::from_str("image/webp").unwrap(),
            Encoding::Gif => IMAGE_GIF,
        }
    }

    /// Returns `true` if the encoding is considered a video, for example a MP4 or GIF (even though GIFs are not neccessarily animated).
    pub fn is_video(self) -> bool {
        match self {
            Encoding::Gif => true,
            _ => false,
        }
    }
}