Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnwa committed Mar 24, 2020
1 parent fa7a768 commit 3bb1acd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ms-converter"
version = "0.7.3"
version = "0.7.4"
authors = ["Mnwa <mihan@panfilov.biz>"]
edition = "2018"
description = "Fast abstraction for converting human-like times into milliseconds."
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ let value = ms_into_time("1d").unwrap();
assert_eq!(value.as_millis(), 86400000)
```

## Supported time strings
* **Years:** `years`, `year`, `yrs`, `yr`, `y`
* **Weeks:** `weeks`, `week`, `w`
* **Days:** `days`, `day`, `d`
* **Hours:** `hours`, `hour`, `hrs`, `hr`, `h`
* **Minutes:** `minutes`, `minute`, `mins`, `min`, `m`
* **Seconds:** `seconds`, `second`, `secs`, `sec`, `s`
* **Milliseconds:** `milliseconds`, `millisecond`, `msecs`, `msec`, `ms` and empty postfix

## Performance
You can check the performance diff between `ms_converter` and `ms` libraries [here](Benchmark.md).

Expand Down
24 changes: 18 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,26 @@ pub const WEEK: f64 = DAY * 7_f64;
/// How many milliseconds in one year
pub const YEAR: f64 = DAY * 365.25_f64;

/// ### Description
/// Fast abstraction for converting human-like times into milliseconds.
/// `ms` function gets an str slice and returns how much milliseconds in your pattern.
///
/// Example:
/// ### Usage
/// ```
/// use crate::ms_converter::ms;
///
/// let value = ms("1d").unwrap();
/// assert_eq!(value, 86400000)
/// ```
///
/// ### Supported time strings
/// * **Years:** `years`, `year`, `yrs`, `yr`, `y`
/// * **Weeks:** `weeks`, `week`, `w`
/// * **Days:** `days`, `day`, `d`
/// * **Hours:** `hours`, `hour`, `hrs`, `hr`, `h`
/// * **Minutes:** `minutes`, `minute`, `mins`, `min`, `m`
/// * **Seconds:** `seconds`, `second`, `secs`, `sec`, `s`
/// * **Milliseconds:** `milliseconds`, `millisecond`, `msecs`, `msec`, `ms` and empty postfix
#[inline(always)]
pub fn ms(s: &str) -> Result<i64, Error> {
let (value, postfix) = s
Expand All @@ -51,12 +61,13 @@ pub fn ms(s: &str) -> Result<i64, Error> {
.map(|v| v.round() as i64)
}

/// ### Description
/// Zero cost converter from human-like time into a number.
/// In the first argument, you need to pass type of your number (`i64`, `f64` and etc).
/// The second argument is human-time construction, like `1 day`, `2 h`.
/// The output will be a number with type what you set in the first argument.
///
/// This macro will be precalculated in compilation time. Also, you can use ms_expr with constants:
/// **This macro will be precalculated in compilation time.** Also, you can use ms_expr with constants:
///
/// ```
/// use crate::ms_converter::ms_expr;
Expand All @@ -65,7 +76,7 @@ pub fn ms(s: &str) -> Result<i64, Error> {
/// assert_eq!(VALUE, 9000000.)
/// ```
///
/// Example:
/// ### Usage
/// ```
/// use crate::ms_converter::ms_expr;
///
Expand Down Expand Up @@ -103,17 +114,18 @@ macro_rules! ms_expr {
}};
}

/// ### Description
/// Ms into time is the abstraction on `ms` function, which converts result into `time.Duration` type.
/// `ms_into_time` function gets an str slice and returns `time.Duration`.
/// `ms_into_time` has some limitations, it's not working with negative values:
/// `ms_into_time` **has some limitations**, it's not working with negative values:
/// ```
/// use crate::ms_converter::ms_into_time;
///
/// let value = ms_into_time("-1d").is_err();
/// assert_eq!(value, true)
/// ```
///
/// Example:
/// ### Usage
/// ```
/// use crate::ms_converter::ms_into_time;
///
Expand All @@ -128,7 +140,7 @@ pub fn ms_into_time(s: &str) -> Result<Duration, Error> {
Ok(Duration::from_millis(milliseconds as u64))
}

/// Error what return ms converter functions in runtime, if something is going wrong.
/// Error which return `ms_converter` functions in runtime, if something is going wrong.
#[derive(Debug)]
pub struct Error {
message: &'static str,
Expand Down

0 comments on commit 3bb1acd

Please sign in to comment.