Skip to main content

css_ast/units/
custom.rs

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