lewp_css/domain/at_rules/counter_style/
system.rs

1// This file is part of css. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
2// Copyright © 2017 The developers of css. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT.
3
4use {
5    crate::{
6        domain::CounterStyleIdent,
7        parsers::{Parse, ParserContext},
8        CustomParseError,
9    },
10    cssparser::{ParseError, Parser, ToCss},
11    std::fmt,
12};
13
14/// <https://drafts.csswg.org/css-counter-styles/#counter-style-system>
15#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
16pub enum System {
17    /// 'cyclic'
18    Cyclic,
19
20    /// 'numeric'
21    Numeric,
22
23    /// 'alphabetic'
24    Alphabetic,
25
26    /// 'symbolic'
27    Symbolic,
28
29    /// 'additive'
30    Additive,
31
32    /// 'fixed <integer>?'
33    Fixed {
34        /// '<integer>?'
35        first_symbol_value: Option<i32>,
36    },
37
38    /// 'extends <counter-style-name>'
39    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}