Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix arithmetic side effects #55

Merged
merged 2 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ rustflags = [
"-Wtrivial_numeric_casts",
"-Wunsafe_code",
"-Wunused_import_braces",
"-Wunused_qualifications",

# https://rust-lang.github.io/rust-clippy/master/index.html
"-Aclippy::doc_markdown",
Expand Down
5 changes: 4 additions & 1 deletion ocpi-tariffs/src/pricer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ impl Pricer {

let total_time = if let Some(first) = periods.first() {
let last = periods.last().unwrap();
(last.end_date_time - first.start_date_time).into()
(last
.end_date_time
.signed_duration_since(first.start_date_time))
.into()
} else {
HoursDecimal::zero()
};
Expand Down
13 changes: 11 additions & 2 deletions ocpi-tariffs/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,20 @@ impl InstantData {
fn next(&self, state: &PeriodData, date_time: DateTime) -> Self {
let mut next = self.clone();

next.total_duration = next.total_duration + (date_time - next.date_time);
let duration = date_time.signed_duration_since(next.date_time);

next.total_duration = next
.total_duration
.checked_add(&duration)
.unwrap_or_else(Duration::max_value);

next.date_time = date_time;

if let Some(duration) = state.charging_duration {
next.total_charging_duration = next.total_charging_duration + duration;
next.total_charging_duration = next
.total_charging_duration
.checked_add(&duration)
.unwrap_or_else(Duration::max_value);
}

if let Some(energy) = state.energy {
Expand Down
2 changes: 1 addition & 1 deletion ocpi-tariffs/src/types/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Div for Number {
type Output = Self;

fn div(self, rhs: Self) -> Self::Output {
bheylin marked this conversation as resolved.
Show resolved Hide resolved
Self(self.0 / rhs.0)
Self(self.0.checked_div(rhs.0).expect("divide by zero"))
}
}

Expand Down
7 changes: 5 additions & 2 deletions ocpi-tariffs/src/types/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@ impl From<Duration> for HoursDecimal {

impl AddAssign for HoursDecimal {
fn add_assign(&mut self, rhs: Self) {
self.0 = self.0 + rhs.0;
self.0 = self
bheylin marked this conversation as resolved.
Show resolved Hide resolved
.0
.checked_add(&rhs.0)
.unwrap_or_else(Duration::max_value);
}
}

impl SubAssign for HoursDecimal {
bheylin marked this conversation as resolved.
Show resolved Hide resolved
fn sub_assign(&mut self, rhs: Self) {
self.0 = self.0 - rhs.0;
self.0 = self.0.checked_sub(&rhs.0).unwrap_or_else(Duration::zero);
}
}

Expand Down