Skip to main content

azul_css/props/style/
lists.rs

1//! css/src/props/style/lists.rs
2//!
3//! CSS properties related to list styling.
4
5use alloc::string::{String, ToString};
6use core::fmt;
7
8use crate::{format_rust_code::FormatAsRustCode, props::formatter::PrintAsCssValue};
9
10// --- list-style-type ---
11
12#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13#[repr(C)]
14pub enum StyleListStyleType {
15    None,
16    Disc,
17    Circle,
18    Square,
19    Decimal,
20    DecimalLeadingZero,
21    LowerRoman,
22    UpperRoman,
23    LowerGreek,
24    UpperGreek,
25    LowerAlpha,
26    UpperAlpha,
27}
28
29impl Default for StyleListStyleType {
30    fn default() -> Self {
31        Self::Disc // Default for <ul>
32    }
33}
34
35impl PrintAsCssValue for StyleListStyleType {
36    fn print_as_css_value(&self) -> String {
37        use StyleListStyleType::*;
38        String::from(match self {
39            None => "none",
40            Disc => "disc",
41            Circle => "circle",
42            Square => "square",
43            Decimal => "decimal",
44            DecimalLeadingZero => "decimal-leading-zero",
45            LowerRoman => "lower-roman",
46            UpperRoman => "upper-roman",
47            LowerGreek => "lower-greek",
48            UpperGreek => "upper-greek",
49            LowerAlpha => "lower-alpha",
50            UpperAlpha => "upper-alpha",
51        })
52    }
53}
54
55impl FormatAsRustCode for StyleListStyleType {
56    fn format_as_rust_code(&self, _tabs: usize) -> String {
57        use StyleListStyleType::*;
58        format!(
59            "StyleListStyleType::{}",
60            match self {
61                None => "None",
62                Disc => "Disc",
63                Circle => "Circle",
64                Square => "Square",
65                Decimal => "Decimal",
66                DecimalLeadingZero => "DecimalLeadingZero",
67                LowerRoman => "LowerRoman",
68                UpperRoman => "UpperRoman",
69                LowerGreek => "LowerGreek",
70                UpperGreek => "UpperGreek",
71                LowerAlpha => "LowerAlpha",
72                UpperAlpha => "UpperAlpha",
73            }
74        )
75    }
76}
77
78impl fmt::Display for StyleListStyleType {
79    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80        write!(f, "{}", self.print_as_css_value())
81    }
82}
83
84// --- list-style-position ---
85
86#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
87#[repr(C)]
88pub enum StyleListStylePosition {
89    Inside,
90    Outside,
91}
92
93impl Default for StyleListStylePosition {
94    fn default() -> Self {
95        Self::Outside
96    }
97}
98
99impl PrintAsCssValue for StyleListStylePosition {
100    fn print_as_css_value(&self) -> String {
101        use StyleListStylePosition::*;
102        String::from(match self {
103            Inside => "inside",
104            Outside => "outside",
105        })
106    }
107}
108
109impl FormatAsRustCode for StyleListStylePosition {
110    fn format_as_rust_code(&self, _tabs: usize) -> String {
111        use StyleListStylePosition::*;
112        format!(
113            "StyleListStylePosition::{}",
114            match self {
115                Inside => "Inside",
116                Outside => "Outside",
117            }
118        )
119    }
120}
121
122impl fmt::Display for StyleListStylePosition {
123    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
124        write!(f, "{}", self.print_as_css_value())
125    }
126}
127
128// --- Parsing Logic ---
129
130#[cfg(feature = "parser")]
131#[derive(Clone, PartialEq)]
132pub enum StyleListStyleTypeParseError<'a> {
133    InvalidValue(&'a str),
134}
135
136#[cfg(feature = "parser")]
137impl_debug_as_display!(StyleListStyleTypeParseError<'a>);
138
139#[cfg(feature = "parser")]
140impl_display! { StyleListStyleTypeParseError<'a>, {
141    InvalidValue(val) => format!("Invalid list-style-type value: \"{}\"", val),
142}}
143
144#[cfg(feature = "parser")]
145#[derive(Debug, Clone, PartialEq)]
146pub enum StyleListStyleTypeParseErrorOwned {
147    InvalidValue(String),
148}
149
150#[cfg(feature = "parser")]
151impl<'a> StyleListStyleTypeParseError<'a> {
152    pub fn to_contained(&self) -> StyleListStyleTypeParseErrorOwned {
153        match self {
154            Self::InvalidValue(s) => StyleListStyleTypeParseErrorOwned::InvalidValue(s.to_string()),
155        }
156    }
157}
158
159#[cfg(feature = "parser")]
160impl StyleListStyleTypeParseErrorOwned {
161    pub fn to_shared<'a>(&'a self) -> StyleListStyleTypeParseError<'a> {
162        match self {
163            Self::InvalidValue(s) => StyleListStyleTypeParseError::InvalidValue(s.as_str()),
164        }
165    }
166}
167
168#[cfg(feature = "parser")]
169pub fn parse_style_list_style_type<'a>(
170    input: &'a str,
171) -> Result<StyleListStyleType, StyleListStyleTypeParseError<'a>> {
172    let input = input.trim();
173    match input {
174        "none" => Ok(StyleListStyleType::None),
175        "disc" => Ok(StyleListStyleType::Disc),
176        "circle" => Ok(StyleListStyleType::Circle),
177        "square" => Ok(StyleListStyleType::Square),
178        "decimal" => Ok(StyleListStyleType::Decimal),
179        "decimal-leading-zero" => Ok(StyleListStyleType::DecimalLeadingZero),
180        "lower-roman" => Ok(StyleListStyleType::LowerRoman),
181        "upper-roman" => Ok(StyleListStyleType::UpperRoman),
182        "lower-greek" => Ok(StyleListStyleType::LowerGreek),
183        "upper-greek" => Ok(StyleListStyleType::UpperGreek),
184        "lower-alpha" | "lower-latin" => Ok(StyleListStyleType::LowerAlpha),
185        "upper-alpha" | "upper-latin" => Ok(StyleListStyleType::UpperAlpha),
186        _ => Err(StyleListStyleTypeParseError::InvalidValue(input)),
187    }
188}
189
190#[cfg(feature = "parser")]
191#[derive(Clone, PartialEq)]
192pub enum StyleListStylePositionParseError<'a> {
193    InvalidValue(&'a str),
194}
195
196#[cfg(feature = "parser")]
197impl_debug_as_display!(StyleListStylePositionParseError<'a>);
198
199#[cfg(feature = "parser")]
200impl_display! { StyleListStylePositionParseError<'a>, {
201    InvalidValue(val) => format!("Invalid list-style-position value: \"{}\"", val),
202}}
203
204#[cfg(feature = "parser")]
205#[derive(Debug, Clone, PartialEq)]
206pub enum StyleListStylePositionParseErrorOwned {
207    InvalidValue(String),
208}
209
210#[cfg(feature = "parser")]
211impl<'a> StyleListStylePositionParseError<'a> {
212    pub fn to_contained(&self) -> StyleListStylePositionParseErrorOwned {
213        match self {
214            Self::InvalidValue(s) => {
215                StyleListStylePositionParseErrorOwned::InvalidValue(s.to_string())
216            }
217        }
218    }
219}
220
221#[cfg(feature = "parser")]
222impl StyleListStylePositionParseErrorOwned {
223    pub fn to_shared<'a>(&'a self) -> StyleListStylePositionParseError<'a> {
224        match self {
225            Self::InvalidValue(s) => StyleListStylePositionParseError::InvalidValue(s.as_str()),
226        }
227    }
228}
229
230#[cfg(feature = "parser")]
231pub fn parse_style_list_style_position<'a>(
232    input: &'a str,
233) -> Result<StyleListStylePosition, StyleListStylePositionParseError<'a>> {
234    let input = input.trim();
235    match input {
236        "inside" => Ok(StyleListStylePosition::Inside),
237        "outside" => Ok(StyleListStylePosition::Outside),
238        _ => Err(StyleListStylePositionParseError::InvalidValue(input)),
239    }
240}