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