image_rs/common.rs
1use std::str::FromStr;
2
3/// Enum representing the layout of an image.
4///
5/// Specifies how an image should be positioned or sized within its container.
6/// The default layout is `Layout::Auto`.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
8pub enum Layout {
9 /// The image should stretch to fill the container, possibly distorting the aspect ratio.
10 Fill,
11
12 /// The image will scale the size of the container while maintaining the aspect ratio.
13 Responsive,
14
15 /// The image will be displayed at its intrinsic size (its natural width and height).
16 Intrinsic,
17
18 /// The image will have a fixed width and height, regardless of the container size.
19 Fixed,
20
21 /// Automatically adjusts the layout based on other properties (default behavior).
22 #[default]
23 Auto,
24
25 /// Stretches the image to fill the container, similar to `Fill`, but respects the aspect ratio more strictly.
26 Stretch,
27
28 /// The image is scaled down to fit the container but does not scale up beyond its original size.
29 ScaleDown,
30}
31
32impl Layout {
33 /// Converts the `Layout` enum variant to its corresponding string representation.
34 ///
35 /// Returns a string that represents the layout type, useful for passing as a CSS property.
36 pub fn as_str(&self) -> &'static str {
37 match self {
38 Layout::Fill => "fill",
39 Layout::Responsive => "responsive",
40 Layout::Intrinsic => "intrinsic",
41 Layout::Fixed => "fixed",
42 Layout::Auto => "auto",
43 Layout::Stretch => "stretch",
44 Layout::ScaleDown => "scale-down",
45 }
46 }
47}
48
49impl FromStr for Layout {
50 type Err = ();
51
52 /// Parses a string into a `Layout` enum variant.
53 ///
54 /// This method converts a string representation of a layout into the corresponding `Layout` enum variant.
55 /// If the string doesn't match any valid layout value, it returns an error.
56 fn from_str(s: &str) -> Result<Self, Self::Err> {
57 match s {
58 "fill" => Ok(Layout::Fill),
59 "responsive" => Ok(Layout::Responsive),
60 "intrinsic" => Ok(Layout::Intrinsic),
61 "fixed" => Ok(Layout::Fixed),
62 "auto" => Ok(Layout::Auto),
63 "stretch" => Ok(Layout::Stretch),
64 "scale-down" => Ok(Layout::ScaleDown),
65 _ => Err(()),
66 }
67 }
68}
69
70/// Enum for the `decoding` attribute of the `Image` component.
71///
72/// Specifies how the image should be decoded. This controls when the browser decodes the image
73/// relative to its loading behavior. It ensures that only valid decoding options are used,
74/// improving type safety and preventing mistakes.
75///
76/// The default behavior is `Auto`.
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
78pub enum Decoding {
79 /// Let the browser automatically decide the best decoding method.
80 #[default]
81 Auto,
82
83 /// Force synchronous decoding (may block other operations).
84 Sync,
85
86 /// Allow asynchronous decoding (non-blocking).
87 Async,
88}
89
90impl Decoding {
91 /// Returns the string value associated with the `Decoding` option.
92 ///
93 /// Useful for setting HTML attributes.
94 pub fn as_str(&self) -> &'static str {
95 match self {
96 Decoding::Auto => "auto",
97 Decoding::Sync => "sync",
98 Decoding::Async => "async",
99 }
100 }
101}
102
103impl FromStr for Decoding {
104 type Err = ();
105
106 /// Parses a string into a `Decoding` enum variant.
107 ///
108 /// Accepts case-insensitive inputs like `"auto"`, `"sync"`, or `"async"`.
109 fn from_str(s: &str) -> Result<Self, Self::Err> {
110 match s.to_lowercase().as_str() {
111 "auto" => Ok(Decoding::Auto),
112 "sync" => Ok(Decoding::Sync),
113 "async" => Ok(Decoding::Async),
114 _ => Err(()),
115 }
116 }
117}
118
119/// Enum representing possible values for the `object-position` attribute of the `Image` component.
120///
121/// Controls how the image is positioned inside its container when using `object-fit`.
122/// This provides predefined common positions while ensuring type safety.
123#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
124pub enum Position {
125 /// Center the image both horizontally and vertically (default).
126 #[default]
127 Center,
128
129 /// Align the image to the top.
130 Top,
131
132 /// Align the image to the bottom.
133 Bottom,
134
135 /// Align the image to the left.
136 Left,
137
138 /// Align the image to the right.
139 Right,
140
141 /// Align the image to the top-left corner.
142 TopLeft,
143
144 /// Align the image to the top-right corner.
145 TopRight,
146
147 /// Align the image to the bottom-left corner.
148 BottomLeft,
149
150 /// Align the image to the bottom-right corner.
151 BottomRight,
152}
153
154impl Position {
155 /// Returns the string value associated with the `Position` option.
156 ///
157 /// Useful for setting the `object-position` CSS property.
158 pub fn as_str(&self) -> &'static str {
159 match self {
160 Position::Center => "center",
161 Position::Top => "top",
162 Position::Bottom => "bottom",
163 Position::Left => "left",
164 Position::Right => "right",
165 Position::TopLeft => "top left",
166 Position::TopRight => "top right",
167 Position::BottomLeft => "bottom left",
168 Position::BottomRight => "bottom right",
169 }
170 }
171}
172
173/// Enum representing possible values for the `object-fit` attribute of the `Image` component.
174///
175/// Defines how the image should be resized to fit its container.
176/// Provides predefined options while ensuring type safety.
177#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
178pub enum ObjectFit {
179 /// Fill the container without preserving the aspect ratio.
180 Fill,
181
182 /// Contain the image within the container while preserving aspect ratio.
183 #[default]
184 Contain,
185
186 /// Cover the container while preserving aspect ratio (may crop).
187 Cover,
188
189 /// Scale down the image only if necessary.
190 ScaleDown,
191
192 /// Do not resize the image.
193 None,
194}
195
196impl ObjectFit {
197 /// Returns the string value associated with the `ObjectFit` option.
198 ///
199 /// Useful for setting the `object-fit` CSS property.
200 pub fn as_str(&self) -> &'static str {
201 match self {
202 ObjectFit::Fill => "fill",
203 ObjectFit::Contain => "contain",
204 ObjectFit::Cover => "cover",
205 ObjectFit::ScaleDown => "scale-down",
206 ObjectFit::None => "none",
207 }
208 }
209}
210
211#[derive(Clone, PartialEq, Default)]
212pub enum CrossOrigin {
213 Anonymous,
214 UseCredentials,
215 #[default]
216 None,
217}
218
219impl CrossOrigin {
220 pub fn as_str(&self) -> Option<&'static str> {
221 match self {
222 CrossOrigin::Anonymous => Some("anonymous"),
223 CrossOrigin::UseCredentials => Some("use-credentials"),
224 CrossOrigin::None => None,
225 }
226 }
227}
228
229#[derive(Clone, PartialEq, Default)]
230pub enum FetchPriority {
231 High,
232 Low,
233 #[default]
234 Auto,
235}
236
237impl FetchPriority {
238 pub fn as_str(&self) -> &'static str {
239 match self {
240 FetchPriority::High => "high",
241 FetchPriority::Low => "low",
242 FetchPriority::Auto => "auto",
243 }
244 }
245}
246
247#[derive(Clone, PartialEq, Default)]
248pub enum Loading {
249 Eager,
250 Lazy,
251 #[default]
252 Auto,
253}
254
255impl Loading {
256 pub fn as_str(&self) -> &'static str {
257 match self {
258 Loading::Eager => "eager",
259 Loading::Lazy => "lazy",
260 Loading::Auto => "auto",
261 }
262 }
263}
264
265/// Defines the referrer policy to use when fetching a resource.
266///
267/// Controls how much referrer information should be included with the request.
268#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
269pub enum ReferrerPolicy {
270 /// No Referer header will be sent.
271 NoReferrer,
272 /// The Referer header will not be sent to origins without TLS (HTTPS).
273 NoReferrerWhenDowngrade,
274 /// Only the origin of the document is sent.
275 Origin,
276 /// Send full referrer on same-origin, but only origin on cross-origin.
277 OriginWhenCrossOrigin,
278 /// Referrer will be sent for same-origin only.
279 SameOrigin,
280 /// Send the origin only, and only when security level is preserved.
281 StrictOrigin,
282 /// Default: full URL for same-origin, origin only for cross-origin HTTPS, nothing for HTTP.
283 #[default]
284 StrictOriginWhenCrossOrigin,
285 /// Unsafe: send origin and path (not fragment/password/username), even to insecure origins.
286 UnsafeUrl,
287}
288
289impl ReferrerPolicy {
290 /// Returns the string representation of the referrer policy, as used in HTML.
291 pub fn as_str(&self) -> &'static str {
292 match self {
293 ReferrerPolicy::NoReferrer => "no-referrer",
294 ReferrerPolicy::NoReferrerWhenDowngrade => "no-referrer-when-downgrade",
295 ReferrerPolicy::Origin => "origin",
296 ReferrerPolicy::OriginWhenCrossOrigin => "origin-when-cross-origin",
297 ReferrerPolicy::SameOrigin => "same-origin",
298 ReferrerPolicy::StrictOrigin => "strict-origin",
299 ReferrerPolicy::StrictOriginWhenCrossOrigin => "strict-origin-when-cross-origin",
300 ReferrerPolicy::UnsafeUrl => "unsafe-url",
301 }
302 }
303}
304
305#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
306pub enum AriaLive {
307 #[default]
308 Off,
309 Polite,
310 Assertive,
311}
312
313impl AriaLive {
314 pub fn as_str(&self) -> &'static str {
315 match self {
316 AriaLive::Off => "off",
317 AriaLive::Polite => "polite",
318 AriaLive::Assertive => "assertive",
319 }
320 }
321}
322
323#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
324pub enum AriaPressed {
325 True,
326 False,
327 Mixed,
328 #[default]
329 Undefined,
330}
331
332impl AriaPressed {
333 pub fn as_str(&self) -> &'static str {
334 match self {
335 AriaPressed::True => "true",
336 AriaPressed::False => "false",
337 AriaPressed::Mixed => "mixed",
338 AriaPressed::Undefined => "undefined",
339 }
340 }
341}