css_ast/units/
int.rs

1use super::prelude::*;
2
3#[derive(IntoCursor, ToCursors, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(transparent))]
5#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(skip))]
6pub struct CSSInt(T![Number]);
7
8impl CSSInt {
9	#[allow(non_upper_case_globals)]
10	pub const Zero: CSSInt = CSSInt(<T![Number]>::ZERO);
11}
12
13impl From<CSSInt> for i32 {
14	fn from(value: CSSInt) -> Self {
15		value.0.into()
16	}
17}
18
19impl From<CSSInt> for f32 {
20	fn from(value: CSSInt) -> Self {
21		value.0.into()
22	}
23}
24
25impl ToNumberValue for CSSInt {
26	fn to_number_value(&self) -> Option<f32> {
27		Some(self.0.into())
28	}
29}
30
31impl<'a> Peek<'a> for CSSInt {
32	fn peek<I>(p: &Parser<'a, I>, c: Cursor) -> bool
33	where
34		I: Iterator<Item = Cursor> + Clone,
35	{
36		<T![Number]>::peek(p, c) && c.token().is_int()
37	}
38}
39
40impl<'a> Parse<'a> for CSSInt {
41	fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
42	where
43		I: Iterator<Item = Cursor> + Clone,
44	{
45		if p.peek::<Self>() {
46			p.parse::<T![Number]>().map(Self)
47		} else {
48			Err(Diagnostic::new(p.next(), Diagnostic::unexpected))?
49		}
50	}
51}
52
53#[cfg(test)]
54mod tests {
55	use super::*;
56	use crate::CssAtomSet;
57	use css_parse::assert_parse;
58
59	#[test]
60	fn size_test() {
61		assert_eq!(std::mem::size_of::<CSSInt>(), 12);
62	}
63
64	#[test]
65	fn test_writes() {
66		assert_parse!(CssAtomSet::ATOMS, CSSInt, "0");
67		assert_parse!(CssAtomSet::ATOMS, CSSInt, "999999");
68	}
69}