1use crate::{
2 AssociatedWhitespaceRules, Cursor, CursorSink, Diagnostic, FunctionBlock, Kind, KindSet, Parse, Parser, Peek,
3 Result as ParserResult, SemanticEq, SimpleBlock, Span, State, T, ToCursors, ToSpan,
4};
5
6#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(untagged))]
11pub enum ComponentValue<'a> {
12 SimpleBlock(SimpleBlock<'a>),
13 Function(FunctionBlock<'a>),
14 Whitespace(T![Whitespace]),
15 Number(T![Number]),
16 Dimension(T![Dimension]),
17 Ident(T![Ident]),
18 AtKeyword(T![AtKeyword]),
19 Hash(T![Hash]),
20 String(T![String]),
21 Url(T![Url]),
22 Delim(T![Delim]),
23 Colon(T![:]),
24 Semicolon(T![;]),
25 Comma(T![,]),
26}
27
28impl<'a> Peek<'a> for ComponentValue<'a> {
29 fn peek<Iter>(_: &Parser<'a, Iter>, c: Cursor) -> bool
30 where
31 Iter: Iterator<Item = Cursor> + Clone,
32 {
33 let kindset = KindSet::new(&[
34 Kind::Whitespace,
35 Kind::Number,
36 Kind::Dimension,
37 Kind::Ident,
38 Kind::AtKeyword,
39 Kind::Hash,
40 Kind::String,
41 Kind::Url,
42 Kind::Delim,
43 Kind::Colon,
44 Kind::Semicolon,
45 Kind::Comma,
46 Kind::Function,
47 Kind::LeftCurly,
48 Kind::LeftParen,
49 Kind::LeftSquare,
50 ]);
51 c == kindset
52 }
53}
54
55impl<'a> Parse<'a> for ComponentValue<'a> {
57 fn parse<Iter>(p: &mut Parser<'a, Iter>) -> ParserResult<Self>
58 where
59 Iter: Iterator<Item = Cursor> + Clone,
60 {
61 let c = p.peek_n(1);
62 Ok(if <T![' ']>::peek(p, c) {
63 Self::Whitespace(p.parse::<T![' ']>()?)
64 } else if <T![PairWiseStart]>::peek(p, c) {
65 let old_state = p.set_state(State::Nested);
66 let block = p.parse::<SimpleBlock>();
67 p.set_state(old_state);
68 Self::SimpleBlock(block?)
69 } else if <T![Function]>::peek(p, c) {
70 Self::Function(p.parse::<FunctionBlock>()?)
71 } else if <T![Number]>::peek(p, c) {
72 Self::Number(p.parse::<T![Number]>()?)
73 } else if <T![Dimension]>::peek(p, c) {
74 Self::Dimension(p.parse::<T![Dimension]>()?)
75 } else if <T![Ident]>::peek(p, c) {
76 Self::Ident(p.parse::<T![Ident]>()?)
77 } else if <T![AtKeyword]>::peek(p, c) {
78 Self::AtKeyword(p.parse::<T![AtKeyword]>()?)
79 } else if <T![Hash]>::peek(p, c) {
80 Self::Hash(p.parse::<T![Hash]>()?)
81 } else if <T![String]>::peek(p, c) {
82 Self::String(p.parse::<T![String]>()?)
83 } else if <T![Url]>::peek(p, c) {
84 Self::Url(p.parse::<T![Url]>()?)
85 } else if <T![Delim]>::peek(p, c) {
86 p.parse::<T![Delim]>().map(|delim| {
87 let mut rules = AssociatedWhitespaceRules::none();
89 if p.peek_n_with_skip(1, KindSet::COMMENTS) == Kind::Whitespace {
90 rules |= AssociatedWhitespaceRules::EnforceAfter;
91 } else {
92 rules |= AssociatedWhitespaceRules::BanAfter;
93 }
94 Self::Delim(delim.with_associated_whitespace(rules))
95 })?
96 } else if <T![:]>::peek(p, c) {
97 Self::Colon(p.parse::<T![:]>()?)
98 } else if <T![;]>::peek(p, c) {
99 Self::Semicolon(p.parse::<T![;]>()?)
100 } else if <T![,]>::peek(p, c) {
101 Self::Comma(p.parse::<T![,]>()?)
102 } else {
103 Err(Diagnostic::new(p.next(), Diagnostic::unexpected))?
104 })
105 }
106}
107
108impl<'a> ToCursors for ComponentValue<'a> {
109 fn to_cursors(&self, s: &mut impl CursorSink) {
110 match self {
111 Self::SimpleBlock(t) => ToCursors::to_cursors(t, s),
112 Self::Function(t) => ToCursors::to_cursors(t, s),
113 Self::Ident(t) => ToCursors::to_cursors(t, s),
114 Self::AtKeyword(t) => ToCursors::to_cursors(t, s),
115 Self::Hash(t) => ToCursors::to_cursors(t, s),
116 Self::String(t) => ToCursors::to_cursors(t, s),
117 Self::Url(t) => ToCursors::to_cursors(t, s),
118 Self::Delim(t) => ToCursors::to_cursors(t, s),
119 Self::Number(t) => ToCursors::to_cursors(t, s),
120 Self::Dimension(t) => ToCursors::to_cursors(t, s),
121 Self::Whitespace(t) => ToCursors::to_cursors(t, s),
122 Self::Colon(t) => ToCursors::to_cursors(t, s),
123 Self::Semicolon(t) => ToCursors::to_cursors(t, s),
124 Self::Comma(t) => ToCursors::to_cursors(t, s),
125 }
126 }
127}
128
129impl<'a> ToSpan for ComponentValue<'a> {
130 fn to_span(&self) -> Span {
131 match self {
132 Self::SimpleBlock(t) => t.to_span(),
133 Self::Function(t) => t.to_span(),
134 Self::Ident(t) => t.to_span(),
135 Self::AtKeyword(t) => t.to_span(),
136 Self::Hash(t) => t.to_span(),
137 Self::String(t) => t.to_span(),
138 Self::Url(t) => t.to_span(),
139 Self::Delim(t) => t.to_span(),
140 Self::Number(t) => t.to_span(),
141 Self::Dimension(t) => t.to_span(),
142 Self::Whitespace(t) => t.to_span(),
143 Self::Colon(t) => t.to_span(),
144 Self::Semicolon(t) => t.to_span(),
145 Self::Comma(t) => t.to_span(),
146 }
147 }
148}
149
150impl<'a> SemanticEq for ComponentValue<'a> {
151 fn semantic_eq(&self, other: &Self) -> bool {
152 match (self, other) {
153 (Self::SimpleBlock(a), Self::SimpleBlock(b)) => a.semantic_eq(b),
154 (Self::Function(a), Self::Function(b)) => a.semantic_eq(b),
155 (Self::Number(a), Self::Number(b)) => a.semantic_eq(b),
156 (Self::Dimension(a), Self::Dimension(b)) => a.semantic_eq(b),
157 (Self::Ident(a), Self::Ident(b)) => a.semantic_eq(b),
158 (Self::AtKeyword(a), Self::AtKeyword(b)) => a.semantic_eq(b),
159 (Self::Hash(a), Self::Hash(b)) => a.semantic_eq(b),
160 (Self::String(a), Self::String(b)) => a.semantic_eq(b),
161 (Self::Url(a), Self::Url(b)) => a.semantic_eq(b),
162 (Self::Delim(a), Self::Delim(b)) => a.semantic_eq(b),
163 (Self::Colon(a), Self::Colon(b)) => a.semantic_eq(b),
164 (Self::Semicolon(a), Self::Semicolon(b)) => a.semantic_eq(b),
165 (Self::Comma(a), Self::Comma(b)) => a.semantic_eq(b),
166 (Self::Whitespace(_), Self::Whitespace(_)) => true,
168 _ => false, }
170 }
171}
172
173#[cfg(test)]
174mod tests {
175 use super::*;
176 use crate::{EmptyAtomSet, test_helpers::*};
177
178 #[test]
179 fn size_test() {
180 assert_eq!(std::mem::size_of::<ComponentValue>(), 64);
181 }
182
183 #[test]
184 fn test_writes() {
185 assert_parse!(EmptyAtomSet::ATOMS, ComponentValue, "foo");
186 assert_parse!(EmptyAtomSet::ATOMS, ComponentValue, " ");
187 assert_parse!(EmptyAtomSet::ATOMS, ComponentValue, "{block}");
188 }
189}