rustyle_css/dynamic/
builder.rs

1//! Dynamic style builder
2//!
3//! Provides a type-safe API for building styles at runtime.
4
5use crate::css::{Color, Radius, Spacing};
6
7/// Dynamic style builder for runtime style generation
8pub struct DynamicStyleBuilder {
9    styles: Vec<String>,
10}
11
12impl DynamicStyleBuilder {
13    /// Create a new dynamic style builder
14    pub fn new() -> Self {
15        Self { styles: Vec::new() }
16    }
17
18    /// Add background color
19    pub fn background_color(mut self, color: Color) -> Self {
20        self.styles
21            .push(format!("background-color: {};", color.to_css()));
22        self
23    }
24
25    /// Add color
26    pub fn color(mut self, color: Color) -> Self {
27        self.styles.push(format!("color: {};", color.to_css()));
28        self
29    }
30
31    /// Add padding
32    pub fn padding(mut self, padding: Spacing) -> Self {
33        self.styles.push(format!("padding: {};", padding.to_css()));
34        self
35    }
36
37    /// Add margin
38    pub fn margin(mut self, margin: Spacing) -> Self {
39        self.styles.push(format!("margin: {};", margin.to_css()));
40        self
41    }
42
43    /// Add border radius
44    pub fn border_radius(mut self, radius: Radius) -> Self {
45        self.styles
46            .push(format!("border-radius: {};", radius.to_css()));
47        self
48    }
49
50    /// Add a custom property
51    pub fn property(mut self, property: &str, value: &str) -> Self {
52        self.styles.push(format!("{}: {};", property, value));
53        self
54    }
55
56    /// Build the CSS string
57    pub fn build(self) -> String {
58        self.styles.join(" ")
59    }
60}
61
62impl Default for DynamicStyleBuilder {
63    fn default() -> Self {
64        Self::new()
65    }
66}