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

            
7
use alloc::string::String;
8
use core::fmt;
9

            
10
// Re-export the PrintAsCssValue trait from the css module
11
pub 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)]
15
use crate::props::{
16
    layout::{dimensions::*, spacing::*},
17
    style::{border::*, border_radius::*},
18
};
19

            
20
/// Zero-allocation CSS value formatting trait using `fmt::Formatter`.
21
///
22
/// Unlike `PrintAsCssValue` (which returns a `String`), this trait writes
23
/// directly into a formatter and is suitable for `Display` impl delegation.
24
pub trait FormatAsCssValue {
25
    /// # Errors
26
    ///
27
    /// Returns an error if writing to the formatter `f` fails.
28
    fn format_as_css_value(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
29
}
30

            
31
macro_rules! impl_print_as_css_display {
32
    ($($t:ty),+ $(,)?) => {
33
        $(impl PrintAsCssValue for $t {
34
1749
            fn print_as_css_value(&self) -> String {
35
1749
                format!("{}", self.inner)
36
1749
            }
37
        })+
38
    };
39
}
40

            
41
macro_rules! impl_print_as_css_hash {
42
    ($($t:ty),+ $(,)?) => {
43
        $(impl PrintAsCssValue for $t {
44
            fn print_as_css_value(&self) -> String {
45
                self.inner.to_hash()
46
            }
47
        })+
48
    };
49
}
50

            
51
// --- Style Properties ---
52

            
53
impl_print_as_css_display!(
54
    StyleBorderTopLeftRadius,
55
    StyleBorderTopRightRadius,
56
    StyleBorderBottomLeftRadius,
57
    StyleBorderBottomRightRadius,
58
    StyleBorderTopStyle,
59
    StyleBorderRightStyle,
60
    StyleBorderBottomStyle,
61
    StyleBorderLeftStyle,
62
    LayoutBorderTopWidth,
63
    LayoutBorderRightWidth,
64
    LayoutBorderBottomWidth,
65
    LayoutBorderLeftWidth,
66
);
67

            
68
impl_print_as_css_hash!(
69
    StyleBorderTopColor,
70
    StyleBorderRightColor,
71
    StyleBorderBottomColor,
72
    StyleBorderLeftColor,
73
);
74

            
75
// --- Layout Spacing Properties ---
76

            
77
impl_print_as_css_display!(
78
    LayoutPaddingTop,
79
    LayoutPaddingLeft,
80
    LayoutPaddingRight,
81
    LayoutPaddingBottom,
82
    LayoutPaddingInlineStart,
83
    LayoutPaddingInlineEnd,
84
    LayoutMarginTop,
85
    LayoutMarginLeft,
86
    LayoutMarginRight,
87
    LayoutMarginBottom,
88
    LayoutColumnGap,
89
    LayoutRowGap,
90
);