Skip to content

Commit 3109f2e

Browse files
committed
style: fix clippy issues
1 parent f15a31a commit 3109f2e

File tree

12 files changed

+61
-67
lines changed

12 files changed

+61
-67
lines changed

src/integrations/duration.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ const NANOS_PER_SEC: i64 = 1_000_000_000;
55
const NANOS_PER_MICRO: i64 = 1000;
66

77
impl Interval {
8-
/// Tries to convert from the `Duration` type to a `Interval`. Will
9-
/// return `None` on a overflow. This is a lossy conversion in that
8+
/// Tries to convert from the `Duration` type to a `Interval`. Will
9+
/// return `None` on a overflow. This is a lossy conversion in that
1010
/// any units smaller than a microsecond will be lost.
1111
pub fn from_duration(duration: Duration) -> Option<Interval> {
12-
let mut days = duration.num_days();
13-
let mut new_dur = duration - Duration::days(days);
14-
let mut hours = duration.num_hours();
15-
new_dur = new_dur - Duration::hours(hours);
16-
let minutes = new_dur.num_minutes();
17-
new_dur = new_dur - Duration::minutes(minutes);
12+
let mut days = duration.num_days();
13+
let mut new_dur = duration - Duration::days(days);
14+
let mut hours = duration.num_hours();
15+
new_dur -= Duration::hours(hours);
16+
let minutes = new_dur.num_minutes();
17+
new_dur -= Duration::minutes(minutes);
1818
let nano_secs = new_dur.num_nanoseconds()?;
19-
if days > (i32::max_value() as i64) {
20-
let overflow_days = days - (i32::max_value() as i64);
19+
if days > (i32::MAX as i64) {
20+
let overflow_days = days - (i32::MAX as i64);
2121
let added_hours = overflow_days.checked_mul(24)?;
22-
hours = hours.checked_add(added_hours)?;
23-
days -= overflow_days;
22+
hours = hours.checked_add(added_hours)?;
23+
days -= overflow_days;
2424
}
2525
let (seconds, remaining_nano) = reduce_by_units(nano_secs, NANOS_PER_SEC);
2626
// We have to discard any remaining nanoseconds
@@ -39,22 +39,21 @@ impl Interval {
3939
}
4040

4141
fn reduce_by_units(nano_secs: i64, unit: i64) -> (i64, i64) {
42-
let new_time_unit = (nano_secs - (nano_secs % unit)) / unit;
42+
let new_time_unit = (nano_secs - (nano_secs % unit)) / unit;
4343
let remaining_nano = nano_secs - (new_time_unit * unit);
44-
(new_time_unit, remaining_nano)
44+
(new_time_unit, remaining_nano)
4545
}
4646

4747
#[cfg(test)]
4848
mod tests {
4949
use super::*;
5050
use chrono::Duration;
5151

52-
5352
#[test]
5453
fn can_convert_small_amount_of_days() {
5554
let dur = Duration::days(5);
5655
let interval = Interval::from_duration(dur);
57-
assert_eq!(interval, Some(Interval::new(0,5,0)))
56+
assert_eq!(interval, Some(Interval::new(0, 5, 0)))
5857
}
5958

6059
#[test]
@@ -68,13 +67,13 @@ mod tests {
6867
fn can_convert_small_amount_of_secs() {
6968
let dur = Duration::seconds(1);
7069
let interval = Interval::from_duration(dur);
71-
assert_eq!(interval, Some(Interval::new(0,0,1_000_000)))
70+
assert_eq!(interval, Some(Interval::new(0, 0, 1_000_000)))
7271
}
7372

7473
#[test]
7574
fn can_convert_one_micro() {
7675
let dur = Duration::nanoseconds(1000);
7776
let interval = Interval::from_duration(dur);
78-
assert_eq!(interval, Some(Interval::new(0,0,1)))
77+
assert_eq!(interval, Some(Interval::new(0, 0, 1)))
7978
}
80-
}
79+
}

src/integrations/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1+
mod duration;
12
mod rust_postgres;
2-
mod duration;

src/integrations/rust_postgres.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::Interval;
22
use bytes::{Buf, BufMut, BytesMut};
3-
use std::error::Error;
43
use postgres_types::{to_sql_checked, FromSql, IsNull, ToSql, Type};
4+
use std::error::Error;
55

66
impl<'a> FromSql<'a> for Interval {
77
fn from_sql(_: &Type, mut raw: &'a [u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {

src/interval_fmt/iso_8601.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ impl IntervalNorm {
3636
if self.days != 0 {
3737
day_interval.push_str(&format!("{}D", self.days));
3838
}
39-
year_interval.push_str(&*day_interval);
40-
year_interval.push_str(&*time_interval);
39+
year_interval.push_str(&day_interval);
40+
year_interval.push_str(&time_interval);
4141
year_interval
4242
}
4343
}

src/interval_fmt/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,30 @@ use std::ops::Neg;
77
/// Safely maps a i64 value to a unsigned number
88
/// without any overflow issues.
99
fn safe_abs_u64(mut num: i64) -> u64 {
10-
let max = i64::max_value();
10+
let max = i64::MAX;
1111
let max_min = max.neg();
1212
if num <= max_min {
1313
let result = max as u64;
1414
num += max;
1515
num *= -1;
1616
result + num as u64
1717
} else {
18-
num.abs() as u64
18+
num.unsigned_abs()
1919
}
2020
}
2121

2222
/// Safely maps a i32 value to a unsigned number
2323
/// without any overflow issues.
2424
fn safe_abs_u32(mut num: i32) -> u32 {
25-
let max = i32::max_value();
25+
let max = i32::MAX;
2626
let max_min = max.neg();
2727
if num <= max_min {
2828
let result = max as u32;
2929
num += max;
3030
num *= -1;
3131
result + num as u32
3232
} else {
33-
num.abs() as u32
33+
num.unsigned_abs()
3434
}
3535
}
3636

@@ -41,7 +41,7 @@ fn pad_i64(val: i64) -> String {
4141
} else {
4242
val as u64
4343
};
44-
return format!("{:02}", num);
44+
format!("{:02}", num)
4545
}
4646

4747
#[cfg(test)]
@@ -50,15 +50,15 @@ mod tests {
5050

5151
#[test]
5252
fn abs_safe_u32() {
53-
let min = i32::min_value();
53+
let min = i32::MIN;
5454
let actual = safe_abs_u32(min);
5555
let expected = 2147483648;
5656
assert_eq!(actual, expected);
5757
}
5858

5959
#[test]
6060
fn abs_safe_u64() {
61-
let min = i64::min_value();
61+
let min = i64::MIN;
6262
let actual = safe_abs_u64(min);
6363
let expected = 9_223_372_036_854_775_808;
6464
assert_eq!(actual, expected);

src/interval_fmt/postgres.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ impl IntervalNorm {
1414
}
1515
if self.is_year_month_present() {
1616
if self.years != 0 {
17-
year_interval.push_str(&*format!("{:#?} year ", self.years))
17+
year_interval.push_str(&format!("{:#?} year ", self.years))
1818
}
1919
if self.months != 0 {
20-
year_interval.push_str(&*format!("{:#?} mons ", self.months));
20+
year_interval.push_str(&format!("{:#?} mons ", self.months));
2121
}
2222
}
23-
year_interval.push_str(&*day_interval);
24-
year_interval.push_str(&*time_interval);
23+
year_interval.push_str(&day_interval);
24+
year_interval.push_str(&time_interval);
2525
year_interval.trim().to_owned()
2626
}
2727

@@ -35,15 +35,15 @@ impl IntervalNorm {
3535
};
3636
let hours = super::pad_i64(self.hours);
3737
time_interval.push_str(
38-
&*(sign
38+
&(sign
3939
+ &hours
4040
+ ":"
4141
+ &super::pad_i64(self.minutes)
4242
+ ":"
4343
+ &super::pad_i64(self.seconds)),
4444
);
4545
if self.microseconds != 0 {
46-
time_interval.push_str(&*format!(".{:06}", super::safe_abs_u64(self.microseconds)))
46+
time_interval.push_str(&format!(".{:06}", super::safe_abs_u64(self.microseconds)))
4747
}
4848
}
4949
time_interval

src/interval_norm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub struct IntervalNorm {
1010
pub microseconds: i64,
1111
}
1212

13-
impl<'a> From<&'a Interval> for IntervalNorm {
13+
impl From<&Interval> for IntervalNorm {
1414
fn from(val: &Interval) -> IntervalNorm {
1515
// grab the base values from the interval
1616
let months = val.months;
@@ -62,7 +62,7 @@ impl IntervalNorm {
6262
})?,
6363
days: self.days,
6464
microseconds: microseconds
65-
.ok_or_else(|| ParseError::from_time("Invalid time interval overflow detected."))?,
65+
.ok_or_else(|| ParseError::from_time("Invalid time interval overflow detected."))?,
6666
})
6767
}
6868

src/interval_parse/iso_8601.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use super::parse_error::ParseError;
12
use super::{
23
scale_date, scale_time, DAYS_PER_MONTH, HOURS_PER_DAY, MICROS_PER_SECOND, MINUTES_PER_HOUR,
34
MONTHS_PER_YEAR, SECONDS_PER_MIN,
45
};
5-
use super::parse_error::ParseError;
66
use crate::{interval_norm::IntervalNorm, Interval};
77

88
enum ParserCode {
@@ -17,7 +17,7 @@ impl Interval {
1717
let delim = vec!['Y', 'M', 'D', 'H', 'S'];
1818
let mut number = String::new();
1919
let mut interval_norm = IntervalNorm::default();
20-
if iso_str.rfind('P').map_or(false, |v| v == 1) {
20+
if iso_str.rfind('P') == Some(1) {
2121
Err(ParseError::from_invalid_interval(
2222
"Invalid format must start with P.",
2323
))
@@ -102,9 +102,9 @@ impl Interval {
102102
}
103103

104104
fn consume_number<'a>(val: &'a char, number: &'a mut String, delim: &[char]) -> ParserCode {
105-
let is_first_char = number.is_empty() && *val == '-';
105+
let is_first_char = number.is_empty() && *val == '-';
106106
let is_period_char = !number.is_empty() && *val == '.';
107-
if val.is_digit(10) || is_first_char || is_period_char {
107+
if val.is_ascii_digit() || is_first_char || is_period_char {
108108
number.push(*val);
109109
ParserCode::Good
110110
} else if delim.contains(val) {
@@ -116,7 +116,7 @@ fn consume_number<'a>(val: &'a char, number: &'a mut String, delim: &[char]) ->
116116

117117
fn parse_number(number: &mut String) -> Result<f64, ParseError> {
118118
let parse_num = number.parse::<f64>()?;
119-
if parse_num > i32::max_value() as f64 {
119+
if parse_num > i32::MAX as f64 {
120120
Err(ParseError::from_invalid_interval("Exceeded max value"))
121121
} else {
122122
*number = "".to_owned();
@@ -257,19 +257,19 @@ mod tests {
257257
#[test]
258258
fn test_from_8601_19() {
259259
let interval = Interval::from_iso("PTT");
260-
assert_eq!(interval.is_err(), true);
260+
assert!(interval.is_err());
261261
}
262262

263263
#[test]
264264
fn test_from_8601_20() {
265265
let interval = Interval::from_iso("PT-");
266-
assert_eq!(interval.is_err(), true);
266+
assert!(interval.is_err());
267267
}
268268

269269
#[test]
270270
fn test_from_8601_21() {
271271
let interval = Interval::from_iso("PT10");
272-
assert_eq!(interval.is_err(), true);
272+
assert!(interval.is_err());
273273
}
274274

275275
#[test]
@@ -299,5 +299,4 @@ mod tests {
299299
let interval_exp = Interval::new(0, 0, 10000000);
300300
assert_eq!(interval, interval_exp);
301301
}
302-
303302
}

src/interval_parse/postgres.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use super::{
77
};
88

99
impl Interval {
10-
pub fn from_postgres(iso_str: &str) -> Result<Interval, ParseError> {
10+
pub fn from_postgres(iso_str: &str) -> Result<Interval, ParseError> {
1111
let mut delim = vec![
1212
"years", "months", "mons", "days", "hours", "minutes", "seconds",
1313
];
14-
let mut time_tokens = iso_str.split(' ').collect::<Vec<&str>>(); // clean up empty values caused by n spaces between values.
14+
let mut time_tokens = iso_str.split(' ').collect::<Vec<&str>>(); // clean up empty values caused by n spaces between values.
1515
time_tokens.retain(|&token| !token.is_empty());
1616
// since there might not be space between the delim and the
1717
// value we need to scan each token.
@@ -88,11 +88,11 @@ fn split_token(val: &str) -> Result<(String, String), ParseError> {
8888
}
8989

9090
/// Consume the token parts and add to the normalized interval.
91-
fn consume_token<'a>(
91+
fn consume_token(
9292
interval: &mut IntervalNorm,
9393
val: f64,
9494
delim: String,
95-
delim_list: &mut Vec<&'a str>,
95+
delim_list: &mut Vec<&str>,
9696
) -> Result<(), ParseError> {
9797
// Unlike iso8601 the delimiter can only appear once
9898
// so we need to check if the token can be found in
@@ -325,19 +325,19 @@ mod tests {
325325
#[test]
326326
fn test_from_postgres_24() {
327327
let interval = Interval::from_postgres("years 1");
328-
assert_eq!(interval.is_err(), true);
328+
assert!(interval.is_err());
329329
}
330330

331331
#[test]
332332
fn test_from_postgres_25() {
333333
let interval = Interval::from_postgres("- years");
334-
assert_eq!(interval.is_err(), true);
334+
assert!(interval.is_err());
335335
}
336336

337337
#[test]
338338
fn test_from_postgres_26() {
339339
let interval = Interval::from_postgres("10");
340-
assert_eq!(interval.is_err(), true);
340+
assert!(interval.is_err());
341341
}
342342

343343
#[test]
@@ -364,7 +364,6 @@ mod tests {
364364
#[test]
365365
fn test_from_postgres_30() {
366366
let interval = Interval::from_postgres("!");
367-
assert_eq!(interval.is_err(), true);
367+
assert!(interval.is_err());
368368
}
369-
370369
}

src/pg_interval.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ mod tests {
5656
#[test]
5757
fn test_clone() {
5858
let interval = Interval::new(1, 1, 30);
59-
let test_interval = interval.clone();
59+
let test_interval = interval;
6060
assert_eq!(interval, test_interval);
6161
}
6262

@@ -313,7 +313,7 @@ mod tests {
313313
}
314314

315315
#[test]
316-
fn test_postgres_19(){
316+
fn test_postgres_19() {
317317
let interval = Interval::new(0, 3, 0);
318318
let output = interval.to_postgres();
319319
assert_eq!(String::from("3 days"), output);
@@ -444,5 +444,4 @@ mod tests {
444444
let output = interval.to_sql();
445445
assert_eq!(String::from("-1:10:15"), output);
446446
}
447-
448447
}

0 commit comments

Comments
 (0)