css_ast/types/
grid_line.rs1use crate::{PositiveNonZeroInt, Visitable, diagnostics};
2use css_parse::{Cursor, Parse, Parser, Result as ParserResult, T, keyword_set, parse_optionals};
3use csskit_derives::{Peek, ToCursors, ToSpan};
4
5keyword_set!(pub enum GridLineKeywords { Auto: "auto", Span: "span" });
6
7#[derive(Peek, ToCursors, ToSpan, Visitable, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
11#[visit(self)]
12pub enum GridLine {
13 Auto(GridLineKeywords),
14 Span(GridLineKeywords, Option<PositiveNonZeroInt>, Option<T![Ident]>),
15 Area(T![Ident]),
16 Placement(T![Number], Option<T![Ident]>),
17}
18
19impl<'a> Parse<'a> for GridLine {
20 fn parse(p: &mut Parser<'a>) -> ParserResult<Self> {
21 if let Some(keyword) = p.parse_if_peek::<GridLineKeywords>()? {
22 return match keyword {
23 GridLineKeywords::Auto(_) => Ok(GridLine::Auto(keyword)),
24 GridLineKeywords::Span(_) => {
25 let (num, ident) = parse_optionals!(p, num: PositiveNonZeroInt, ident: T![Ident]);
26 Ok(Self::Span(keyword, num, ident))
27 }
28 };
29 }
30
31 if let Some(ident) = p.parse_if_peek::<T![Ident]>()? {
32 return Ok(Self::Area(ident));
33 }
34
35 let num = p.parse::<T![Number]>()?;
36 {
37 let c: Cursor = num.into();
38 if !num.is_int() {
39 Err(diagnostics::ExpectedInt(c))?
40 }
41 if num.value() == 0.0 {
42 Err(diagnostics::UnexpectedZero(c))?
43 }
44 }
45
46 Ok(Self::Placement(num, p.parse_if_peek::<T![Ident]>()?))
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53 use css_parse::{assert_parse, assert_parse_error};
54
55 #[test]
56 fn size_test() {
57 assert_eq!(std::mem::size_of::<GridLine>(), 48);
58 }
59
60 #[test]
61 fn test_writes() {
62 assert_parse!(GridLine, "auto", GridLine::Auto(_));
63 assert_parse!(GridLine, "span 1 foo", GridLine::Span(_, Some(_), Some(_)));
64 assert_parse!(GridLine, "span 1");
65 assert_parse!(GridLine, "span foo");
66 assert_parse!(GridLine, "span foo 1", "span 1 foo");
67 assert_parse!(GridLine, "baz");
68 assert_parse!(GridLine, "1 baz");
69 assert_parse!(GridLine, "-1 baz");
70 }
71
72 #[test]
73 fn test_errors() {
74 assert_parse_error!(GridLine, "span 0 foo");
75 assert_parse_error!(GridLine, "span 1.2 foo");
76 assert_parse_error!(GridLine, "span -2 foo");
77 assert_parse_error!(GridLine, "0 baz");
78 assert_parse_error!(GridLine, "span 0");
79 assert_parse_error!(GridLine, "span -0 baz");
80 }
81}