Skip to main content

css_ast/types/
ratio.rs

1use super::prelude::*;
2use crate::units::CSSInt;
3
4/// <https://drafts.csswg.org/css-values-4/#ratios>
5///
6/// ```text,ignore
7/// <ratio> = <number [0,∞]> [ / <number [0,∞]> ]?
8/// ```
9#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
11#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
12#[derive(csskit_derives::NodeWithMetadata)]
13pub struct Ratio {
14	pub numerator: CSSInt,
15	pub slash: Option<T![/]>,
16	pub denominator: Option<CSSInt>,
17}
18
19#[cfg(test)]
20mod tests {
21	use super::*;
22	use crate::CssAtomSet;
23	use css_parse::{assert_parse, assert_parse_error};
24
25	#[test]
26	fn size_test() {
27		assert_eq!(std::mem::size_of::<Ratio>(), 44);
28	}
29
30	#[test]
31	fn test_writes() {
32		assert_parse!(CssAtomSet::ATOMS, Ratio, "1/1");
33		assert_parse!(CssAtomSet::ATOMS, Ratio, "5/3");
34		assert_parse!(CssAtomSet::ATOMS, Ratio, "5");
35	}
36
37	#[test]
38	fn test_errors() {
39		assert_parse_error!(CssAtomSet::ATOMS, Ratio, "5 : 3");
40		assert_parse_error!(CssAtomSet::ATOMS, Ratio, "5 / 1 / 1");
41	}
42
43	// #[cfg(feature = "serde")]
44	// #[test]
45	// fn test_serializes() {
46	// 	assert_json!(Ratio, "5/3", {
47	// 		"node": [5, 3],
48	// 		"start": 0,
49	// 		"end": 5
50	// 	});
51	// }
52}