css_ast/units/
custom.rs

1use super::prelude::*;
2
3#[derive(ToCursors, IntoCursor, 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	fn peek<I>(p: &Parser<'a, I>, c: Cursor) -> bool
15	where
16		I: Iterator<Item = Cursor> + Clone,
17	{
18		<T![Dimension]>::peek(p, c)
19			&& p.to_source_cursor(c).source()[c.token().numeric_len() as usize..].starts_with("--")
20	}
21}
22
23impl<'a> Parse<'a> for CustomDimension {
24	fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
25	where
26		I: Iterator<Item = Cursor> + Clone,
27	{
28		if p.peek::<Self>() {
29			p.parse::<T![Dimension]>().map(Self)
30		} else {
31			Err(Diagnostic::new(p.next(), Diagnostic::unexpected))?
32		}
33	}
34}
35
36#[cfg(test)]
37mod tests {
38	use super::*;
39	use crate::CssAtomSet;
40	use css_parse::assert_parse;
41
42	#[test]
43	fn size_test() {
44		assert_eq!(std::mem::size_of::<CustomDimension>(), 12);
45	}
46
47	#[test]
48	fn test_writes() {
49		assert_parse!(CssAtomSet::ATOMS, CustomDimension, "1--foo");
50	}
51}