css_ast/types/
grid_line.rs1use super::prelude::*;
2use crate::{CSSInt, CustomIdent, NonZero, NumericValue, PositiveNonZeroInt};
3use css_parse::parse_optionals;
4
5#[node]
11#[derive(Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
13#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
14#[derive(csskit_derives::NodeWithMetadata)]
15pub enum GridLine<'a> {
16 Auto(T![Ident]),
17 Span(T![Ident], Option<NumericValue<'a, PositiveNonZeroInt>>, Option<T![Ident]>),
18 Area(CustomIdent),
19 Placement(NumericValue<'a, NonZero<CSSInt>>, Option<T![Ident]>),
20}
21
22impl<'a> Parse<'a> for GridLine<'a> {
23 fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
24 where
25 I: Iterator<Item = Cursor> + Clone,
26 {
27 let c = p.peek_n(1);
28 if <T![Ident]>::peek(p, c) {
29 return match p.to_atom::<CssAtomSet>(c) {
30 CssAtomSet::Auto => Ok(GridLine::Auto(p.parse::<T![Ident]>()?)),
31 CssAtomSet::Span => {
32 let keyword = p.parse::<T![Ident]>()?;
33 let (num, ident) = parse_optionals!(p, num: NumericValue<PositiveNonZeroInt>, ident: T![Ident]);
34 Ok(Self::Span(keyword, num, ident))
35 }
36 _ => Ok(Self::Area(p.parse::<CustomIdent>()?)),
37 };
38 }
39 let num = p.parse::<NumericValue<NonZero<CSSInt>>>()?;
40 Ok(Self::Placement(num, p.parse_if_peek::<T![Ident]>()?))
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47 use crate::CssAtomSet;
48 use css_parse::{assert_parse, assert_parse_error};
49
50 #[test]
51 fn test_writes() {
52 assert_parse!(CssAtomSet::ATOMS, GridLine, "auto", GridLine::Auto(_));
53 assert_parse!(CssAtomSet::ATOMS, GridLine, "span 1 foo", GridLine::Span(_, Some(_), Some(_)));
54 assert_parse!(CssAtomSet::ATOMS, GridLine, "span 1");
55 assert_parse!(CssAtomSet::ATOMS, GridLine, "span foo");
56 assert_parse!(CssAtomSet::ATOMS, GridLine, "span foo 1");
57 assert_parse!(CssAtomSet::ATOMS, GridLine, "baz");
58 assert_parse!(CssAtomSet::ATOMS, GridLine, "1 baz");
59 assert_parse!(CssAtomSet::ATOMS, GridLine, "-1 baz");
60 }
61
62 #[test]
63 fn test_substitution() {
64 assert_parse!(CssAtomSet::ATOMS, GridLine, "var(--n)");
65 assert_parse!(CssAtomSet::ATOMS, GridLine, "calc(1 + 1) baz");
66 assert_parse!(CssAtomSet::ATOMS, GridLine, "span var(--n)");
67 assert_parse!(CssAtomSet::ATOMS, GridLine, "span calc(1 + 1) foo");
68 }
69
70 #[test]
71 fn test_errors() {
72 assert_parse_error!(CssAtomSet::ATOMS, GridLine, "span 0 foo");
73 assert_parse_error!(CssAtomSet::ATOMS, GridLine, "span 1.2 foo");
74 assert_parse_error!(CssAtomSet::ATOMS, GridLine, "span -2 foo");
75 assert_parse_error!(CssAtomSet::ATOMS, GridLine, "0 baz");
76 assert_parse_error!(CssAtomSet::ATOMS, GridLine, "span 0");
77 assert_parse_error!(CssAtomSet::ATOMS, GridLine, "span -0 baz");
78 }
79}