Skip to content

Commit eeb2791

Browse files
committed
Remove percentage Raw. Just work with f64 if unvalidated is needed
1 parent c7f5f0c commit eeb2791

6 files changed

Lines changed: 78 additions & 85 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
:w
21
#v0.2
32
- Add `RelativeResourcePath` and `RelativeResourcePathBuf`. Deprecate `RelativeFilePath`, as it's name was misleading (it contained a PathBuf)
43
- `RelativeDirPathError` and `RelativeFilePathError` changed from containing `String` to the constructor input type
4+
- Remove `sealedstruct::Seal` from Percentage, thus remove PercentageRaw

flake.lock

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pilatus-engineering/src/spatial/relative_polygon.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ impl RelativePolygon {
1616
.iter()
1717
.map(|(col_rel, row_rel)| {
1818
(
19-
(col_rel.into_inner().value() * x_dist + 0.5) as u32,
20-
(row_rel.into_inner().value() * y_dist + 0.5) as u32,
19+
(col_rel.value() * x_dist + 0.5) as u32,
20+
(row_rel.value() * y_dist + 0.5) as u32,
2121
)
2222
})
2323
.collect::<Vec<_>>()

pilatus-engineering/src/spatial/relative_rectangle.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ impl RelativeRectangle {
2020
let x_dist = (dimensions.0.get() - 1) as f64;
2121
let y_dist = (dimensions.1.get() - 1) as f64;
2222

23-
let col1 = **self.column.from * x_dist + 0.5;
24-
let row1 = **self.row.from * y_dist + 0.5;
25-
let col2 = **self.column.to * x_dist + 0.5;
26-
let row2 = **self.row.to * y_dist + 0.5;
23+
let col1 = *self.column.from * x_dist + 0.5;
24+
let row1 = *self.row.from * y_dist + 0.5;
25+
let col2 = *self.column.to * x_dist + 0.5;
26+
let row2 = *self.row.to * y_dist + 0.5;
2727

2828
[col1 as u32, row1 as u32, col2 as u32, row2 as u32]
2929
}

pilatus/src/relative/percentage.rs

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,69 @@ use std::{
55

66
use serde::{Deserialize, Serialize};
77

8-
#[derive(PartialEq, PartialOrd, Clone, Copy, Serialize, Deserialize, sealedstruct::Seal)]
9-
pub struct PercentageRaw(f64);
8+
#[derive(PartialEq, PartialOrd, Clone, Copy, Serialize, Deserialize)]
9+
#[serde(try_from = "f64")]
10+
pub struct Percentage(f64);
1011

1112
#[cfg(feature = "impex")]
1213
impl impex::ImpexPrimitive for Percentage {}
1314

14-
impl sealedstruct::Validator for PercentageRaw {
15-
fn check(&self) -> sealedstruct::Result<()> {
16-
if **self < 0. {
17-
return sealedstruct::ValidationError::new(format!("{} is too small", self.0)).into();
18-
}
15+
#[derive(Debug, thiserror::Error)]
16+
#[error("Invalid percentage {0}")]
17+
pub struct InvalidPercentage(f64);
18+
19+
impl TryFrom<f64> for Percentage {
20+
type Error = InvalidPercentage;
1921

20-
if **self > 1. {
21-
return sealedstruct::ValidationError::new(format!("{} is too big", self.0)).into();
22+
fn try_from(value: f64) -> Result<Self, Self::Error> {
23+
if ((0.)..=1.).contains(&value) {
24+
Ok(Percentage(value))
25+
} else {
26+
Err(InvalidPercentage(value))
2227
}
23-
Ok(())
2428
}
2529
}
2630

2731
impl Percentage {
28-
pub fn new(i: f64) -> sealedstruct::Result<Self> {
29-
PercentageRaw(i).seal()
32+
pub const fn new(value: f64) -> Option<Self> {
33+
if value >= 0. && value <= 1. {
34+
Some(Percentage(value))
35+
} else {
36+
None
37+
}
3038
}
31-
pub fn max() -> Self {
32-
Percentage::new_unchecked(PercentageRaw(1.0))
39+
40+
pub const fn max() -> Self {
41+
Percentage(1.0)
3342
}
34-
pub fn min() -> Self {
35-
Percentage::new_unchecked(PercentageRaw(0.0))
43+
pub const fn min() -> Self {
44+
Percentage(0.0)
3645
}
3746

3847
pub fn fifty() -> Self {
39-
Percentage::new_unchecked(PercentageRaw(0.5))
48+
Percentage(0.5)
4049
}
4150
}
4251

43-
impl approx::AbsDiffEq for PercentageRaw {
52+
impl approx::AbsDiffEq for Percentage {
4453
type Epsilon = f64;
4554

4655
fn default_epsilon() -> Self::Epsilon {
4756
f64::EPSILON
4857
}
4958

5059
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
51-
self.0.abs_diff_eq(other, epsilon)
60+
self.0.abs_diff_eq(&other.0, epsilon)
5261
}
5362
}
5463

55-
impl PercentageRaw {
56-
pub fn new(i: f64) -> Self {
57-
Self(i)
58-
}
59-
64+
impl Percentage {
6065
pub fn value(&self) -> f64 {
6166
self.0
6267
}
6368
}
6469

65-
impl Display for PercentageRaw {
70+
impl Display for Percentage {
6671
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
6772
f.write_fmt(format_args!("{}%", self.0 * 100.))
6873
}
@@ -74,42 +79,22 @@ impl Debug for Percentage {
7479
}
7580
}
7681

77-
impl std::ops::Sub for PercentageRaw {
78-
type Output = PercentageRaw;
82+
impl std::ops::Mul<f64> for Percentage {
83+
type Output = f64;
7984

80-
fn sub(self, rhs: Self) -> Self::Output {
81-
PercentageRaw(self.0 - rhs.0)
85+
fn mul(self, rhs: f64) -> Self::Output {
86+
self.0 * rhs
8287
}
8388
}
84-
85-
impl std::ops::Add for PercentageRaw {
86-
type Output = PercentageRaw;
87-
88-
fn add(self, rhs: Self) -> Self::Output {
89-
PercentageRaw(self.0 + rhs.0)
90-
}
91-
}
92-
93-
impl std::ops::Mul for PercentageRaw {
94-
type Output = PercentageRaw;
89+
impl std::ops::Mul for Percentage {
90+
type Output = Percentage;
9591

9692
fn mul(self, rhs: Self) -> Self::Output {
97-
PercentageRaw(self.0 * rhs.0)
98-
}
99-
}
100-
101-
impl From<f64> for Percentage {
102-
fn from(i: f64) -> Self {
103-
Self(i.into())
104-
}
105-
}
106-
impl From<f64> for PercentageRaw {
107-
fn from(i: f64) -> Self {
108-
Self(i)
93+
Percentage(self.0 * rhs.0)
10994
}
11095
}
11196

112-
impl Deref for PercentageRaw {
97+
impl Deref for Percentage {
11398
type Target = f64;
11499

115100
fn deref(&self) -> &Self::Target {
@@ -123,7 +108,7 @@ mod tests {
123108

124109
#[test]
125110
fn invalid_percentage() {
126-
assert!(PercentageRaw::new(-1.).seal().is_err());
127-
assert!(PercentageRaw::new(1.1).seal().is_err());
111+
assert!(Percentage::new(-1.).is_none());
112+
assert!(Percentage::new(1.1).is_none());
128113
}
129114
}

pilatus/src/relative/range.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::{fmt::Display};
2+
13
use serde::{Deserialize, Serialize};
24

35
use super::Percentage;
@@ -53,11 +55,13 @@ impl RelativeRange {
5355
) -> (Option<RelativeRange>, RelativeRange, Option<RelativeRange>) {
5456
let width_percentage = *self.to - *self.from;
5557
let from_split_position = (*self.from + width_percentage * *range.from)
56-
.seal()
58+
.try_into()
59+
.unwrap();
60+
let to_split_position = (*self.from + width_percentage * *range.to)
61+
.try_into()
5762
.unwrap();
58-
let to_split_position = (*self.from + width_percentage * *range.to).seal().unwrap();
5963

60-
let left = (**range.from > 0.).then(|| {
64+
let left = (*range.from > 0.).then(|| {
6165
RelativeRangeRaw {
6266
from: self.from,
6367
to: from_split_position,
@@ -73,7 +77,7 @@ impl RelativeRange {
7377
.seal()
7478
.unwrap();
7579

76-
let right = (**range.to < 1.).then(|| {
80+
let right = (*range.to < 1.).then(|| {
7781
RelativeRangeRaw {
7882
from: to_split_position,
7983
to: self.to,
@@ -88,12 +92,16 @@ impl RelativeRange {
8892

8993
impl RelativeRange {
9094
pub fn new(
91-
from: impl Into<Percentage>,
92-
to: impl Into<Percentage>,
95+
from: impl TryInto<Percentage, Error: Display>,
96+
to: impl TryInto<Percentage, Error: Display>,
9397
) -> sealedstruct::Result<Self> {
9498
RelativeRangeRaw {
95-
from: from.into(),
96-
to: to.into(),
99+
from: from
100+
.try_into()
101+
.map_err(|x| sealedstruct::ValidationError::new(format!("Invalid from {x}")))?,
102+
to: to
103+
.try_into()
104+
.map_err(|x| sealedstruct::ValidationError::new(format!("Invalid to {x}")))?,
97105
}
98106
.seal()
99107
}

0 commit comments

Comments
 (0)