css_ast/types/
grid_line.rs

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