rustyle_css/css/
units.rs

1//! CSS unit types
2
3/// Represents a CSS length value
4#[derive(Clone, Debug)]
5pub enum Length {
6    /// Pixels
7    Px(f32),
8    /// Rem units
9    Rem(f32),
10    /// Em units
11    Em(f32),
12    /// Viewport width
13    Vw(f32),
14    /// Viewport height
15    Vh(f32),
16    /// Percentage
17    Percent(f32),
18    /// Auto
19    Auto,
20    /// Zero
21    Zero,
22}
23
24impl Length {
25    /// Create a pixel value
26    pub fn px(value: f32) -> Self {
27        Self::Px(value)
28    }
29
30    /// Create a rem value
31    pub fn rem(value: f32) -> Self {
32        Self::Rem(value)
33    }
34
35    /// Create an em value
36    pub fn em(value: f32) -> Self {
37        Self::Em(value)
38    }
39
40    /// Create a viewport width value
41    pub fn vw(value: f32) -> Self {
42        Self::Vw(value)
43    }
44
45    /// Create a viewport height value
46    pub fn vh(value: f32) -> Self {
47        Self::Vh(value)
48    }
49
50    /// Create a percentage value
51    pub fn percent(value: f32) -> Self {
52        Self::Percent(value)
53    }
54
55    /// Convert to CSS string
56    pub fn to_css(&self) -> String {
57        match self {
58            Length::Px(v) => format!("{}px", v),
59            Length::Rem(v) => format!("{}rem", v),
60            Length::Em(v) => format!("{}em", v),
61            Length::Vw(v) => format!("{}vw", v),
62            Length::Vh(v) => format!("{}vh", v),
63            Length::Percent(v) => format!("{}%", v),
64            Length::Auto => "auto".to_string(),
65            Length::Zero => "0".to_string(),
66        }
67    }
68}
69
70/// Represents a CSS percentage value
71#[derive(Clone, Debug)]
72pub struct Percentage(pub f32);
73
74impl Percentage {
75    /// Create a percentage value
76    pub fn new(value: f32) -> Self {
77        Self(value)
78    }
79
80    /// Convert to CSS string
81    pub fn to_css(&self) -> String {
82        format!("{}%", self.0)
83    }
84}
85
86/// Represents a CSS time value
87#[derive(Clone, Debug)]
88pub enum Time {
89    /// Seconds
90    Seconds(f32),
91    /// Milliseconds
92    Milliseconds(f32),
93}
94
95impl Time {
96    /// Create a seconds value
97    pub fn seconds(value: f32) -> Self {
98        Self::Seconds(value)
99    }
100
101    /// Create a milliseconds value
102    pub fn milliseconds(value: f32) -> Self {
103        Self::Milliseconds(value)
104    }
105
106    /// Convert to CSS string
107    pub fn to_css(&self) -> String {
108        match self {
109            Time::Seconds(v) => format!("{}s", v),
110            Time::Milliseconds(v) => format!("{}ms", v),
111        }
112    }
113}
114
115/// Represents a CSS angle value
116#[derive(Clone, Debug)]
117pub enum Angle {
118    /// Degrees
119    Deg(f32),
120    /// Radians
121    Rad(f32),
122}
123
124impl Angle {
125    /// Create a degrees value
126    pub fn deg(value: f32) -> Self {
127        Self::Deg(value)
128    }
129
130    /// Create a radians value
131    pub fn rad(value: f32) -> Self {
132        Self::Rad(value)
133    }
134
135    /// Convert to CSS string
136    pub fn to_css(&self) -> String {
137        match self {
138            Angle::Deg(v) => format!("{}deg", v),
139            Angle::Rad(v) => format!("{}rad", v),
140        }
141    }
142}