Skip to main content

cotis_defaults/
generic_image.rs

1#[derive(Clone)]
2enum CotisImagePath {
3    StringPath(String),
4    StaticPath(&'static str),
5}
6
7/// Error returned when constructing path-based image wrappers.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum CotisImageError {
10    /// The path was absolute; Cotis image paths must be relative.
11    NotRelativePath,
12    /// The provided path was empty.
13    EmptyPath,
14}
15
16/// JPEG image path wrapper.
17#[derive(Clone)]
18pub struct JPEGImage {
19    path: CotisImagePath,
20}
21
22impl JPEGImage {
23    /// Creates a JPEG image from a relative path.
24    ///
25    /// # Errors
26    ///
27    /// Returns:
28    /// - [`CotisImageError::EmptyPath`] when `path` is empty.
29    /// - [`CotisImageError::NotRelativePath`] when `path` starts with `/`.
30    pub fn new(path: &str) -> Result<Self, CotisImageError> {
31        if path.is_empty() {
32            return Err(CotisImageError::EmptyPath);
33        }
34        if path.starts_with("/") {
35            return Err(CotisImageError::NotRelativePath);
36        }
37        Ok(Self {
38            path: CotisImagePath::StringPath(path.to_string()),
39        })
40    }
41
42    /// Creates a JPEG image from a relative static path.
43    ///
44    /// # Panics
45    ///
46    /// Panics with `CotisImageError::*` text when the path is empty or absolute.
47    pub const fn new_const(path: &'static str) -> Self {
48        if path.is_empty() {
49            panic!("CotisImageError::EmptyPath");
50        }
51        if path.as_bytes()[0] == b'/' {
52            panic!("CotisImageError::NotRelativePath");
53        }
54
55        Self {
56            path: CotisImagePath::StaticPath(path),
57        }
58    }
59
60    /// Returns the original image path.
61    pub fn get_path(&self) -> &str {
62        match &self.path {
63            CotisImagePath::StringPath(s) => s,
64            CotisImagePath::StaticPath(s) => s,
65        }
66    }
67}
68
69/// SVG image path wrapper.
70#[derive(Clone)]
71pub struct SVGImage {
72    path: CotisImagePath,
73}
74
75impl SVGImage {
76    /// Creates an SVG image from a relative path.
77    ///
78    /// # Errors
79    ///
80    /// Returns:
81    /// - [`CotisImageError::EmptyPath`] when `path` is empty.
82    /// - [`CotisImageError::NotRelativePath`] when `path` starts with `/`.
83    pub fn new(path: &str) -> Result<Self, CotisImageError> {
84        if path.is_empty() {
85            return Err(CotisImageError::EmptyPath);
86        }
87        if path.starts_with("/") {
88            return Err(CotisImageError::NotRelativePath);
89        }
90        Ok(Self {
91            path: CotisImagePath::StringPath(path.to_string()),
92        })
93    }
94
95    /// Creates an SVG image from a relative static path.
96    ///
97    /// # Panics
98    ///
99    /// Panics with `CotisImageError::*` text when the path is empty or absolute.
100    pub const fn new_const(path: &'static str) -> Self {
101        if path.is_empty() {
102            panic!("CotisImageError::EmptyPath");
103        }
104        if path.as_bytes()[0] == b'/' {
105            panic!("CotisImageError::NotRelativePath");
106        }
107
108        Self {
109            path: CotisImagePath::StaticPath(path),
110        }
111    }
112
113    /// Returns the original image path.
114    pub fn get_path(&self) -> &str {
115        match &self.path {
116            CotisImagePath::StringPath(s) => s,
117            CotisImagePath::StaticPath(s) => s,
118        }
119    }
120}
121
122/// PNG image path wrapper.
123#[derive(Clone)]
124pub struct PNGImage {
125    path: CotisImagePath,
126}
127
128impl PNGImage {
129    /// Creates a PNG image from a relative path.
130    ///
131    /// # Errors
132    ///
133    /// Returns:
134    /// - [`CotisImageError::EmptyPath`] when `path` is empty.
135    /// - [`CotisImageError::NotRelativePath`] when `path` starts with `/`.
136    pub fn new(path: &str) -> Result<Self, CotisImageError> {
137        if path.is_empty() {
138            return Err(CotisImageError::EmptyPath);
139        }
140        if path.starts_with("/") {
141            return Err(CotisImageError::NotRelativePath);
142        }
143        Ok(Self {
144            path: CotisImagePath::StringPath(path.to_string()),
145        })
146    }
147
148    /// Creates a PNG image from a relative static path.
149    ///
150    /// # Panics
151    ///
152    /// Panics with `CotisImageError::*` text when the path is empty or absolute.
153    pub const fn new_const(path: &'static str) -> Self {
154        if path.is_empty() {
155            panic!("CotisImageError::EmptyPath");
156        }
157        if path.as_bytes()[0] == b'/' {
158            panic!("CotisImageError::NotRelativePath");
159        }
160
161        Self {
162            path: CotisImagePath::StaticPath(path),
163        }
164    }
165
166    /// Returns the original image path.
167    pub fn get_path(&self) -> &str {
168        match &self.path {
169            CotisImagePath::StringPath(s) => s,
170            CotisImagePath::StaticPath(s) => s,
171        }
172    }
173}