Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
baoyachi committed May 7, 2024
1 parent 36a707a commit f767558
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 73 deletions.
75 changes: 2 additions & 73 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
mod parser;
#[cfg(feature = "serde")]
mod serde;
mod unit;

pub use parser::parse;
#[cfg(feature = "serde")]
Expand All @@ -180,6 +181,7 @@ use std::str::FromStr;
use std::time::Duration;
use thiserror::Error;

use crate::unit::TimeUnit;
#[cfg(feature = "chrono")]
pub use naive_date::{
after_naive_date, after_naive_date_time, before_naive_date, before_naive_date_time,
Expand All @@ -197,57 +199,6 @@ pub enum DError {
OverflowError,
}

#[derive(Debug, Eq, PartialEq, Default, Clone)]
pub(crate) enum TimeUnit {
Year,
Month,
Week,
Day,
Hour,
Minute,
#[default]
Second,
MilliSecond,
MicroSecond,
NanoSecond,
}

impl FromStr for TimeUnit {
type Err = DError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match &*s.to_lowercase() {
"y" | "year" => Ok(TimeUnit::Year),
"mon" | "month" => Ok(TimeUnit::Month),
"w" | "week" => Ok(TimeUnit::Week),
"d" | "day" => Ok(TimeUnit::Day),
"h" | "hour" | "hr" => Ok(TimeUnit::Hour),
"m" | "min" | "minute" => Ok(TimeUnit::Minute),
"s" | "sec" | "second" => Ok(TimeUnit::Second),
"ms" | "msec" | "millisecond" => Ok(TimeUnit::MilliSecond),
"µs" | "µsec" | "µsecond" | "us" | "usec" | "usecond" | "microsecond" => {
Ok(TimeUnit::MicroSecond)
}
"ns" | "nsec" | "nanosecond" => Ok(TimeUnit::NanoSecond),
_ => Err(DError::ParseError(Self::expect_err(s))),
}
}
}

impl ExpectErr<11> for TimeUnit {
fn expect_val() -> [&'static str; 11] {
["y", "mon", "w", "d", "h", "m", "s", "ms", "µs", "us", "ns"]
}

fn expect_err<S: AsRef<str> + Display>(s: S) -> String {
format!(
"expect one of :{:?} or their longer forms, but find:{}",
Self::expect_val(),
s,
)
}
}

const ONE_MICROSECOND_NANOSECOND: u64 = 1000;
const ONE_MILLISECOND_NANOSECOND: u64 = 1000 * ONE_MICROSECOND_NANOSECOND;
const ONE_SECOND_NANOSECOND: u64 = 1000 * ONE_MILLISECOND_NANOSECOND;
Expand All @@ -263,28 +214,6 @@ fn one_second_decimal() -> Decimal {
1_000_000_000.into()
}

impl TimeUnit {
fn duration(&self, time_str: impl AsRef<str>) -> DResult<u64> {
let time = time_str
.as_ref()
.parse::<u64>()
.map_err(|err| DError::ParseError(err.to_string()))?;
let unit = match self {
TimeUnit::Year => ONE_YEAR_NANOSECOND,
TimeUnit::Month => ONE_MONTH_NANOSECOND,
TimeUnit::Week => ONE_WEEK_NANOSECOND,
TimeUnit::Day => ONE_DAY_NANOSECOND,
TimeUnit::Hour => ONE_HOUR_NANOSECOND,
TimeUnit::Minute => ONE_MINUTE_NANOSECOND,
TimeUnit::Second => ONE_SECOND_NANOSECOND,
TimeUnit::MilliSecond => ONE_MILLISECOND_NANOSECOND,
TimeUnit::MicroSecond => ONE_MICROSECOND_NANOSECOND,
TimeUnit::NanoSecond => 1,
};
time.checked_mul(unit).ok_or(DError::OverflowError)
}
}

const PLUS: &str = "+";
const STAR: &str = "*";

Expand Down
80 changes: 80 additions & 0 deletions src/unit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use crate::{
DError, DResult, ExpectErr, ONE_DAY_NANOSECOND, ONE_HOUR_NANOSECOND,
ONE_MICROSECOND_NANOSECOND, ONE_MILLISECOND_NANOSECOND, ONE_MINUTE_NANOSECOND,
ONE_MONTH_NANOSECOND, ONE_SECOND_NANOSECOND, ONE_WEEK_NANOSECOND, ONE_YEAR_NANOSECOND,
};
use std::fmt::Display;
use std::str::FromStr;

#[derive(Debug, Eq, PartialEq, Default, Clone)]
pub(crate) enum TimeUnit {
Year,
Month,
Week,
Day,
Hour,
Minute,
#[default]
Second,
MilliSecond,
MicroSecond,
NanoSecond,
}

impl TimeUnit {
pub(crate) fn duration(&self, time_str: impl AsRef<str>) -> DResult<u64> {
let time = time_str
.as_ref()
.parse::<u64>()
.map_err(|err| DError::ParseError(err.to_string()))?;
let unit = match self {
TimeUnit::Year => ONE_YEAR_NANOSECOND,
TimeUnit::Month => ONE_MONTH_NANOSECOND,
TimeUnit::Week => ONE_WEEK_NANOSECOND,
TimeUnit::Day => ONE_DAY_NANOSECOND,
TimeUnit::Hour => ONE_HOUR_NANOSECOND,
TimeUnit::Minute => ONE_MINUTE_NANOSECOND,
TimeUnit::Second => ONE_SECOND_NANOSECOND,
TimeUnit::MilliSecond => ONE_MILLISECOND_NANOSECOND,
TimeUnit::MicroSecond => ONE_MICROSECOND_NANOSECOND,
TimeUnit::NanoSecond => 1,
};
time.checked_mul(unit).ok_or(DError::OverflowError)
}
}

impl FromStr for TimeUnit {
type Err = DError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match &*s.to_lowercase() {
"y" | "year" => Ok(TimeUnit::Year),
"mon" | "month" => Ok(TimeUnit::Month),
"w" | "week" => Ok(TimeUnit::Week),
"d" | "day" => Ok(TimeUnit::Day),
"h" | "hour" | "hr" => Ok(TimeUnit::Hour),
"m" | "min" | "minute" => Ok(TimeUnit::Minute),
"s" | "sec" | "second" => Ok(TimeUnit::Second),
"ms" | "msec" | "millisecond" => Ok(TimeUnit::MilliSecond),
"µs" | "µsec" | "µsecond" | "us" | "usec" | "usecond" | "microsecond" => {
Ok(TimeUnit::MicroSecond)
}
"ns" | "nsec" | "nanosecond" => Ok(TimeUnit::NanoSecond),
_ => Err(DError::ParseError(Self::expect_err(s))),
}
}
}

impl ExpectErr<11> for TimeUnit {
fn expect_val() -> [&'static str; 11] {
["y", "mon", "w", "d", "h", "m", "s", "ms", "µs", "us", "ns"]
}

fn expect_err<S: AsRef<str> + Display>(s: S) -> String {
format!(
"expect one of :{:?} or their longer forms, but find:{}",
Self::expect_val(),
s,
)
}
}

0 comments on commit f767558

Please sign in to comment.