-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from baoyachi/code_refactor
Code refactor
- Loading branch information
Showing
6 changed files
with
68 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "duration-str" | ||
version = "0.11.0" | ||
version = "0.11.1" | ||
authors = ["baoyachi <[email protected]>"] | ||
edition = "2021" | ||
description = "duration string parser" | ||
|
@@ -25,6 +25,11 @@ winnow = "0.6.8" | |
|
||
[dev-dependencies] | ||
serde_json = { version = "1.0.87" } | ||
criterion = "0.3" | ||
|
||
[[bench]] | ||
name = "parser_benchmark" | ||
harness = false | ||
|
||
# docs.rs-specific configuration | ||
[package.metadata.docs.rs] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use std::time::Duration; | ||
use winnow::ascii::{digit1, multispace0}; | ||
use winnow::error::ContextError; | ||
use winnow::token::literal; | ||
use winnow::Parser; | ||
|
||
fn parse_duration() { | ||
let duration = duration_str::parse("2h 37m").unwrap(); | ||
assert_eq!(duration, Duration::new(9420, 0)) | ||
} | ||
|
||
fn impeccable_duration() { | ||
let input = "2h 37m"; | ||
let duration = ( | ||
digit1::<_, ContextError>.try_map(str::parse::<u64>), | ||
literal('h').value(3600), | ||
multispace0, | ||
digit1.try_map(str::parse::<u64>), | ||
literal('m').value(60), | ||
) | ||
.map(|(hour, h_unit, _, min, min_unit)| hour * h_unit + min * min_unit) | ||
.map(|seconds| Duration::new(seconds, 0)) | ||
.parse(input) | ||
.unwrap(); | ||
assert_eq!(duration, Duration::new(9420, 0)) | ||
} | ||
|
||
pub fn duration_str_benchmark(c: &mut Criterion) { | ||
c.bench_function("duration_str", |b| b.iter(|| parse_duration())); | ||
} | ||
|
||
pub fn impeccable_benchmark(c: &mut Criterion) { | ||
c.bench_function("impeccable", |b| b.iter(|| impeccable_duration())); | ||
} | ||
|
||
criterion_group!(benches, duration_str_benchmark, impeccable_benchmark); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters