ragit_pdl/
image.rs

1use crate::error::Error;
2use regex::Regex;
3
4#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
5pub enum ImageType {
6    Jpeg,
7    Png,
8    Gif,
9    Webp,
10    Svg,
11}
12
13impl ImageType {
14    // for anthropic api
15    pub fn get_media_type(&self) -> &str {
16        match self {
17            ImageType::Jpeg => "image/jpeg",
18            ImageType::Png => "image/png",
19            ImageType::Gif => "image/gif",
20            ImageType::Webp => "image/webp",
21
22            // I'm not sure whether it'd work with anthropic api
23            ImageType::Svg => "image/svg+xml",
24        }
25    }
26
27    pub fn from_media_type(s: &str) -> Result<Self, Error> {
28        match s.to_ascii_lowercase() {
29            s if s == "image/jpeg" || s == "image/jpg" => Ok(ImageType::Jpeg),
30            s if s == "image/png" => Ok(ImageType::Png),
31            s if s == "image/gif" => Ok(ImageType::Gif),
32            s if s == "image/webp" => Ok(ImageType::Webp),
33            s if s == "image/svg+xml" => Ok(ImageType::Svg),
34            _ => Err(Error::InvalidImageType(s.to_string())),
35        }
36    }
37
38    pub fn from_extension(ext: &str) -> Result<Self, Error> {
39        match ext.to_ascii_lowercase() {
40            ext if ext == "png" => Ok(ImageType::Png),
41            ext if ext == "jpeg" || ext == "jpg" => Ok(ImageType::Jpeg),
42            ext if ext == "gif" => Ok(ImageType::Gif),
43            ext if ext == "webp" => Ok(ImageType::Webp),
44            ext if ext == "svg" => Ok(ImageType::Svg),
45            _ => Err(Error::InvalidImageType(ext.to_string())),
46        }
47    }
48
49    pub fn infer_from_path(path: &str) -> Result<Self, Error> {
50        let ext_re = Regex::new(r".+\.([^.]+)$").unwrap();
51
52        if let Some(ext) = ext_re.captures(path) {
53            ImageType::from_extension(ext.get(1).unwrap().as_str())
54        }
55
56        else {
57            Err(Error::InvalidImageType(path.to_string()))
58        }
59    }
60
61    pub fn to_extension(&self) -> &str {
62        match self {
63            ImageType::Jpeg => "jpg",
64            ImageType::Png => "png",
65            ImageType::Gif => "gif",
66            ImageType::Webp => "webp",
67            ImageType::Svg => "svg",
68        }
69    }
70}
71
72impl TryFrom<ImageType> for image::ImageFormat {
73    type Error = Error;
74
75    fn try_from(image_type: ImageType) -> Result<Self, Error> {
76        match image_type {
77            ImageType::Jpeg => Ok(image::ImageFormat::Jpeg),
78            ImageType::Png => Ok(image::ImageFormat::Png),
79            ImageType::Gif => Ok(image::ImageFormat::Gif),
80            ImageType::Webp => Ok(image::ImageFormat::WebP),
81            ImageType::Svg => Err(Error::InvalidImageType(image_type.to_extension().to_string())),
82        }
83    }
84}
85
86impl TryFrom<image::ImageFormat> for ImageType {
87    type Error = Error;
88
89    fn try_from(image_format: image::ImageFormat) -> Result<Self, Error> {
90        match image_format {
91            image::ImageFormat::Jpeg => Ok(ImageType::Jpeg),
92            image::ImageFormat::Png => Ok(ImageType::Png),
93            image::ImageFormat::Gif => Ok(ImageType::Gif),
94            image::ImageFormat::WebP => Ok(ImageType::Webp),
95            f => Err(Error::InvalidImageType(format!("{f:?}"))),
96        }
97    }
98}