css_ast/types/
grid_line.rs1use super::prelude::*;
2use crate::{CustomIdent, PositiveNonZeroInt};
3use css_parse::parse_optionals;
4
5#[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))]
10pub enum GridLine {
11 Auto(T![Ident]),
12 Span(T![Ident], Option<PositiveNonZeroInt>, Option<T![Ident]>),
13 Area(CustomIdent),
14 Placement(T![Number], Option<T![Ident]>),
15}
16
17impl<'a> Parse<'a> for GridLine {
18 fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
19 where
20 I: Iterator<Item = Cursor> + Clone,
21 {
22 let c = p.peek_n(1);
23 if <T![Ident]>::peek(p, c) {
24 return match p.to_atom::<CssAtomSet>(c) {
25 CssAtomSet::Auto => Ok(GridLine::Auto(p.parse::<T![Ident]>()?)),
26 CssAtomSet::Span => {
27 let keyword = p.parse::<T![Ident]>()?;
28 let (num, ident) = parse_optionals!(p, num: PositiveNonZeroInt, ident: T![Ident]);
29 Ok(Self::Span(keyword, num, ident))
30 }
31 _ => Ok(Self::Area(p.parse::<CustomIdent>()?)),
32 };
33 }
34 let num = p.parse::<T![Number]>()?;
35 {
36 let c: Cursor = num.into();
37 if !num.is_int() {
38 Err(Diagnostic::new(c, Diagnostic::expected_int))?
39 }
40 if num.value() == 0.0 {
41 Err(Diagnostic::new(c, Diagnostic::unexpected_zero))?
42 }
43 }
44
45 Ok(Self::Placement(num, p.parse_if_peek::<T![Ident]>()?))
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52 use crate::CssAtomSet;
53 use css_parse::{assert_parse, assert_parse_error};
54
55 #[test]
56 fn size_test() {
57 assert_eq!(std::mem::size_of::<GridLine>(), 44);
58 }
59
60 #[test]
61 fn test_writes() {
62 assert_parse!(CssAtomSet::ATOMS, GridLine, "auto", GridLine::Auto(_));
63 assert_parse!(CssAtomSet::ATOMS, GridLine, "span 1 foo", GridLine::Span(_, Some(_), Some(_)));
64 assert_parse!(CssAtomSet::ATOMS, GridLine, "span 1");
65 assert_parse!(CssAtomSet::ATOMS, GridLine, "span foo");
66 assert_parse!(CssAtomSet::ATOMS, GridLine, "span foo 1");
67 assert_parse!(CssAtomSet::ATOMS, GridLine, "baz");
68 assert_parse!(CssAtomSet::ATOMS, GridLine, "1 baz");
69 assert_parse!(CssAtomSet::ATOMS, GridLine, "-1 baz");
70 }
71
72 #[test]
73 fn test_errors() {
74 assert_parse_error!(CssAtomSet::ATOMS, GridLine, "span 0 foo");
75 assert_parse_error!(CssAtomSet::ATOMS, GridLine, "span 1.2 foo");
76 assert_parse_error!(CssAtomSet::ATOMS, GridLine, "span -2 foo");
77 assert_parse_error!(CssAtomSet::ATOMS, GridLine, "0 baz");
78 assert_parse_error!(CssAtomSet::ATOMS, GridLine, "span 0");
79 assert_parse_error!(CssAtomSet::ATOMS, GridLine, "span -0 baz");
80 }
81}