fetsig/interface/
mediatype.rs

1use core::fmt::Display;
2
3use serde::{Deserialize, Deserializer, Serialize};
4use smol_str::SmolStr;
5#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
6pub enum MediaType {
7    #[default]
8    ByteStream,
9    Cbor,
10    Css,
11    Form,
12    FormMultipart,
13    Html,
14    Ico,
15    Javascript,
16    Jpeg,
17    Json,
18    Pdf,
19    Plain,
20    Png,
21    Postcard,
22    Pwg,
23    Sse,
24    Svg,
25    Urf,
26    Wasm,
27    Xml,
28    Xlsx,
29    Zip,
30    Zip7,
31}
32
33const BYTE_STREAM: &str = "application/octet-stream";
34const CBOR: &str = "application/cbor";
35const CSS: &str = "text/css";
36const FORM: &str = "application/x-www-form-urlencoded";
37const MULTIPART_FORM: &str = "multipart/form-data";
38const HTML: &str = "text/html";
39const ICO: &str = "image/x-icon";
40const JAVASCRIPT: &str = "application/javascript";
41const JPEG: &str = "image/jpeg";
42const JSON: &str = "application/json";
43const PDF: &str = "application/pdf";
44const PLAIN: &str = "text/plain";
45const PNG: &str = "image/png";
46const POSTCARD: &str = "application/x-postcard";
47const PWG: &str = "image/pwg-raster";
48const SSE: &str = "text/event-stream";
49const SVG: &str = "image/svg+xml";
50const URF: &str = "image/urf";
51const WASM: &str = "application/wasm";
52const XLSX: &str = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
53const XML: &str = "application/xml";
54const ZIP: &str = "application/zip";
55const ZIP_WIN: &str = "application/x-zip-compressed";
56const ZIP_7: &str = "application/x-7z-compressed";
57
58impl MediaType {
59    pub fn as_str(&self) -> &str {
60        self.as_ref()
61    }
62}
63
64impl Display for MediaType {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        f.write_str(self.as_ref())
67    }
68}
69
70impl From<&str> for MediaType {
71    fn from(mime: &str) -> Self {
72        match mime {
73            BYTE_STREAM => Self::ByteStream,
74            CBOR => Self::Cbor,
75            CSS => Self::Css,
76            FORM => Self::Form,
77            MULTIPART_FORM => Self::FormMultipart,
78            HTML => Self::Html,
79            ICO => Self::Ico,
80            JAVASCRIPT => Self::Javascript,
81            JPEG => Self::Jpeg,
82            JSON => Self::Json,
83            PDF => Self::Pdf,
84            PNG => Self::Png,
85            POSTCARD => Self::Postcard,
86            PWG => Self::Pwg,
87            SSE => Self::Sse,
88            SVG => Self::Svg,
89            URF => Self::Urf,
90            WASM => Self::Wasm,
91            XML => Self::Xml,
92            XLSX => Self::Xlsx,
93            ZIP => Self::Zip,
94            ZIP_WIN => Self::Zip,
95            ZIP_7 => Self::Zip7,
96            _ => Self::default(),
97        }
98    }
99}
100
101impl From<SmolStr> for MediaType {
102    fn from(mime: SmolStr) -> Self {
103        Self::from(mime.as_str())
104    }
105}
106
107impl AsRef<str> for MediaType {
108    fn as_ref(&self) -> &str {
109        match self {
110            MediaType::ByteStream => BYTE_STREAM,
111            MediaType::Cbor => CBOR,
112            MediaType::Css => CSS,
113            MediaType::Form => FORM,
114            MediaType::FormMultipart => MULTIPART_FORM,
115            MediaType::Html => HTML,
116            MediaType::Ico => ICO,
117            MediaType::Javascript => JAVASCRIPT,
118            MediaType::Jpeg => JPEG,
119            MediaType::Json => JSON,
120            MediaType::Pdf => PDF,
121            MediaType::Plain => PLAIN,
122            MediaType::Png => PNG,
123            MediaType::Postcard => POSTCARD,
124            MediaType::Pwg => PWG,
125            MediaType::Sse => SSE,
126            MediaType::Svg => SVG,
127            MediaType::Urf => URF,
128            MediaType::Wasm => WASM,
129            MediaType::Xml => XML,
130            MediaType::Xlsx => XLSX,
131            MediaType::Zip => ZIP,
132            MediaType::Zip7 => ZIP_7,
133        }
134    }
135}
136
137impl Serialize for MediaType {
138    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
139    where
140        S: serde::Serializer,
141    {
142        serializer.serialize_str(self.as_ref())
143    }
144}
145
146impl<'de> Deserialize<'de> for MediaType {
147    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
148    where
149        D: Deserializer<'de>,
150    {
151        let str = <&'de str as Deserialize>::deserialize(deserializer)?;
152        Ok(str.into())
153    }
154}