wonfy_tools/util/image/
encode_format.rs

1use image::ImageFormat;
2#[cfg(target_arch = "wasm32")]
3use wasm_bindgen::prelude::wasm_bindgen;
4
5#[derive(Debug, Clone, Copy)]
6#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
7pub enum EncodeFormat {
8    Png,
9    Jpeg,
10    Gif,
11    WebP,
12}
13
14impl EncodeFormat {
15    pub fn content_type(&self) -> &str {
16        use EncodeFormat::*;
17
18        match self {
19            Png => "image/png",
20            Jpeg => "image/jpeg",
21            Gif => "image/gif",
22            WebP => "image/webp",
23        }
24    }
25
26    pub fn file_extension(&self) -> &str {
27        use EncodeFormat::*;
28
29        match self {
30            Png => "png",
31            Jpeg => "jpeg",
32            Gif => "gif",
33            WebP => "webp",
34        }
35    }
36}
37
38#[cfg(target_arch = "wasm32")]
39#[wasm_bindgen]
40pub fn encode_format_content_type(format: EncodeFormat) -> String {
41    format.content_type().into()
42}
43
44#[cfg(target_arch = "wasm32")]
45#[wasm_bindgen]
46pub fn encode_format_file_extension(format: EncodeFormat) -> String {
47    format.file_extension().into()
48}
49
50impl Into<ImageFormat> for EncodeFormat {
51    fn into(self) -> ImageFormat {
52        use EncodeFormat::*;
53
54        match self {
55            Png => ImageFormat::Png,
56            Jpeg => ImageFormat::Jpeg,
57            Gif => ImageFormat::Gif,
58            WebP => ImageFormat::WebP,
59        }
60    }
61}