goud_engine/assets/loaders/texture/
format.rs1use image::ImageFormat;
4use std::fmt;
5
6#[repr(u8)]
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
12pub enum TextureFormat {
13 #[default]
15 Png = 0,
16 Jpeg = 1,
18 Bmp = 2,
20 Tga = 3,
22 Gif = 4,
24 WebP = 5,
26 Ico = 6,
28 Tiff = 7,
30 Unknown = 255,
32}
33
34impl TextureFormat {
35 pub const fn extension(&self) -> &'static str {
37 match self {
38 Self::Png => "png",
39 Self::Jpeg => "jpg",
40 Self::Bmp => "bmp",
41 Self::Tga => "tga",
42 Self::Gif => "gif",
43 Self::WebP => "webp",
44 Self::Ico => "ico",
45 Self::Tiff => "tiff",
46 Self::Unknown => "",
47 }
48 }
49
50 pub const fn name(&self) -> &'static str {
52 match self {
53 Self::Png => "PNG",
54 Self::Jpeg => "JPEG",
55 Self::Bmp => "BMP",
56 Self::Tga => "TGA",
57 Self::Gif => "GIF",
58 Self::WebP => "WebP",
59 Self::Ico => "ICO",
60 Self::Tiff => "TIFF",
61 Self::Unknown => "Unknown",
62 }
63 }
64
65 pub fn from_extension(ext: &str) -> Self {
67 match ext.to_lowercase().as_str() {
68 "png" => Self::Png,
69 "jpg" | "jpeg" => Self::Jpeg,
70 "bmp" => Self::Bmp,
71 "tga" => Self::Tga,
72 "gif" => Self::Gif,
73 "webp" => Self::WebP,
74 "ico" => Self::Ico,
75 "tif" | "tiff" => Self::Tiff,
76 _ => Self::Unknown,
77 }
78 }
79
80 pub(super) fn to_image_format(self) -> Option<ImageFormat> {
82 match self {
83 Self::Png => Some(ImageFormat::Png),
84 Self::Jpeg => Some(ImageFormat::Jpeg),
85 Self::Bmp => Some(ImageFormat::Bmp),
86 Self::Tga => Some(ImageFormat::Tga),
87 Self::Gif => Some(ImageFormat::Gif),
88 Self::WebP => Some(ImageFormat::WebP),
89 Self::Ico => Some(ImageFormat::Ico),
90 Self::Tiff => Some(ImageFormat::Tiff),
91 Self::Unknown => None,
92 }
93 }
94}
95
96impl fmt::Display for TextureFormat {
97 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98 write!(f, "{}", self.name())
99 }
100}