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
51
52
53
54
55
56
57
58
59
60
use std::str::FromStr;

use super::error::PostError;

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Extension {
    JPG,
    PNG,
    WEBP,
    GIF,
    WEBM,
    MP4,
    Ugoira,
    Unknown,
}

impl Extension {
    pub fn guess_format(s: &str) -> Self {
        let uu = Extension::from_str(s);
        if uu.is_err() {
            return Self::Unknown;
        }
        uu.unwrap()
    }
}

impl FromStr for Extension {
    type Err = PostError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "jpg" | "jpeg" | "jfif" => Ok(Self::JPG),
            "png" | "apng" => Ok(Self::PNG),
            "webp" => Ok(Self::WEBP),
            "webm" => Ok(Self::WEBM),
            "mp4" => Ok(Self::MP4),
            "gif" => Ok(Self::GIF),
            "zip" => Ok(Self::Ugoira),
            _ => Err(PostError::UnknownExtension {
                message: s.to_string(),
            }),
        }
    }
}

impl ToString for Extension {
    fn to_string(&self) -> String {
        match self {
            Extension::JPG => String::from("jpg"),
            Extension::PNG => String::from("png"),
            Extension::WEBP => String::from("webp"),
            Extension::GIF => String::from("gif"),
            Extension::WEBM => String::from("webm"),
            Extension::MP4 => String::from("mp4"),
            Extension::Ugoira => String::from("zip"),
            Extension::Unknown => String::from("bin"),
        }
    }
}