mime_type/
font.rs

1use crate::MimeFormat;
2use std::fmt::{self, Display, Formatter};
3
4/// Font file formats.
5///
6/// Supports common web and desktop font formats.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum Font {
9    /// TrueType Font format
10    Ttf,
11    /// OpenType Font format
12    Otf,
13    /// Web Open Font Format
14    Woff,
15    /// Web Open Font Format 2
16    Woff2,
17}
18
19impl Display for Font {
20    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
21        let mime_str = match self {
22            Font::Ttf => "application/font-sfnt",
23            Font::Otf => "application/font-sfnt",
24            Font::Woff => "application/font-woff",
25            Font::Woff2 => "application/font-woff",
26        };
27        write!(f, "{}", mime_str)
28    }
29}
30
31impl MimeFormat for Font {
32    fn from_ext(ext: &str) -> Option<crate::MimeType> {
33        match ext {
34            "ttf" => Some(crate::MimeType::Font(Font::Ttf)),
35            "otf" => Some(crate::MimeType::Font(Font::Otf)),
36            "woff" => Some(crate::MimeType::Font(Font::Woff)),
37            "woff2" => Some(crate::MimeType::Font(Font::Woff2)),
38            _ => None,
39        }
40    }
41
42    fn from_mime(mime: &str) -> Option<crate::MimeType> {
43        match mime {
44            "application/font-sfnt" => Some(crate::MimeType::Font(Font::Ttf)),
45            "application/font-woff" => Some(crate::MimeType::Font(Font::Woff)),
46            _ => None,
47        }
48    }
49}