11use pg_interval:: Interval ;
2+ use std:: ops:: Neg ;
23
34pub 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 {
4546impl 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.
196199fn 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.
211213fn 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}
0 commit comments