Skip to content

Commit b3a2967

Browse files
committed
Angles and times don't need to store calcs
1 parent 7529be2 commit b3a2967

File tree

2 files changed

+29
-54
lines changed

2 files changed

+29
-54
lines changed

src/values/angle.rs

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ pub enum Angle {
1010
Deg(f32),
1111
Grad(f32),
1212
Rad(f32),
13-
Turn(f32),
14-
Calc(Calc<Angle>)
13+
Turn(f32)
1514
}
1615

1716
impl Parse for Angle {
1817
fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ()>> {
1918
match input.try_parse(Calc::parse) {
2019
Ok(Calc::Value(v)) => return Ok(*v),
21-
Ok(calc) => return Ok(Angle::Calc(calc)),
20+
// Angles are always compatible, so they will always compute to a value.
21+
Ok(_) => unreachable!(),
2222
_ => {}
2323
}
2424

@@ -45,20 +45,16 @@ impl ToCss for Angle {
4545
Angle::Deg(val) => (*val, "deg"),
4646
Angle::Grad(val) => (*val, "grad"),
4747
Angle::Rad(val) => {
48-
if let Some(deg) = self.to_degrees() {
49-
// We print 5 digits of precision by default.
50-
// Switch to degrees if there are an even number of them.
51-
if (deg * 100000.0).round().fract() == 0.0 {
52-
(deg, "deg")
53-
} else {
54-
(*val, "rad")
55-
}
48+
let deg = self.to_degrees();
49+
// We print 5 digits of precision by default.
50+
// Switch to degrees if there are an even number of them.
51+
if (deg * 100000.0).round().fract() == 0.0 {
52+
(deg, "deg")
5653
} else {
5754
(*val, "rad")
5855
}
5956
},
60-
Angle::Turn(val) => (*val, "turn"),
61-
Angle::Calc(calc) => return calc.to_css(dest)
57+
Angle::Turn(val) => (*val, "turn")
6258
};
6359

6460
use cssparser::ToCss;
@@ -93,47 +89,42 @@ impl Angle {
9389
use Angle::*;
9490
match self {
9591
Deg(v) | Rad(v) | Grad(v) | Turn(v) => *v == 0.0,
96-
Calc(_) => false
9792
}
9893
}
9994

100-
pub fn to_radians(&self) -> Option<f32> {
95+
pub fn to_radians(&self) -> f32 {
10196
const RAD_PER_DEG: f32 = PI / 180.0;
102-
let r = match self {
97+
match self {
10398
Angle::Deg(deg) => deg * RAD_PER_DEG,
10499
Angle::Rad(rad) => *rad,
105100
Angle::Grad(grad) => grad * 180.0 / 200.0 * RAD_PER_DEG,
106101
Angle::Turn(turn) => turn * 360.0 * RAD_PER_DEG,
107-
Angle::Calc(_) => return None
108-
};
109-
Some(r)
102+
}
110103
}
111104

112-
pub fn to_degrees(&self) -> Option<f32> {
105+
pub fn to_degrees(&self) -> f32 {
113106
const DEG_PER_RAD: f32 = 180.0 / PI;
114-
let d = match self {
107+
match self {
115108
Angle::Deg(deg) => *deg,
116109
Angle::Rad(rad) => rad * DEG_PER_RAD,
117110
Angle::Grad(grad) => grad * 180.0 / 200.0,
118111
Angle::Turn(turn) => turn * 360.0,
119-
Angle::Calc(_) => return None
120-
};
121-
Some(d)
112+
}
122113
}
123114
}
124115

125116
impl std::convert::Into<Calc<Angle>> for Angle {
126117
fn into(self) -> Calc<Angle> {
127-
match self {
128-
Angle::Calc(c) => c,
129-
b => Calc::Value(Box::new(b))
130-
}
118+
Calc::Value(Box::new(self))
131119
}
132120
}
133121

134122
impl std::convert::From<Calc<Angle>> for Angle {
135123
fn from(calc: Calc<Angle>) -> Angle {
136-
Angle::Calc(calc)
124+
match calc {
125+
Calc::Value(v) => *v,
126+
_ => unreachable!()
127+
}
137128
}
138129
}
139130

@@ -146,7 +137,6 @@ impl std::ops::Mul<f32> for Angle {
146137
Angle::Rad(v) => Angle::Deg(v * other),
147138
Angle::Grad(v) => Angle::Deg(v * other),
148139
Angle::Turn(v) => Angle::Deg(v * other),
149-
Angle::Calc(c) => Angle::Calc(c * other)
150140
}
151141
}
152142
}
@@ -155,20 +145,14 @@ impl std::ops::Add<Angle> for Angle {
155145
type Output = Self;
156146

157147
fn add(self, other: Angle) -> Angle {
158-
match (self, other) {
159-
(Angle::Calc(a), Angle::Calc(b)) => Angle::Calc(a + b),
160-
(Angle::Calc(a), b) => Angle::Calc(a + Calc::Value(Box::new(b))),
161-
(a, Angle::Calc(b)) => Angle::Calc(Calc::Value(Box::new(a)) + b),
162-
(a, b) => Angle::Deg(a.to_degrees().unwrap() + b.to_degrees().unwrap())
163-
}
148+
Angle::Deg(self.to_degrees() + other.to_degrees())
164149
}
165150
}
166151

167152
impl std::cmp::PartialEq<f32> for Angle {
168153
fn eq(&self, other: &f32) -> bool {
169154
match self {
170155
Angle::Deg(a) | Angle::Rad(a) | Angle::Grad(a) | Angle::Turn(a) => a == other,
171-
Angle::Calc(_) => false
172156
}
173157
}
174158
}
@@ -177,7 +161,6 @@ impl std::cmp::PartialOrd<f32> for Angle {
177161
fn partial_cmp(&self, other: &f32) -> Option<std::cmp::Ordering> {
178162
match self {
179163
Angle::Deg(a) | Angle::Rad(a) | Angle::Grad(a) | Angle::Turn(a) => a.partial_cmp(other),
180-
Angle::Calc(_) => None
181164
}
182165
}
183166
}

src/values/time.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
use cssparser::*;
22
use crate::traits::{Parse, ToCss};
33
use crate::printer::Printer;
4-
use std::fmt::Write;
54
use super::calc::Calc;
65

76
/// https://www.w3.org/TR/css3-values/#time-value
87
#[derive(Debug, Clone, PartialEq)]
98
pub enum Time {
109
Seconds(f32),
11-
Milliseconds(f32),
12-
Calc(Calc<Time>)
10+
Milliseconds(f32)
1311
}
1412

1513
impl Parse for Time {
1614
fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ()>> {
1715
match input.try_parse(Calc::parse) {
1816
Ok(Calc::Value(v)) => return Ok(*v),
19-
Ok(calc) => return Ok(Time::Calc(calc)),
17+
// Time is always compatible, so they will always compute to a value.
18+
Ok(_) => unreachable!(),
2019
_ => {}
2120
}
2221

@@ -57,23 +56,22 @@ impl ToCss for Time {
5756
dest.write_str("ms")
5857
}
5958
}
60-
Time::Calc(calc) => calc.to_css(dest)
6159
}
6260
}
6361
}
6462

6563
impl std::convert::Into<Calc<Time>> for Time {
6664
fn into(self) -> Calc<Time> {
67-
match self {
68-
Time::Calc(c) => c,
69-
b => Calc::Value(Box::new(b))
70-
}
65+
Calc::Value(Box::new(self))
7166
}
7267
}
7368

7469
impl std::convert::From<Calc<Time>> for Time {
7570
fn from(calc: Calc<Time>) -> Time {
76-
Time::Calc(calc)
71+
match calc {
72+
Calc::Value(v) => *v,
73+
_ => unreachable!()
74+
}
7775
}
7876
}
7977

@@ -84,7 +82,6 @@ impl std::ops::Mul<f32> for Time {
8482
match self {
8583
Time::Seconds(t) => Time::Seconds(t * other),
8684
Time::Milliseconds(t) => Time::Milliseconds(t * other),
87-
Time::Calc(c) => Time::Calc(c * other)
8885
}
8986
}
9087
}
@@ -98,9 +95,6 @@ impl std::ops::Add<Time> for Time {
9895
(Time::Milliseconds(a), Time::Milliseconds(b)) => Time::Milliseconds(a + b),
9996
(Time::Seconds(a), Time::Milliseconds(b)) => Time::Seconds(a + b / 1000.0),
10097
(Time::Milliseconds(a), Time::Seconds(b)) => Time::Seconds(a + b * 1000.0),
101-
(Time::Calc(a), Time::Calc(b)) => Time::Calc(a + b),
102-
(Time::Calc(a), b) => Time::Calc(a + Calc::Value(Box::new(b))),
103-
(a, Time::Calc(b)) => Time::Calc(Calc::Value(Box::new(a)) + b),
10498
}
10599
}
106100
}
@@ -109,7 +103,6 @@ impl std::cmp::PartialEq<f32> for Time {
109103
fn eq(&self, other: &f32) -> bool {
110104
match self {
111105
Time::Seconds(a) | Time::Milliseconds(a) => a == other,
112-
Time::Calc(_) => false
113106
}
114107
}
115108
}
@@ -118,7 +111,6 @@ impl std::cmp::PartialOrd<f32> for Time {
118111
fn partial_cmp(&self, other: &f32) -> Option<std::cmp::Ordering> {
119112
match self {
120113
Time::Seconds(a) | Time::Milliseconds(a) => a.partial_cmp(other),
121-
Time::Calc(_) => None
122114
}
123115
}
124116
}

0 commit comments

Comments
 (0)