Skip to main content

css_ast/types/
position_area.rs

1use super::prelude::*;
2
3/// <https://drafts.csswg.org/css-anchor-position-1/#typedef-position-area>
4///
5/// ```text,ignore
6/// <position-area> = [
7///   [ left | center | right | span-left | span-right
8///   | x-start | x-end | span-x-start | span-x-end
9///   | x-self-start | x-self-end | span-x-self-start | span-x-self-end
10///   | span-all ]
11///   ||
12///   [ top | center | bottom | span-top | span-bottom
13///   | y-start | y-end | span-y-start | span-y-end
14///   | y-self-start | y-self-end | span-y-self-start | span-y-self-end
15///   | span-all ]
16/// |
17///   [ block-start | center | block-end | span-block-start | span-block-end | span-all ]
18///   ||
19///   [ inline-start | center | inline-end | span-inline-start | span-inline-end
20///   | span-all ]
21/// |
22///   [ self-block-start | center | self-block-end | span-self-block-start
23///   | span-self-block-end | span-all ]
24///   ||
25///   [ self-inline-start | center | self-inline-end | span-self-inline-start
26///   | span-self-inline-end | span-all ]
27/// |
28///   [ start | center | end | span-start | span-end | span-all ]{1,2}
29/// |
30///   [ self-start | center | self-end | span-self-start | span-self-end | span-all ]{1,2}
31/// ]
32/// ```
33#[derive(ToCursors, ToSpan, SemanticEq, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
34#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
35#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
36#[derive(csskit_derives::NodeWithMetadata)]
37pub enum PositionArea {
38	Physical(Option<PositionAreaPhsyicalHorizontal>, Option<PositionAreaPhsyicalVertical>),
39	Logical(Option<PositionAreaBlock>, Option<PositionAreaInline>),
40	SelfLogical(Option<PositionAreaSelfBlock>, Option<PositionAreaSelfInline>),
41	Position(PositionAreaPosition, Option<PositionAreaPosition>),
42	SelfPosition(PositionAreaSelfPosition, Option<PositionAreaSelfPosition>),
43}
44
45impl<'a> Peek<'a> for PositionArea {
46	const PEEK_KINDSET: KindSet = PositionAreaPhsyicalVertical::PEEK_KINDSET
47		.combine(PositionAreaPhsyicalHorizontal::PEEK_KINDSET)
48		.combine(PositionAreaBlock::PEEK_KINDSET)
49		.combine(PositionAreaInline::PEEK_KINDSET)
50		.combine(PositionAreaSelfBlock::PEEK_KINDSET)
51		.combine(PositionAreaSelfInline::PEEK_KINDSET)
52		.combine(PositionAreaPosition::PEEK_KINDSET)
53		.combine(PositionAreaSelfPosition::PEEK_KINDSET);
54
55	#[inline(always)]
56	fn peek<I>(p: &Parser<'a, I>, c: Cursor) -> bool
57	where
58		I: Iterator<Item = Cursor> + Clone,
59	{
60		PositionAreaPhsyicalVertical::peek(p, c)
61			|| PositionAreaPhsyicalHorizontal::peek(p, c)
62			|| PositionAreaBlock::peek(p, c)
63			|| PositionAreaInline::peek(p, c)
64			|| PositionAreaSelfBlock::peek(p, c)
65			|| PositionAreaSelfInline::peek(p, c)
66			|| PositionAreaPosition::peek(p, c)
67			|| PositionAreaSelfPosition::peek(p, c)
68	}
69}
70
71impl<'a> Parse<'a> for PositionArea {
72	fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
73	where
74		I: Iterator<Item = Cursor> + Clone,
75	{
76		if let Some(first) = p.parse_if_peek::<PositionAreaPosition>()? {
77			Ok(Self::Position(first, p.parse_if_peek::<PositionAreaPosition>()?))
78		} else if let Some(first) = p.parse_if_peek::<PositionAreaSelfPosition>()? {
79			Ok(Self::SelfPosition(first, p.parse_if_peek::<PositionAreaSelfPosition>()?))
80		} else if let Some(block) = p.parse_if_peek::<PositionAreaBlock>()? {
81			Ok(Self::Logical(Some(block), p.parse_if_peek::<PositionAreaInline>()?))
82		} else if let Some(inline) = p.parse_if_peek::<PositionAreaInline>()? {
83			Ok(Self::Logical(p.parse_if_peek::<PositionAreaBlock>()?, Some(inline)))
84		} else if let Some(block) = p.parse_if_peek::<PositionAreaSelfBlock>()? {
85			Ok(Self::SelfLogical(Some(block), p.parse_if_peek::<PositionAreaSelfInline>()?))
86		} else if let Some(inline) = p.parse_if_peek::<PositionAreaSelfInline>()? {
87			Ok(Self::SelfLogical(p.parse_if_peek::<PositionAreaSelfBlock>()?, Some(inline)))
88		} else if let Some(horizontal) = p.parse_if_peek::<PositionAreaPhsyicalHorizontal>()? {
89			Ok(Self::Physical(Some(horizontal), p.parse_if_peek::<PositionAreaPhsyicalVertical>()?))
90		} else if let Some(vertical) = p.parse_if_peek::<PositionAreaPhsyicalVertical>()? {
91			Ok(Self::Physical(p.parse_if_peek::<PositionAreaPhsyicalHorizontal>()?, Some(vertical)))
92		} else {
93			Err(Diagnostic::new(p.next(), Diagnostic::unexpected))?
94		}
95	}
96}
97
98#[derive(
99	Parse, Peek, IntoCursor, ToSpan, SemanticEq, ToCursors, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash,
100)]
101#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
102pub enum PositionAreaPhsyicalHorizontal {
103	#[atom(CssAtomSet::Left)]
104	Left(T![Ident]),
105	#[atom(CssAtomSet::Center)]
106	Center(T![Ident]),
107	#[atom(CssAtomSet::Right)]
108	Right(T![Ident]),
109	#[atom(CssAtomSet::SpanLeft)]
110	SpanLeft(T![Ident]),
111	#[atom(CssAtomSet::SpanRight)]
112	SpanRight(T![Ident]),
113	#[atom(CssAtomSet::XStart)]
114	XStart(T![Ident]),
115	#[atom(CssAtomSet::XEnd)]
116	XEnd(T![Ident]),
117	#[atom(CssAtomSet::SpanXStart)]
118	SpanXStart(T![Ident]),
119	#[atom(CssAtomSet::SpanXEnd)]
120	SpanXEnd(T![Ident]),
121	#[atom(CssAtomSet::XSelfStart)]
122	XSelfStart(T![Ident]),
123	#[atom(CssAtomSet::XSelfEnd)]
124	XSelfEnd(T![Ident]),
125	#[atom(CssAtomSet::SpanXSelfStart)]
126	SpanXSelfStart(T![Ident]),
127	#[atom(CssAtomSet::SpanXSelfEnd)]
128	SpanXSelfEnd(T![Ident]),
129	#[atom(CssAtomSet::SpanAll)]
130	SpanAll(T![Ident]),
131}
132
133#[derive(
134	Parse, Peek, IntoCursor, ToSpan, SemanticEq, ToCursors, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash,
135)]
136#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
137pub enum PositionAreaPhsyicalVertical {
138	#[atom(CssAtomSet::Top)]
139	Top(T![Ident]),
140	#[atom(CssAtomSet::Center)]
141	Center(T![Ident]),
142	#[atom(CssAtomSet::Bottom)]
143	Bottom(T![Ident]),
144	#[atom(CssAtomSet::SpanTop)]
145	SpanTop(T![Ident]),
146	#[atom(CssAtomSet::SpanBottom)]
147	SpanBottom(T![Ident]),
148	#[atom(CssAtomSet::YStart)]
149	YStart(T![Ident]),
150	#[atom(CssAtomSet::YEnd)]
151	YEnd(T![Ident]),
152	#[atom(CssAtomSet::SpanYStart)]
153	SpanYStart(T![Ident]),
154	#[atom(CssAtomSet::SpanYEnd)]
155	SpanYEnd(T![Ident]),
156	#[atom(CssAtomSet::YSelfStart)]
157	YSelfStart(T![Ident]),
158	#[atom(CssAtomSet::YSelfEnd)]
159	YSelfEnd(T![Ident]),
160	#[atom(CssAtomSet::SpanYSelfStart)]
161	SpanYSelfStart(T![Ident]),
162	#[atom(CssAtomSet::SpanYSelfEnd)]
163	SpanYSelfEnd(T![Ident]),
164	#[atom(CssAtomSet::SpanAll)]
165	SpanAll(T![Ident]),
166}
167
168#[derive(
169	Parse, Peek, IntoCursor, ToSpan, SemanticEq, ToCursors, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash,
170)]
171#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
172pub enum PositionAreaBlock {
173	#[atom(CssAtomSet::BlockStart)]
174	BlockStart(T![Ident]),
175	#[atom(CssAtomSet::Center)]
176	Center(T![Ident]),
177	#[atom(CssAtomSet::BlockEnd)]
178	BlockEnd(T![Ident]),
179	#[atom(CssAtomSet::SpanBlockStart)]
180	SpanBlockStart(T![Ident]),
181	#[atom(CssAtomSet::SpanBlockEnd)]
182	SpanBlockEnd(T![Ident]),
183	#[atom(CssAtomSet::SpanAll)]
184	SpanAll(T![Ident]),
185}
186
187#[derive(
188	Parse, Peek, IntoCursor, ToSpan, SemanticEq, ToCursors, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash,
189)]
190#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
191pub enum PositionAreaInline {
192	#[atom(CssAtomSet::InlineStart)]
193	InlineStart(T![Ident]),
194	#[atom(CssAtomSet::Center)]
195	Center(T![Ident]),
196	#[atom(CssAtomSet::InlineEnd)]
197	InlineEnd(T![Ident]),
198	#[atom(CssAtomSet::SpanInlineStart)]
199	SpanInlineStart(T![Ident]),
200	#[atom(CssAtomSet::SpanInlineEnd)]
201	SpanInlineEnd(T![Ident]),
202	#[atom(CssAtomSet::SpanAll)]
203	SpanAll(T![Ident]),
204}
205
206#[derive(
207	Parse, Peek, IntoCursor, ToSpan, SemanticEq, ToCursors, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash,
208)]
209#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
210pub enum PositionAreaSelfBlock {
211	#[atom(CssAtomSet::SelfBlockStart)]
212	SelfBlockStart(T![Ident]),
213	#[atom(CssAtomSet::Center)]
214	Center(T![Ident]),
215	#[atom(CssAtomSet::SelfBlockEnd)]
216	SelfBlockEnd(T![Ident]),
217	#[atom(CssAtomSet::SpanSelfBlockStart)]
218	SpanSelfBlockStart(T![Ident]),
219	#[atom(CssAtomSet::SpanSelfBlockEnd)]
220	SpanSelfBlockEnd(T![Ident]),
221	#[atom(CssAtomSet::SpanAll)]
222	SpanAll(T![Ident]),
223}
224
225#[derive(
226	Parse, Peek, IntoCursor, ToSpan, SemanticEq, ToCursors, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash,
227)]
228#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
229pub enum PositionAreaSelfInline {
230	#[atom(CssAtomSet::SelfInlineStart)]
231	SelfInlineStart(T![Ident]),
232	#[atom(CssAtomSet::Center)]
233	Center(T![Ident]),
234	#[atom(CssAtomSet::SelfInlineEnd)]
235	SelfInlineEnd(T![Ident]),
236	#[atom(CssAtomSet::SpanSelfInlineStart)]
237	SpanSelfInlineStart(T![Ident]),
238	#[atom(CssAtomSet::SpanSelfInlineEnd)]
239	SpanSelfInlineEnd(T![Ident]),
240	#[atom(CssAtomSet::SpanAll)]
241	SpanAll(T![Ident]),
242}
243
244#[derive(
245	Parse, Peek, IntoCursor, ToSpan, SemanticEq, ToCursors, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash,
246)]
247#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
248pub enum PositionAreaPosition {
249	#[atom(CssAtomSet::Start)]
250	Start(T![Ident]),
251	#[atom(CssAtomSet::Center)]
252	Center(T![Ident]),
253	#[atom(CssAtomSet::End)]
254	End(T![Ident]),
255	#[atom(CssAtomSet::SpanStart)]
256	SpanStart(T![Ident]),
257	#[atom(CssAtomSet::SpanEnd)]
258	SpanEnd(T![Ident]),
259	#[atom(CssAtomSet::SpanAll)]
260	SpanAll(T![Ident]),
261}
262
263#[derive(
264	Parse, Peek, IntoCursor, ToSpan, SemanticEq, ToCursors, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash,
265)]
266#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
267pub enum PositionAreaSelfPosition {
268	#[atom(CssAtomSet::SelfStart)]
269	SelfStart(T![Ident]),
270	#[atom(CssAtomSet::Center)]
271	Center(T![Ident]),
272	#[atom(CssAtomSet::SelfEnd)]
273	SelfEnd(T![Ident]),
274	#[atom(CssAtomSet::SpanSelfStart)]
275	SpanSelfStart(T![Ident]),
276	#[atom(CssAtomSet::SpanSelfEnd)]
277	SpanSelfEnd(T![Ident]),
278	#[atom(CssAtomSet::SpanAll)]
279	SpanAll(T![Ident]),
280}