/rust/registry/src/index.crates.io-1949cf8c6b5b557f/lightningcss-1.0.0-alpha.72/src/values/number.rs
Line | Count | Source |
1 | | //! CSS number values. |
2 | | |
3 | | use super::angle::impl_try_from_angle; |
4 | | use super::calc::Calc; |
5 | | use crate::error::{ParserError, PrinterError}; |
6 | | use crate::printer::Printer; |
7 | | use crate::traits::private::AddInternal; |
8 | | use crate::traits::{Map, Op, Parse, Sign, ToCss, Zero}; |
9 | | use cssparser::*; |
10 | | |
11 | | /// A CSS [`<number>`](https://www.w3.org/TR/css-values-4/#numbers) value. |
12 | | /// |
13 | | /// Numbers may be explicit or computed by `calc()`, but are always stored and serialized |
14 | | /// as their computed value. |
15 | | pub type CSSNumber = f32; |
16 | | |
17 | | impl<'i> Parse<'i> for CSSNumber { |
18 | 699k | fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> { |
19 | 699k | match input.try_parse(Calc::parse) { |
20 | 0 | Ok(Calc::Value(v)) => return Ok(*v), |
21 | 11 | Ok(Calc::Number(n)) => return Ok(n), |
22 | | // Numbers are always compatible, so they will always compute to a value. |
23 | 0 | Ok(_) => return Err(input.new_custom_error(ParserError::InvalidValue)), |
24 | 699k | _ => {} |
25 | | } |
26 | | |
27 | 699k | let number = input.expect_number()?; |
28 | 110k | Ok(number) |
29 | 699k | } |
30 | | } |
31 | | |
32 | | impl ToCss for CSSNumber { |
33 | 0 | fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> |
34 | 0 | where |
35 | 0 | W: std::fmt::Write, |
36 | | { |
37 | 0 | let number = *self; |
38 | 0 | if number != 0.0 && number.abs() < 1.0 { |
39 | 0 | let mut s = String::new(); |
40 | 0 | cssparser::ToCss::to_css(self, &mut s)?; |
41 | 0 | if number < 0.0 { |
42 | 0 | dest.write_char('-')?; |
43 | 0 | dest.write_str(s.trim_start_matches("-").trim_start_matches("0")) |
44 | | } else { |
45 | 0 | dest.write_str(s.trim_start_matches('0')) |
46 | | } |
47 | | } else { |
48 | 0 | cssparser::ToCss::to_css(self, dest)?; |
49 | 0 | Ok(()) |
50 | | } |
51 | 0 | } |
52 | | } |
53 | | |
54 | | impl std::convert::Into<Calc<CSSNumber>> for CSSNumber { |
55 | 0 | fn into(self) -> Calc<CSSNumber> { |
56 | 0 | Calc::Value(Box::new(self)) |
57 | 0 | } |
58 | | } |
59 | | |
60 | | impl std::convert::From<Calc<CSSNumber>> for CSSNumber { |
61 | 0 | fn from(calc: Calc<CSSNumber>) -> CSSNumber { |
62 | 0 | match calc { |
63 | 0 | Calc::Value(v) => *v, |
64 | 0 | Calc::Number(n) => n, |
65 | 0 | _ => unreachable!(), |
66 | | } |
67 | 0 | } |
68 | | } |
69 | | |
70 | | impl AddInternal for CSSNumber { |
71 | 0 | fn add(self, other: Self) -> Self { |
72 | 0 | self + other |
73 | 0 | } |
74 | | } |
75 | | |
76 | | impl Op for CSSNumber { |
77 | 0 | fn op<F: FnOnce(f32, f32) -> f32>(&self, to: &Self, op: F) -> Self { |
78 | 0 | op(*self, *to) |
79 | 0 | } Unexecuted instantiation: <f32 as lightningcss::traits::Op>::op::<<lightningcss::values::calc::Calc<f32>>::parse_with<<lightningcss::values::calc::Calc<f32> as lightningcss::traits::Parse>::parse::{closure#0}>::{closure#4}::{closure#0}>Unexecuted instantiation: <f32 as lightningcss::traits::Op>::op::<<lightningcss::values::calc::Calc<f32>>::parse_with<<lightningcss::values::color::RelativeComponentParser as cssparser_color::ColorParser>::parse_number::{closure#1}>::{closure#4}::{closure#0}>Unexecuted instantiation: <f32 as lightningcss::traits::Op>::op::<<lightningcss::values::calc::Calc<f32>>::parse_hypot::{closure#1}::{closure#0}>Unexecuted instantiation: <f32 as lightningcss::traits::Op>::op::<<f32>::hypot> Unexecuted instantiation: <f32 as lightningcss::traits::Op>::op::<lightningcss::values::calc::modulo> Unexecuted instantiation: <f32 as lightningcss::traits::Op>::op::<<f32 as core::ops::arith::Rem>::rem> |
80 | | |
81 | 0 | fn op_to<T, F: FnOnce(f32, f32) -> T>(&self, rhs: &Self, op: F) -> T { |
82 | 0 | op(*self, *rhs) |
83 | 0 | } Unexecuted instantiation: <f32 as lightningcss::traits::Op>::op_to::<lightningcss::values::angle::Angle, <lightningcss::values::calc::Calc<f32>>::parse_atan2_args<<lightningcss::values::calc::Calc<f32>>::parse_atan2<<lightningcss::values::calc::Calc<f32> as lightningcss::traits::Parse>::parse::{closure#0}>::{closure#4}>::{closure#0}>Unexecuted instantiation: <f32 as lightningcss::traits::Op>::op_to::<lightningcss::values::angle::Angle, <lightningcss::values::calc::Calc<f32>>::parse_atan2_args<<lightningcss::values::calc::Calc<f32>>::parse_atan2<<lightningcss::values::color::RelativeComponentParser as cssparser_color::ColorParser>::parse_number::{closure#1}>::{closure#4}>::{closure#0}>Unexecuted instantiation: <f32 as lightningcss::traits::Op>::op_to::<lightningcss::values::angle::Angle, <lightningcss::values::calc::Calc<f32>>::parse_atan2_args<<lightningcss::values::calc::Calc<lightningcss::values::angle::Angle>>::parse_atan2<<lightningcss::values::color::RelativeComponentParser as cssparser_color::ColorParser>::parse_angle_or_number::{closure#1}::{closure#0}>::{closure#4}>::{closure#0}>Unexecuted instantiation: <f32 as lightningcss::traits::Op>::op_to::<lightningcss::values::angle::Angle, <lightningcss::values::calc::Calc<f32>>::parse_atan2_args<<lightningcss::values::calc::Calc<lightningcss::values::angle::Angle>>::parse_atan2<<lightningcss::values::calc::Calc<lightningcss::values::angle::Angle> as lightningcss::traits::Parse>::parse::{closure#0}>::{closure#4}>::{closure#0}>Unexecuted instantiation: <f32 as lightningcss::traits::Op>::op_to::<lightningcss::values::angle::Angle, <lightningcss::values::calc::Calc<f32>>::parse_atan2_args<<lightningcss::values::calc::Calc<lightningcss::values::percentage::DimensionPercentage<lightningcss::values::angle::Angle>>>::parse_atan2<<lightningcss::values::calc::Calc<lightningcss::values::percentage::DimensionPercentage<lightningcss::values::angle::Angle>> as lightningcss::traits::Parse>::parse::{closure#0}>::{closure#4}>::{closure#0}>Unexecuted instantiation: <f32 as lightningcss::traits::Op>::op_to::<lightningcss::values::angle::Angle, <lightningcss::values::calc::Calc<f32>>::parse_atan2_args<<lightningcss::values::calc::Calc<lightningcss::values::percentage::DimensionPercentage<lightningcss::values::length::LengthValue>>>::parse_atan2<<lightningcss::values::calc::Calc<lightningcss::values::percentage::DimensionPercentage<lightningcss::values::length::LengthValue>> as lightningcss::traits::Parse>::parse::{closure#0}>::{closure#4}>::{closure#0}>Unexecuted instantiation: <f32 as lightningcss::traits::Op>::op_to::<lightningcss::values::angle::Angle, <lightningcss::values::calc::Calc<f32>>::parse_atan2_args<<lightningcss::values::calc::Calc<lightningcss::values::percentage::Percentage>>::parse_atan2<<lightningcss::values::color::RelativeComponentParser as cssparser_color::ColorParser>::parse_percentage::{closure#1}::{closure#0}>::{closure#4}>::{closure#0}>Unexecuted instantiation: <f32 as lightningcss::traits::Op>::op_to::<lightningcss::values::angle::Angle, <lightningcss::values::calc::Calc<f32>>::parse_atan2_args<<lightningcss::values::calc::Calc<lightningcss::values::percentage::Percentage>>::parse_atan2<<lightningcss::values::color::RelativeComponentParser as cssparser_color::ColorParser>::parse_number_or_percentage::{closure#1}::{closure#0}>::{closure#4}>::{closure#0}>Unexecuted instantiation: <f32 as lightningcss::traits::Op>::op_to::<lightningcss::values::angle::Angle, <lightningcss::values::calc::Calc<f32>>::parse_atan2_args<<lightningcss::values::calc::Calc<lightningcss::values::percentage::Percentage>>::parse_atan2<<lightningcss::values::calc::Calc<lightningcss::values::percentage::Percentage> as lightningcss::traits::Parse>::parse::{closure#0}>::{closure#4}>::{closure#0}>Unexecuted instantiation: <f32 as lightningcss::traits::Op>::op_to::<lightningcss::values::angle::Angle, <lightningcss::values::calc::Calc<f32>>::parse_atan2_args<<lightningcss::values::calc::Calc<lightningcss::values::time::Time>>::parse_atan2<<lightningcss::values::calc::Calc<lightningcss::values::time::Time> as lightningcss::traits::Parse>::parse::{closure#0}>::{closure#4}>::{closure#0}>Unexecuted instantiation: <f32 as lightningcss::traits::Op>::op_to::<lightningcss::values::angle::Angle, <lightningcss::values::calc::Calc<f32>>::parse_atan2_args<<lightningcss::values::calc::Calc<lightningcss::values::length::Length>>::parse_atan2<<lightningcss::values::calc::Calc<lightningcss::values::length::Length> as lightningcss::traits::Parse>::parse::{closure#0}>::{closure#4}>::{closure#0}> |
84 | | } |
85 | | |
86 | | impl Map for CSSNumber { |
87 | 0 | fn map<F: FnOnce(f32) -> f32>(&self, op: F) -> Self { |
88 | 0 | op(*self) |
89 | 0 | } Unexecuted instantiation: <f32 as lightningcss::traits::Map>::map::<<lightningcss::values::calc::Calc<f32>>::parse_with<<lightningcss::values::calc::Calc<f32> as lightningcss::traits::Parse>::parse::{closure#0}>::{closure#12}::{closure#0}>Unexecuted instantiation: <f32 as lightningcss::traits::Map>::map::<<lightningcss::values::calc::Calc<f32>>::parse_with<<lightningcss::values::color::RelativeComponentParser as cssparser_color::ColorParser>::parse_number::{closure#1}>::{closure#12}::{closure#0}>Unexecuted instantiation: <f32 as lightningcss::traits::Map>::map::<<lightningcss::values::calc::Calc<f32>>::parse_hypot::{closure#0}>Unexecuted instantiation: <f32 as lightningcss::traits::Map>::map::<<f32>::abs> Unexecuted instantiation: <f32 as lightningcss::traits::Map>::map::<<f32>::sqrt> |
90 | | } |
91 | | |
92 | | impl Sign for CSSNumber { |
93 | 0 | fn sign(&self) -> f32 { |
94 | 0 | if *self == 0.0 { |
95 | 0 | return if f32::is_sign_positive(*self) { 0.0 } else { -0.0 }; |
96 | 0 | } |
97 | 0 | self.signum() |
98 | 0 | } |
99 | | } |
100 | | |
101 | | impl Zero for CSSNumber { |
102 | 0 | fn zero() -> Self { |
103 | 0 | 0.0 |
104 | 0 | } |
105 | | |
106 | 0 | fn is_zero(&self) -> bool { |
107 | 0 | *self == 0.0 |
108 | 0 | } |
109 | | } |
110 | | |
111 | | impl_try_from_angle!(CSSNumber); |
112 | | |
113 | | /// A CSS [`<integer>`](https://www.w3.org/TR/css-values-4/#integers) value. |
114 | | pub type CSSInteger = i32; |
115 | | |
116 | | impl<'i> Parse<'i> for CSSInteger { |
117 | 25.7k | fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> { |
118 | | // TODO: calc?? |
119 | 25.7k | let integer = input.expect_integer()?; |
120 | 631 | Ok(integer) |
121 | 25.7k | } |
122 | | } |
123 | | |
124 | | impl ToCss for CSSInteger { |
125 | 0 | fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> |
126 | 0 | where |
127 | 0 | W: std::fmt::Write, |
128 | | { |
129 | 0 | cssparser::ToCss::to_css(self, dest)?; |
130 | 0 | Ok(()) |
131 | 0 | } |
132 | | } |
133 | | |
134 | | impl Zero for CSSInteger { |
135 | 0 | fn zero() -> Self { |
136 | 0 | 0 |
137 | 0 | } |
138 | | |
139 | 0 | fn is_zero(&self) -> bool { |
140 | 0 | *self == 0 |
141 | 0 | } |
142 | | } |