|
1 |
| -use super::{Number, Numeric, UnitSet}; |
| 1 | +use super::{Numeric, UnitSet}; |
2 | 2 | use crate::css::Value;
|
3 |
| -use std::fmt; |
4 | 3 |
|
5 | 4 | pub struct ValueRange {
|
6 |
| - from: Number, |
7 |
| - to: Number, |
8 |
| - step: Number, |
| 5 | + from: i64, |
| 6 | + to: i64, |
| 7 | + step: i64, |
9 | 8 | unit: UnitSet,
|
10 | 9 | }
|
11 | 10 |
|
12 | 11 | impl ValueRange {
|
13 |
| - pub fn new( |
14 |
| - from: Value, |
15 |
| - to: Value, |
16 |
| - inclusive: bool, |
17 |
| - ) -> Result<Self, RangeError> { |
18 |
| - let from = |
19 |
| - from.numeric_value().map_err(RangeError::FromNotNumeric)?; |
20 |
| - let to = to.numeric_value().map_err(RangeError::ToNotNumeric)?; |
21 |
| - |
22 |
| - let to = if from.is_no_unit() || to.is_no_unit() { |
23 |
| - to.value |
24 |
| - } else if let Some(scaled) = to.as_unitset(&from.unit) { |
25 |
| - scaled |
26 |
| - } else { |
27 |
| - return Err(RangeError::IncompatibleUnits(from.unit, to.unit)); |
28 |
| - }; |
29 |
| - let step = if to >= from.value { |
30 |
| - Number::from(1) |
31 |
| - } else { |
32 |
| - Number::from(-1) |
33 |
| - }; |
34 |
| - let to = if inclusive { to + step.clone() } else { to }; |
35 |
| - Ok(Self { |
36 |
| - from: from.value, |
| 12 | + pub fn new(from: i64, to: i64, inclusive: bool, unit: UnitSet) -> Self { |
| 13 | + let step = if to >= from { 1 } else { -1 }; |
| 14 | + let to = if inclusive { to + step } else { to }; |
| 15 | + Self { |
| 16 | + from, |
37 | 17 | to,
|
38 | 18 | step,
|
39 |
| - unit: from.unit, |
40 |
| - }) |
| 19 | + unit, |
| 20 | + } |
41 | 21 | }
|
42 | 22 | }
|
43 | 23 |
|
44 | 24 | impl Iterator for ValueRange {
|
45 | 25 | type Item = Value;
|
46 | 26 | fn next(&mut self) -> Option<Value> {
|
47 |
| - if self.from.partial_cmp(&self.to) |
48 |
| - == Number::from(0).partial_cmp(&self.step) |
49 |
| - { |
50 |
| - let result = |
51 |
| - Numeric::new(self.from.clone(), self.unit.clone()).into(); |
52 |
| - self.from = self.from.clone() + self.step.clone(); |
| 27 | + if self.from.partial_cmp(&self.to) == 0.partial_cmp(&self.step) { |
| 28 | + let result = Numeric::new(self.from, self.unit.clone()).into(); |
| 29 | + self.from += self.step; |
53 | 30 | Some(result)
|
54 | 31 | } else {
|
55 | 32 | None
|
56 | 33 | }
|
57 | 34 | }
|
58 | 35 | }
|
59 |
| - |
60 |
| -#[derive(Debug)] |
61 |
| -pub enum RangeError { |
62 |
| - FromNotNumeric(Value), |
63 |
| - ToNotNumeric(Value), |
64 |
| - IncompatibleUnits(UnitSet, UnitSet), |
65 |
| -} |
66 |
| - |
67 |
| -impl fmt::Display for RangeError { |
68 |
| - fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result { |
69 |
| - match self { |
70 |
| - Self::FromNotNumeric(v) => { |
71 |
| - write!( |
72 |
| - out, |
73 |
| - "Bad Range: from needs to be numeric, got {}", |
74 |
| - v.format(Default::default()), |
75 |
| - ) |
76 |
| - } |
77 |
| - Self::ToNotNumeric(v) => { |
78 |
| - write!( |
79 |
| - out, |
80 |
| - "Bad Range: to needs to be numeric, got {}", |
81 |
| - v.format(Default::default()) |
82 |
| - ) |
83 |
| - } |
84 |
| - Self::IncompatibleUnits(a, b) => { |
85 |
| - write!(out, "for loop needs compatible units, got {a}..{b}") |
86 |
| - } |
87 |
| - } |
88 |
| - } |
89 |
| -} |
0 commit comments