Skip to main content

azul_css/props/
formatter.rs

1//! Trait and implementations for formatting CSS properties back into strings.
2//!
3//! This module defines `FormatAsCssValue` (zero-alloc, `fmt::Formatter`-based)
4//! and re-exports `PrintAsCssValue` (from `css.rs`, returns `String`).
5//! `PrintAsCssValue` impls for border, padding, margin, and gap types live here.
6
7use alloc::string::String;
8use core::fmt;
9
10// Re-export the PrintAsCssValue trait from the css module
11pub use crate::css::PrintAsCssValue;
12// wildcard imports: this formatter pulls in every layout/style value type it
13// renders; enumerating them all explicitly is unmaintainable.
14#[allow(clippy::wildcard_imports)]
15use crate::props::{
16    layout::{dimensions::*, spacing::*},
17    style::{
18        border::*,
19        border_radius::*,
20    },
21};
22
23/// Zero-allocation CSS value formatting trait using `fmt::Formatter`.
24///
25/// Unlike `PrintAsCssValue` (which returns a `String`), this trait writes
26/// directly into a formatter and is suitable for `Display` impl delegation.
27pub trait FormatAsCssValue {
28    /// # Errors
29    ///
30    /// Returns an error if writing to the formatter `f` fails.
31    fn format_as_css_value(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
32}
33
34macro_rules! impl_print_as_css_display {
35    ($($t:ty),+ $(,)?) => {
36        $(impl PrintAsCssValue for $t {
37            fn print_as_css_value(&self) -> String {
38                format!("{}", self.inner)
39            }
40        })+
41    };
42}
43
44macro_rules! impl_print_as_css_hash {
45    ($($t:ty),+ $(,)?) => {
46        $(impl PrintAsCssValue for $t {
47            fn print_as_css_value(&self) -> String {
48                self.inner.to_hash()
49            }
50        })+
51    };
52}
53
54// --- Style Properties ---
55
56impl_print_as_css_display!(
57    StyleBorderTopLeftRadius,
58    StyleBorderTopRightRadius,
59    StyleBorderBottomLeftRadius,
60    StyleBorderBottomRightRadius,
61    StyleBorderTopStyle,
62    StyleBorderRightStyle,
63    StyleBorderBottomStyle,
64    StyleBorderLeftStyle,
65    LayoutBorderTopWidth,
66    LayoutBorderRightWidth,
67    LayoutBorderBottomWidth,
68    LayoutBorderLeftWidth,
69);
70
71impl_print_as_css_hash!(
72    StyleBorderTopColor,
73    StyleBorderRightColor,
74    StyleBorderBottomColor,
75    StyleBorderLeftColor,
76);
77
78// --- Layout Spacing Properties ---
79
80impl_print_as_css_display!(
81    LayoutPaddingTop,
82    LayoutPaddingLeft,
83    LayoutPaddingRight,
84    LayoutPaddingBottom,
85    LayoutPaddingInlineStart,
86    LayoutPaddingInlineEnd,
87    LayoutMarginTop,
88    LayoutMarginLeft,
89    LayoutMarginRight,
90    LayoutMarginBottom,
91    LayoutColumnGap,
92    LayoutRowGap,
93);