cotis_defaults/
generic_image.rs1#[derive(Clone)]
2enum CotisImagePath {
3 StringPath(String),
4 StaticPath(&'static str),
5}
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum CotisImageError {
10 NotRelativePath,
12 EmptyPath,
14}
15
16#[derive(Clone)]
18pub struct JPEGImage {
19 path: CotisImagePath,
20}
21
22impl JPEGImage {
23 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 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 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#[derive(Clone)]
71pub struct SVGImage {
72 path: CotisImagePath,
73}
74
75impl SVGImage {
76 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 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 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#[derive(Clone)]
124pub struct PNGImage {
125 path: CotisImagePath,
126}
127
128impl PNGImage {
129 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 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 pub fn get_path(&self) -> &str {
168 match &self.path {
169 CotisImagePath::StringPath(s) => s,
170 CotisImagePath::StaticPath(s) => s,
171 }
172 }
173}