2 releases

Uses old Rust 2015

0.1.1 Sep 21, 2018
0.1.0 Sep 20, 2018

#9 in #humanize

Download history 320/week @ 2024-04-11 162/week @ 2024-04-18 253/week @ 2024-04-25 227/week @ 2024-05-02 183/week @ 2024-05-09 368/week @ 2024-05-16 317/week @ 2024-05-23 252/week @ 2024-05-30 206/week @ 2024-06-06 288/week @ 2024-06-13 412/week @ 2024-06-20 504/week @ 2024-06-27 327/week @ 2024-07-04 787/week @ 2024-07-11 561/week @ 2024-07-18 365/week @ 2024-07-25

2,182 downloads per month
Used in 3 crates

Custom license

11KB
242 lines

serde-humanize-rs

A Serde deserializer for duration and byte-size using the crate humanize-rs.

Usage

  1. Add this lib as a dependency
[dependencies]
serde-humanize-rs = "0.1"
  1. Add the crate reference
extern crate serde_humanize_rs;

Example

extern crate serde;
#[macro_use] extern crate serde_derive;
extern crate serde_json;
extern crate serde_humanize_rs;

use std::time::Duration;

#[derive(Deserialize)]
struct Config {
    #[serde(with = "serde_humanize_rs")]
    size: usize,

    #[serde(with = "serde_humanize_rs")]
    interval: Duration,
}

let json = r#"{"size": "1 M", "interval": "1h30m", "close_at": "2105-03-01T10:23:57.000013579+08:00"}"#;
let cfg = serde_json::from_str::<Config>(json).unwrap();
assert_eq!(cfg.size, 1_000_000);
assert_eq!(cfg.interval, Duration::from_secs(5400));
assert_eq!(
    cfg.close_at.duration_since(SystemTime::UNIX_EPOCH).unwrap(),
    Duration::new(4265317437, 000013579)
);

lib.rs:

A Serde deserializer for duration and byte-size using the crate humanize-rs.

Example

extern crate serde;
#[macro_use] extern crate serde_derive;
extern crate serde_json;
extern crate serde_humanize_rs;

use std::time::{Duration, SystemTime};

#[derive(Deserialize)]
struct Config {
    #[serde(with = "serde_humanize_rs")]
    size: usize,

    #[serde(with = "serde_humanize_rs")]
    interval: Duration,

    #[serde(with = "serde_humanize_rs")]
    close_at: SystemTime,
}

let json = r#"{"size": "1 M", "interval": "1h30m", "close_at": "2105-03-01T10:23:57.000013579+08:00"}"#;
let cfg = serde_json::from_str::<Config>(json).unwrap();
assert_eq!(cfg.size, 1_000_000);
assert_eq!(cfg.interval, Duration::from_secs(5400));
assert_eq!(
    cfg.close_at.duration_since(SystemTime::UNIX_EPOCH).unwrap(),
    Duration::new(4265317437, 000013579)
);

Dependencies

~155–390KB