Skip to content

Commit 84f8062

Browse files
committed
refactor(*): fix clippy warnings found the modules
1 parent 447cb78 commit 84f8062

File tree

4 files changed

+51
-50
lines changed

4 files changed

+51
-50
lines changed

src/pg_formatter.rs

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use pg_interval::Interval;
2+
use std::ops::Neg;
23

34
pub struct IntervalNorm {
45
pub years: i32,
@@ -21,15 +22,15 @@ impl<'a> From<&'a Interval> for IntervalNorm {
2122
let months = months - years * 12;
2223
// calc the hours from the microseconds and update
2324
// the remaining microseconds.
24-
let hours = (microseconds - (microseconds % 3600000000)) / 3600000000;
25-
let microseconds = microseconds - hours * 3600000000;
25+
let hours = (microseconds - (microseconds % 3_600_000_000)) / 3_600_000_000;
26+
let microseconds = microseconds - hours * 3_600_000_000;
2627
// calc the minutes from remaining microseconds and
2728
// update the remaining microseconds.
28-
let minutes = (microseconds - (microseconds % 60000000)) / 60000000;
29-
let microseconds = microseconds - minutes * 60000000;
29+
let minutes = (microseconds - (microseconds % 60_000_000)) / 60_000_000;
30+
let microseconds = microseconds - minutes * 60_000_000;
3031
// calc the seconds and update the remaining microseconds.
31-
let seconds = (microseconds - (microseconds % 1000000)) / 1000000;
32-
let microseconds = microseconds - seconds * 1000000;
32+
let seconds = (microseconds - (microseconds % 1_000_000)) / 1_000_000;
33+
let microseconds = microseconds - seconds * 1_000_000;
3334
IntervalNorm {
3435
years,
3536
months,
@@ -45,22 +46,22 @@ impl<'a> From<&'a Interval> for IntervalNorm {
4546
impl IntervalNorm {
4647
/// Is all the values in the interval set to 0?
4748
fn is_zeroed(&self) -> bool {
48-
return self.years == 0
49+
self.years == 0
4950
&& self.months == 0
5051
&& self.hours == 0
5152
&& self.minutes == 0
5253
&& self.seconds == 0
53-
&& self.microseconds == 0;
54+
&& self.microseconds == 0
5455
}
5556

5657
/// Is the years or month value set?
5758
fn is_year_month_present(&self) -> bool {
58-
return self.years != 0 || self.months != 0;
59+
self.years != 0 || self.months != 0
5960
}
6061

6162
/// Is the day value set?
6263
fn is_day_present(&self) -> bool {
63-
return self.days != 0;
64+
self.days != 0
6465
}
6566

6667
/// Is at least one of hours,minutes,seconds,microseconds values
@@ -81,10 +82,11 @@ impl IntervalNorm {
8182
fn get_sql_postgres_time_interval(&self) -> String {
8283
let mut time_interval = "".to_owned();
8384
if self.is_time_present() {
84-
let mut sign = "".to_owned();
85-
if !self.is_time_interval_pos() {
86-
sign = "-".to_owned();
87-
}
85+
let sign = if !self.is_time_interval_pos() {
86+
"-".to_owned()
87+
} else {
88+
"".to_owned()
89+
};
8890
time_interval.push_str(
8991
&*(sign
9092
+ &pad_i64(self.hours)
@@ -101,7 +103,7 @@ impl IntervalNorm {
101103
}
102104

103105
/// Produces a iso 8601 compliant interval string.
104-
pub fn to_iso_8601(self) -> String {
106+
pub fn into_iso_8601(self) -> String {
105107
if self.is_zeroed() {
106108
return "PT0S".to_owned();
107109
}
@@ -137,11 +139,11 @@ impl IntervalNorm {
137139
}
138140
year_interval.push_str(&*day_interval);
139141
year_interval.push_str(&*time_interval);
140-
return year_interval;
142+
year_interval
141143
}
142144

143145
/// Produces a postgres compliant interval string.
144-
pub fn to_postgres(self) -> String {
146+
pub fn into_postgres(self) -> String {
145147
if self.is_zeroed() {
146148
return "00:00:00".to_owned();
147149
}
@@ -165,7 +167,7 @@ impl IntervalNorm {
165167
}
166168

167169
/// Produces a compliant sql interval string.
168-
pub fn to_sql(self) -> String {
170+
pub fn into_sql(self) -> String {
169171
if self.is_zeroed() {
170172
return "0".to_owned();
171173
}
@@ -179,41 +181,40 @@ impl IntervalNorm {
179181
day_interval = format!("{:#?} ", self.days);
180182
}
181183
if self.is_year_month_present() {
182-
let mut sign = "+".to_owned();
183-
if self.years < 0 || self.months < 0 {
184-
sign = "-".to_owned();
185-
}
184+
let sign = if self.years < 0 || self.months < 0 {
185+
"-".to_owned()
186+
} else {
187+
"+".to_owned()
188+
};
186189
year_interval = format!("{}{}-{} ", sign, self.years, self.months);
187190
}
188191
year_interval.push_str(&day_interval);
189192
year_interval.push_str(&time_interval);
190-
return year_interval.trim().to_owned();
193+
year_interval.trim().to_owned()
191194
}
192195
}
193196

194197
/// Safely maps a i64 value to a unsigned number
195198
/// without any overflow issues.
196199
fn safe_abs_u64(mut num: i64) -> u64 {
197200
let max = i64::max_value();
198-
let max_min = max * -1;
201+
let max_min = max.neg();
199202
if num <= max_min {
200203
let result = max as u64;
201204
num += max;
202-
num = num * -1;
203-
let result = result + num as u64;
204-
result
205+
num *= -1;
206+
result + num as u64
205207
} else {
206208
num.abs() as u64
207209
}
208210
}
209211

210212
/// Pads a i64 value with a width of 2.
211213
fn pad_i64(val: i64) -> String {
212-
let num;
213-
if val < 0 {
214-
num = safe_abs_u64(val);
214+
let num = if val < 0 {
215+
safe_abs_u64(val)
215216
} else {
216-
num = val as u64;
217-
}
217+
val as u64
218+
};
218219
return format!("{:02}", num);
219220
}

src/pg_interval.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@ impl Interval {
1111
/// Create a new instance of interval from the months, days, and microseconds.
1212
pub fn new(months: i32, days: i32, microseconds: i64) -> Interval {
1313
Interval {
14-
months: months,
15-
days: days,
16-
microseconds: microseconds,
14+
months,
15+
days,
16+
microseconds,
1717
}
1818
}
1919

2020
/// Output the interval as iso 8601 compliant string.
2121
pub fn to_iso_8601(&self) -> String {
22-
IntervalNorm::from(self).to_iso_8601()
22+
IntervalNorm::from(self).into_iso_8601()
2323
}
2424

2525
/// Output the interval as a postgres interval string.
2626
pub fn to_postgres(&self) -> String {
27-
IntervalNorm::from(self).to_postgres()
27+
IntervalNorm::from(self).into_postgres()
2828
}
2929

3030
///Output the interval as a sql compliant interval string.
3131
pub fn to_sql(&self) -> String {
32-
IntervalNorm::from(self).to_sql()
32+
IntervalNorm::from(self).into_sql()
3333
}
3434
}
3535

src/pg_interval_add.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ impl Interval {
1515
/// Shortcut method to add day time part to the interval. Any units smaller than a microsecond
1616
/// will be truncated.
1717
pub fn add_day_time(self, days: i32, hours: i64, minutes: i64, seconds: f64) -> Interval {
18-
let hours_as_micro: i64 = hours * 3600000000;
19-
let minutes_as_micro: i64 = minutes * 60000000;
20-
let seconds_as_micro: i64 = (seconds * 1000000.0).floor() as i64;
18+
let hours_as_micro: i64 = hours * 3_600_000_000;
19+
let minutes_as_micro: i64 = minutes * 60_000_000;
20+
let seconds_as_micro: i64 = (seconds * 1_000_000.0).floor() as i64;
2121
let additional_micro: i64 = hours_as_micro + minutes_as_micro + seconds_as_micro;
2222
Interval {
2323
months: self.months,
@@ -35,9 +35,9 @@ impl Interval {
3535
minutes: i64,
3636
seconds: f64,
3737
) -> Option<Interval> {
38-
let hours_as_micro: i64 = hours.checked_mul(3600000000)?;
39-
let minutes_as_micro: i64 = minutes.checked_mul(60000000)?;
40-
let seconds_as_micro: i64 = (seconds * 1000000.0).floor() as i64;
38+
let hours_as_micro: i64 = hours.checked_mul(3_600_000_000)?;
39+
let minutes_as_micro: i64 = minutes.checked_mul(60_000_000)?;
40+
let seconds_as_micro: i64 = (seconds * 1_000_000.0).floor() as i64;
4141
let additional_micro: i64 = hours_as_micro
4242
.checked_add(minutes_as_micro)?
4343
.checked_add(seconds_as_micro)?;

src/pg_interval_sub.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ impl Interval {
1515
/// Shortcut method to subtract day time part to the interval. Any units smaller than
1616
/// a microsecond will be truncated.
1717
pub fn sub_day_time(self, days: i32, hours: i64, minutes: i64, seconds: f64) -> Interval {
18-
let hours_as_micro: i64 = hours * 3600000000;
19-
let minutes_as_micro: i64 = minutes * 60000000;
20-
let seconds_as_micro: i64 = (seconds * 1000000.0).floor() as i64;
18+
let hours_as_micro: i64 = hours * 3_600_000_000;
19+
let minutes_as_micro: i64 = minutes * 60_000_000;
20+
let seconds_as_micro: i64 = (seconds * 1_000_000.0).floor() as i64;
2121
let additional_micro: i64 = hours_as_micro + minutes_as_micro + seconds_as_micro;
2222
Interval {
2323
months: self.months,
@@ -35,9 +35,9 @@ impl Interval {
3535
minutes: i64,
3636
seconds: f64,
3737
) -> Option<Interval> {
38-
let hours_as_micro: i64 = hours.checked_mul(3600000000)?;
39-
let minutes_as_micro: i64 = minutes.checked_mul(60000000)?;
40-
let seconds_as_micro: i64 = (seconds * 1000000.0).floor() as i64;
38+
let hours_as_micro: i64 = hours.checked_mul(3_600_000_000)?;
39+
let minutes_as_micro: i64 = minutes.checked_mul(60_000_000)?;
40+
let seconds_as_micro: i64 = (seconds * 1_000_000.0).floor() as i64;
4141
let subtracted_micro: i64 = hours_as_micro
4242
.checked_add(minutes_as_micro)?
4343
.checked_add(seconds_as_micro)?;

0 commit comments

Comments
 (0)