Skip to content

Commit 8d4cac5

Browse files
authored
feat(prost-types): Add normalized functions (#1158)
Make `normalize` easier to use, by introducing `Duration::normalized` and `Timestamp::normalized`. They do the same as there `normalize` equivalents, but return a copy for ease of use.
1 parent 644c328 commit 8d4cac5

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

prost-types/src/datetime.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -989,10 +989,7 @@ mod tests {
989989
let date_time = DateTime::from(timestamp);
990990
let roundtrip = Timestamp::try_from(date_time).unwrap();
991991

992-
let mut normalized_timestamp = timestamp;
993-
normalized_timestamp.normalize();
994-
995-
prop_assert_eq!(normalized_timestamp, roundtrip);
992+
prop_assert_eq!(timestamp.normalized(), roundtrip);
996993
}
997994

998995
#[test]

prost-types/src/duration.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ impl Duration {
5858
// debug_assert!(self.seconds >= -315_576_000_000 && self.seconds <= 315_576_000_000,
5959
// "invalid duration: {:?}", self);
6060
}
61+
62+
/// Returns a normalized copy of the duration to a canonical format.
63+
///
64+
/// Based on [`google::protobuf::util::CreateNormalized`][1].
65+
///
66+
/// [1]: https://github.com/google/protobuf/blob/v3.3.2/src/google/protobuf/util/time_util.cc#L79-L100
67+
pub fn normalized(&self) -> Self {
68+
let mut result = *self;
69+
result.normalize();
70+
result
71+
}
6172
}
6273

6374
impl Name for Duration {
@@ -77,9 +88,8 @@ impl TryFrom<time::Duration> for Duration {
7788
let seconds = i64::try_from(duration.as_secs()).map_err(|_| DurationError::OutOfRange)?;
7889
let nanos = duration.subsec_nanos() as i32;
7990

80-
let mut duration = Duration { seconds, nanos };
81-
duration.normalize();
82-
Ok(duration)
91+
let duration = Duration { seconds, nanos };
92+
Ok(duration.normalized())
8393
}
8494
}
8595

@@ -105,8 +115,7 @@ impl TryFrom<Duration> for time::Duration {
105115

106116
impl fmt::Display for Duration {
107117
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108-
let mut d = *self;
109-
d.normalize();
118+
let d = self.normalized();
110119
if self.seconds < 0 || self.nanos < 0 {
111120
write!(f, "-")?;
112121
}
@@ -407,14 +416,13 @@ mod tests {
407416
];
408417

409418
for case in cases.iter() {
410-
let mut test_duration = Duration {
419+
let test_duration = Duration {
411420
seconds: case.1,
412421
nanos: case.2,
413422
};
414-
test_duration.normalize();
415423

416424
assert_eq!(
417-
test_duration,
425+
test_duration.normalized(),
418426
Duration {
419427
seconds: case.3,
420428
nanos: case.4,

prost-types/src/timestamp.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ impl Timestamp {
6262
}
6363
}
6464

65+
/// Return a normalized copy of the timestamp to a canonical format.
66+
///
67+
/// Based on [`google::protobuf::util::CreateNormalized`][1].
68+
///
69+
/// [1]: https://github.com/google/protobuf/blob/v3.3.2/src/google/protobuf/util/time_util.cc#L59-L77
70+
pub fn normalized(&self) -> Self {
71+
let mut result = *self;
72+
result.normalize();
73+
result
74+
}
75+
6576
/// Creates a new `Timestamp` at the start of the provided UTC date.
6677
pub fn date(year: i64, month: u8, day: u8) -> Result<Timestamp, TimestampError> {
6778
Timestamp::date_time_nanos(year, month, day, 0, 0, 0, 0)
@@ -399,14 +410,13 @@ mod tests {
399410
];
400411

401412
for case in cases.iter() {
402-
let mut test_timestamp = crate::Timestamp {
413+
let test_timestamp = crate::Timestamp {
403414
seconds: case.1,
404415
nanos: case.2,
405416
};
406-
test_timestamp.normalize();
407417

408418
assert_eq!(
409-
test_timestamp,
419+
test_timestamp.normalized(),
410420
crate::Timestamp {
411421
seconds: case.3,
412422
nanos: case.4,

0 commit comments

Comments
 (0)