lewp_css/domain/at_rules/counter_style/
system.rs1use {
5 crate::{
6 domain::CounterStyleIdent,
7 parsers::{Parse, ParserContext},
8 CustomParseError,
9 },
10 cssparser::{ParseError, Parser, ToCss},
11 std::fmt,
12};
13
14#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
16pub enum System {
17 Cyclic,
19
20 Numeric,
22
23 Alphabetic,
25
26 Symbolic,
28
29 Additive,
31
32 Fixed {
34 first_symbol_value: Option<i32>,
36 },
37
38 Extends(CounterStyleIdent),
40}
41
42impl Parse for System {
43 fn parse<'i, 't>(
44 _context: &ParserContext,
45 input: &mut Parser<'i, 't>,
46 ) -> Result<Self, ParseError<'i, CustomParseError<'i>>> {
47 use self::System::*;
48
49 let identifier = input.expect_ident_cloned()?;
50
51 match_ignore_ascii_case! {
52 &*identifier,
53
54 "cyclic" => Ok(Cyclic),
55
56 "numeric" => Ok(Numeric),
57
58 "alphabetic" => Ok(Alphabetic),
59
60 "symbolic" => Ok(Symbolic),
61
62 "additive" => Ok(Additive),
63
64 "fixed" =>
65 {
66 let first_symbol_value = input.r#try(|i| i.expect_integer()).ok();
67 Ok(Fixed { first_symbol_value })
68 },
69
70 "extends" => Ok(Extends(CounterStyleIdent::parse(input)?)),
71
72 _ => Err(ParseError::from(CustomParseError::CounterStyleSystemIsNotKnown(identifier.clone()))),
73 }
74 }
75}
76
77impl ToCss for System {
78 fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
79 use self::System::*;
80
81 match *self {
82 Cyclic => dest.write_str("cyclic"),
83
84 Numeric => dest.write_str("numeric"),
85
86 Alphabetic => dest.write_str("alphabetic"),
87
88 Symbolic => dest.write_str("symbolic"),
89
90 Additive => dest.write_str("additive"),
91
92 Fixed { first_symbol_value } => {
93 if let Some(value) = first_symbol_value {
94 dest.write_str("fixed ")?;
95 value.to_css(dest)
96 } else {
97 dest.write_str("fixed")
98 }
99 }
100
101 Extends(ref other) => {
102 dest.write_str("extends ")?;
103 other.to_css(dest)
104 }
105 }
106 }
107}