Skip to content

Commit

Permalink
Merge pull request #49 from baoyachi/code_refactor
Browse files Browse the repository at this point in the history
Code refactor
  • Loading branch information
baoyachi committed Jun 5, 2024
2 parents 5c612a0 + 32768ad commit a1be313
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 11 deletions.
7 changes: 6 additions & 1 deletion Cargo.toml
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 <liaoymxsdl@gmail.com>"]
edition = "2021"
description = "duration string parser"
Expand All @@ -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]
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# parse string to std::time::Duration
========================================
<p align="center">
<img
width="200"
src="https://raw.githubusercontent.com/baoyachi/duration-str/master/duration-str.png"
alt="duration-str parser"
/>
</p>

[![Chrono GitHub Actions](https://github.com/baoyachi/duration-str-rs/actions/workflows/check.yml/badge.svg)](https://github.com/baoyachi/duration-str-rs/actions?query=workflow%3Abuild)
[![Crates.io](https://img.shields.io/crates/v/duration-str.svg)](https://crates.io/crates/duration-str)
Expand Down
38 changes: 38 additions & 0 deletions benches/parser_benchmark.rs
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);
18 changes: 11 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![doc(
html_logo_url = "https://raw.githubusercontent.com/baoyachi/duration-rs/master/duration-str.png"
html_logo_url = "https://raw.githubusercontent.com/baoyachi/duration-str/master/duration-str.png"
)]
//! Parse string to `Duration` .
//!
Expand Down Expand Up @@ -215,8 +215,10 @@ fn one_second_decimal() -> Decimal {
const PLUS: &str = "+";
const STAR: &str = "*";

trait ExpectErr<const LEN: usize> {
fn expect_val() -> [&'static str; LEN];
trait ExpectErr {
type Output: Debug;

fn expect_val() -> Self::Output;
fn expect_err<S: AsRef<str> + Display>(s: S) -> String;
}

Expand All @@ -238,9 +240,11 @@ impl FromStr for CondUnit {
}
}

impl ExpectErr<2> for CondUnit {
fn expect_val() -> [&'static str; 2] {
["+", "*"]
impl ExpectErr for CondUnit {
type Output = [char; 2];

fn expect_val() -> Self::Output {
['+', '*']
}

fn expect_err<S: AsRef<str> + Display>(s: S) -> String {
Expand All @@ -254,7 +258,7 @@ impl CondUnit {
}

fn contain(c: char) -> bool {
Self::expect_val().contains(&&*c.to_string())
Self::expect_val().contains(&c)
}

fn change_duration(&self) -> u64 {
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ partial_input:mxyz, expect one of :["y", "mon", "w", "d", "h", "m", "s", "ms", "
r#"
3ms-2ms
^
partial_input:-2ms, expect one of:["+", "*"], but find:-2ms"#
partial_input:-2ms, expect one of:['+', '*'], but find:-2ms"#
.trim()
);
}
Expand Down
6 changes: 4 additions & 2 deletions src/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ impl FromStr for TimeUnit {
}
}

impl ExpectErr<11> for TimeUnit {
fn expect_val() -> [&'static str; 11] {
impl ExpectErr for TimeUnit {
type Output = [&'static str; 11];

fn expect_val() -> Self::Output {
["y", "mon", "w", "d", "h", "m", "s", "ms", "µs", "us", "ns"]
}

Expand Down

0 comments on commit a1be313

Please sign in to comment.