Skip to main content

css_ast/units/
custom.rs

1use super::prelude::*;
2
3#[node]
4#[derive(ToCursors, IntoCursor, ToSpan, SemanticEq, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
6pub struct CustomDimension(T![Dimension]);
7
8impl From<CustomDimension> for f32 {
9	fn from(custom: CustomDimension) -> Self {
10		custom.0.into()
11	}
12}
13
14impl<'a> Peek<'a> for CustomDimension {
15	const PEEK_KINDSET: KindSet = KindSet::new(&[Kind::Dimension]);
16
17	#[inline(always)]
18	fn peek<I>(p: &Parser<'a, I>, c: Cursor) -> bool
19	where
20		I: Iterator<Item = Cursor> + Clone,
21	{
22		<T![Dimension]>::peek(p, c)
23			&& p.to_source_cursor(c).source()[c.token().numeric_len() as usize..].starts_with("--")
24	}
25}
26
27impl<'a> Parse<'a> for CustomDimension {
28	fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
29	where
30		I: Iterator<Item = Cursor> + Clone,
31	{
32		if p.peek::<Self>() {
33			p.parse::<T![Dimension]>().map(Self)
34		} else {
35			Err(Diagnostic::new(p.next(), Diagnostic::unexpected))?
36		}
37	}
38}
39
40#[cfg(test)]
41mod tests {
42	use super::*;
43	use crate::CssAtomSet;
44	use css_parse::assert_parse;
45
46	#[test]
47	fn test_writes() {
48		assert_parse!(CssAtomSet::ATOMS, CustomDimension, "1--foo");
49	}
50}