Skip to main content

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