Skip to main content

css_parse/traits/
to_number_value.rs

1pub trait ToNumberValue {
2	fn to_number_value(&self) -> Option<f32>;
3
4	fn to_int_value(&self) -> Option<i32> {
5		self.to_number_value().map(|f| f as i32)
6	}
7}
8
9impl<T: ToNumberValue> ToNumberValue for Option<T> {
10	fn to_number_value(&self) -> Option<f32> {
11		self.as_ref().and_then(|t| t.to_number_value())
12	}
13}
14
15/// Returns the canonical (unit-normalised) numeric value for range validation.
16///
17/// Unlike `ToNumberValue` which returns the raw token value, this returns the value in a unit suitable for range
18/// comparison. For example, `Angle` returns degrees regardless of whether the token was `deg`, `rad`, `grad`, or
19/// `turn`.
20///
21/// For plain numeric types (integers, numbers, lengths, percentages) the value is identical to the raw token value.
22pub trait ToNormalisedValue {
23	fn to_normalised_value(&self) -> Option<f32>;
24}
25
26impl<T: ToNormalisedValue> ToNormalisedValue for Option<T> {
27	fn to_normalised_value(&self) -> Option<f32> {
28		self.as_ref().and_then(|t| t.to_normalised_value())
29	}
30}