css_ast/units/
custom.rs

1use css_parse::{Build, Cursor, DimensionUnit, Parser, Peek, T};
2use csskit_derives::{IntoCursor, ToCursors};
3
4#[derive(ToCursors, IntoCursor, 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	fn peek(p: &Parser<'a>, c: Cursor) -> bool {
16		<T![Dimension]>::peek(p, c) && c == DimensionUnit::Unknown && p.parse_str(c).starts_with("--")
17	}
18}
19
20impl<'a> Build<'a> for CustomDimension {
21	fn build(p: &Parser<'a>, c: Cursor) -> Self {
22		Self(<T![Dimension]>::build(p, c))
23	}
24}
25
26#[cfg(test)]
27mod tests {
28	use super::*;
29	use css_parse::assert_parse;
30
31	#[test]
32	fn size_test() {
33		assert_eq!(std::mem::size_of::<CustomDimension>(), 12);
34	}
35
36	#[test]
37	fn test_writes() {
38		assert_parse!(CustomDimension, "1--foo");
39	}
40}