css_ast 0.0.25

CSS Abstract Syntax Trees with visitable nodes and style value types.
Documentation
use super::prelude::*;

#[derive(IntoCursor, ToSpan, SemanticEq, ToCursors, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(transparent))]
#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(skip))]
#[derive(csskit_derives::NodeWithMetadata)]
pub struct CSSInt(T![Number]);

impl CSSInt {
	#[allow(non_upper_case_globals)]
	pub const Zero: CSSInt = CSSInt(<T![Number]>::ZERO);

	pub fn preserve_sign(self) -> Self {
		CSSInt(self.0.preserve_sign())
	}
}

impl From<CSSInt> for i32 {
	fn from(value: CSSInt) -> Self {
		value.0.into()
	}
}

impl From<CSSInt> for f32 {
	fn from(value: CSSInt) -> Self {
		value.0.into()
	}
}

impl ToNumberValue for CSSInt {
	fn to_number_value(&self) -> Option<f32> {
		Some(self.0.into())
	}
}

impl<'a> Peek<'a> for CSSInt {
	const PEEK_KINDSET: KindSet = KindSet::new(&[Kind::Number]);

	#[inline(always)]
	fn peek<I>(p: &Parser<'a, I>, c: Cursor) -> bool
	where
		I: Iterator<Item = Cursor> + Clone,
	{
		<T![Number]>::peek(p, c) && c.token().is_int()
	}
}

impl<'a> Parse<'a> for CSSInt {
	fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
	where
		I: Iterator<Item = Cursor> + Clone,
	{
		if p.peek::<Self>() {
			p.parse::<T![Number]>().map(Self)
		} else {
			Err(Diagnostic::new(p.next(), Diagnostic::unexpected))?
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::CssAtomSet;
	use css_parse::assert_parse;

	#[test]
	fn size_test() {
		assert_eq!(std::mem::size_of::<CSSInt>(), 12);
	}

	#[test]
	fn test_writes() {
		assert_parse!(CssAtomSet::ATOMS, CSSInt, "0");
		assert_parse!(CssAtomSet::ATOMS, CSSInt, "999999");
	}
}