1use css_parse::{Build, Cursor, Parser, Peek, T, ToNumberValue};
2use csskit_derives::{IntoCursor, ToCursors, Visitable};
3
4#[derive(IntoCursor, ToCursors, Visitable, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(transparent))]
6#[visit(skip)]
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
14impl From<CSSInt> for i32 {
15 fn from(value: CSSInt) -> Self {
16 value.0.into()
17 }
18}
19
20impl From<CSSInt> for f32 {
21 fn from(value: CSSInt) -> Self {
22 value.0.into()
23 }
24}
25
26impl ToNumberValue for CSSInt {
27 fn to_number_value(&self) -> Option<f32> {
28 Some(self.0.into())
29 }
30}
31
32impl<'a> Peek<'a> for CSSInt {
33 fn peek(p: &Parser<'a>, c: Cursor) -> bool {
34 <T![Number]>::peek(p, c) && c.token().is_int()
35 }
36}
37
38impl<'a> Build<'a> for CSSInt {
39 fn build(p: &Parser<'a>, c: Cursor) -> Self {
40 Self(<T![Number]>::build(p, c))
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47 use css_parse::assert_parse;
48
49 #[test]
50 fn size_test() {
51 assert_eq!(std::mem::size_of::<CSSInt>(), 12);
52 }
53
54 #[test]
55 fn test_writes() {
56 assert_parse!(CSSInt, "0");
57 assert_parse!(CSSInt, "999999");
58 }
59}