style/typed_om/
numeric_values.rs1use crate::derives::*;
8use crate::values::specified::{
9 NoCalcAngle, NoCalcLength, NoCalcNumber, NoCalcPercentage, NoCalcTime,
10};
11use crate::values::CSSFloat;
12use cssparser::match_ignore_ascii_case;
13use style_traits::ParsingMode;
14
15#[derive(Clone, ToTyped)]
17#[repr(u8)]
18pub enum NoCalcNumeric {
19 Length(NoCalcLength),
23
24 Angle(NoCalcAngle),
28
29 Time(NoCalcTime),
33
34 Number(NoCalcNumber),
38
39 Percentage(NoCalcPercentage),
43 }
45
46impl NoCalcNumeric {
47 pub fn unitless_value(&self) -> CSSFloat {
49 match *self {
50 Self::Length(v) => v.unitless_value(),
51 Self::Angle(v) => v.unitless_value(),
52 Self::Time(v) => v.unitless_value(),
53 Self::Number(v) => v.get(),
54 Self::Percentage(v) => v.get(),
55 }
56 }
57
58 pub fn unit(&self) -> &'static str {
64 match *self {
65 Self::Length(v) => v.unit(),
66 Self::Angle(v) => v.unit(),
67 Self::Time(v) => v.unit(),
68 Self::Number(v) => v.unit(),
69 Self::Percentage(v) => v.unit(),
70 }
71 }
72
73 pub fn canonical_unit(&self) -> Option<&'static str> {
78 match *self {
79 Self::Length(v) => v.canonical_unit(),
80 Self::Angle(v) => v.canonical_unit(),
81 Self::Time(v) => v.canonical_unit(),
82 Self::Number(v) => v.canonical_unit(),
83 Self::Percentage(v) => v.canonical_unit(),
84 }
85 }
86
87 pub fn to(&self, unit: &str) -> Result<Self, ()> {
92 match self {
93 Self::Length(v) => Ok(Self::Length(v.to(unit)?)),
94 Self::Angle(v) => Ok(Self::Angle(v.to(unit)?)),
95 Self::Time(v) => Ok(Self::Time(v.to(unit)?)),
96 Self::Number(v) => Ok(Self::Number(v.to(unit)?)),
97 Self::Percentage(v) => Ok(Self::Percentage(v.to(unit)?)),
98 }
99 }
100
101 pub fn parse_unit_value(value: CSSFloat, unit: &str) -> Result<Self, ()> {
103 if let Ok(length) = NoCalcLength::parse_dimension_with_flags(
104 ParsingMode::DEFAULT,
105 false,
106 value,
107 unit,
108 ) {
109 return Ok(NoCalcNumeric::Length(length));
110 }
111
112 if let Ok(angle) = NoCalcAngle::parse_dimension(value, unit) {
113 return Ok(NoCalcNumeric::Angle(angle));
114 }
115
116 if let Ok(time) = NoCalcTime::parse_dimension(value, unit) {
117 return Ok(NoCalcNumeric::Time(time));
118 }
119
120 match_ignore_ascii_case! { unit,
121 "number" => Ok(NoCalcNumeric::Number(NoCalcNumber::new(value))),
122 "percent" => Ok(NoCalcNumeric::Percentage(NoCalcPercentage::new(value))),
123 _ => Err(()),
124 }
125
126 }
128}