Skip to content

Commit e598c92

Browse files
committed
refactor(iso8601): 🚚 rename parse/format functions for consistency with chrono
specifically they already have a function called `parse_from_rfc3339` on their DateTime, so we mimic that convention
1 parent d97203e commit e598c92

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

benches/relative_duration.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ fn relative_duration_format_benchmark(c: &mut Criterion) {
99
"P99999999Y11M30DT23H59M59.999999999S",
1010
]
1111
.iter()
12-
.map(|s| RelativeDuration::from_iso_8601(s).unwrap())
12+
.map(|s| RelativeDuration::parse_from_iso8601(s).unwrap())
1313
.collect::<Vec<RelativeDuration>>();
1414

1515
let mut g = c.benchmark_group("relative_duration_format");
1616

1717
g.bench_function("one_specifier", |b| {
18-
b.iter(|| black_box(durations[0]).to_iso_8601())
18+
b.iter(|| black_box(durations[0]).format_to_iso8601())
1919
});
2020
g.bench_function("all_specifiers", |b| {
21-
b.iter(|| black_box(durations[1]).to_iso_8601())
21+
b.iter(|| black_box(durations[1]).format_to_iso8601())
2222
});
2323
g.bench_function("long_specifiers", |b| {
24-
b.iter(|| black_box(durations[2]).to_iso_8601())
24+
b.iter(|| black_box(durations[2]).format_to_iso8601())
2525
});
2626
}
2727

@@ -35,13 +35,13 @@ fn relative_duration_parse_benchmark(c: &mut Criterion) {
3535
let mut g = c.benchmark_group("relative_duration_parse");
3636

3737
g.bench_function("one_specifier", |b| {
38-
b.iter(|| RelativeDuration::from_iso_8601(black_box(durations[0])))
38+
b.iter(|| RelativeDuration::parse_from_iso8601(black_box(durations[0])))
3939
});
4040
g.bench_function("all_specifiers", |b| {
41-
b.iter(|| RelativeDuration::from_iso_8601(black_box(durations[1])))
41+
b.iter(|| RelativeDuration::parse_from_iso8601(black_box(durations[1])))
4242
});
4343
g.bench_function("long_specifiers", |b| {
44-
b.iter(|| RelativeDuration::from_iso_8601(black_box(durations[2])))
44+
b.iter(|| RelativeDuration::parse_from_iso8601(black_box(durations[2])))
4545
});
4646
}
4747

src/relative_duration/parse.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ impl RelativeDuration {
139139
/// use chronoutil::RelativeDuration;
140140
///
141141
/// assert_eq!(
142-
/// RelativeDuration::from_iso_8601("P1Y").unwrap(),
142+
/// RelativeDuration::parse_from_iso8601("P1Y").unwrap(),
143143
/// RelativeDuration::years(1),
144144
/// );
145145
/// ```
146-
pub fn from_iso_8601(input: &str) -> Result<RelativeDuration, String> {
146+
pub fn parse_from_iso8601(input: &str) -> Result<RelativeDuration, String> {
147147
let input = input
148148
.strip_prefix('P')
149149
.ok_or_else(|| "duration was not prefixed with P".to_string())?;
@@ -174,11 +174,11 @@ impl RelativeDuration {
174174
/// use chronoutil::RelativeDuration;
175175
///
176176
/// assert_eq!(
177-
/// RelativeDuration::years(1).to_iso_8601(),
177+
/// RelativeDuration::years(1).format_to_iso8601(),
178178
/// "P1Y",
179179
/// );
180180
/// ```
181-
pub fn to_iso_8601(&self) -> String {
181+
pub fn format_to_iso8601(&self) -> String {
182182
let years = self.months as i64 / 12;
183183
let months = self.months as i64 % 12;
184184

@@ -301,7 +301,10 @@ mod tests {
301301
]
302302
.iter()
303303
.for_each(|(input, expected)| {
304-
assert_eq!(RelativeDuration::from_iso_8601(input).unwrap(), *expected)
304+
assert_eq!(
305+
RelativeDuration::parse_from_iso8601(input).unwrap(),
306+
*expected
307+
)
305308
})
306309
}
307310

@@ -333,7 +336,7 @@ mod tests {
333336
),
334337
]
335338
.iter()
336-
.for_each(|(input, expected)| assert_eq!(input.to_iso_8601(), *expected))
339+
.for_each(|(input, expected)| assert_eq!(input.format_to_iso8601(), *expected))
337340
}
338341

339342
proptest! {
@@ -344,19 +347,19 @@ mod tests {
344347
nanos in 0u32..1_000_000_000
345348
) {
346349
let d = RelativeDuration::months(months).with_duration(Duration::new(secs, nanos).unwrap());
347-
prop_assert_eq!(d, RelativeDuration::from_iso_8601(&(d.to_iso_8601())).unwrap());
350+
prop_assert_eq!(d, RelativeDuration::parse_from_iso8601(&(d.format_to_iso8601())).unwrap());
348351
}
349352

350353
#[test]
351354
fn proptest_parse_and_back(
352355
s in r"P(?:[1-9][0-9]{0,7}Y)?(?:(?:[1-9]|1[0-1])M)?(?:(?:[1-9]|[1-2][0-9])D)?(?:T(?:(?:[1-9]|1[0-9]|2[0-3])H)(?:(?:[1-9]|[1-5][0-9])M)(?:(?:(?:[1-9]|[1-5][0-9])|(?:(?:[0-9]|[1-5][0-9])\.[0-9]{0,8}[1-9]))S))?",
353356
) {
354-
prop_assert_eq!(s.clone(), RelativeDuration::from_iso_8601(&s).unwrap().to_iso_8601());
357+
prop_assert_eq!(s.clone(), RelativeDuration::parse_from_iso8601(&s).unwrap().format_to_iso8601());
355358
}
356359

357360
#[test]
358361
fn proptest_parse_doesnt_panic(s in r"//PC*") {
359-
let _ = RelativeDuration::from_iso_8601(&s);
362+
let _ = RelativeDuration::parse_from_iso8601(&s);
360363
}
361364
}
362365
}

0 commit comments

Comments
 (0)