1use super::prelude::*;
2
3#[derive(IntoCursor, ToSpan, SemanticEq, 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 const PEEK_KINDSET: KindSet = KindSet::new(&[Kind::Number]);
38
39 #[inline(always)]
40 fn peek<I>(p: &Parser<'a, I>, c: Cursor) -> bool
41 where
42 I: Iterator<Item = Cursor> + Clone,
43 {
44 <T![Number]>::peek(p, c) && c.token().is_int()
45 }
46}
47
48impl<'a> Parse<'a> for CSSInt {
49 fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
50 where
51 I: Iterator<Item = Cursor> + Clone,
52 {
53 if p.peek::<Self>() {
54 p.parse::<T![Number]>().map(Self)
55 } else {
56 Err(Diagnostic::new(p.next(), Diagnostic::unexpected))?
57 }
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64 use crate::CssAtomSet;
65 use css_parse::assert_parse;
66
67 #[test]
68 fn size_test() {
69 assert_eq!(std::mem::size_of::<CSSInt>(), 12);
70 }
71
72 #[test]
73 fn test_writes() {
74 assert_parse!(CssAtomSet::ATOMS, CSSInt, "0");
75 assert_parse!(CssAtomSet::ATOMS, CSSInt, "999999");
76 }
77}