serde-humanize-rs

Crates.ioserde-humanize-rs
lib.rsserde-humanize-rs
version0.1.1
sourcesrc
created_at2018-09-20 10:20:22.69902
updated_at2018-09-21 17:06:18.604387
description A Serde deserializer for duration and byte-size.
homepagehttps://github.com/dtynn/serde-humanize-rs
repositoryhttps://github.com/dtynn/serde-humanize-rs
max_upload_size
id85609
size11,856
Caesar Wang (dtynn)

documentation

https://docs.rs/serde-humanize-rs

README

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)
);
Commit count: 11

cargo fmt