#serde #humanize #deserialize #human-friendly #readable

serde-humanize-rs

用于时长和字节数的Serde反序列化器

2个版本

使用旧Rust 2015

0.1.1 2018年9月21日
0.1.0 2018年9月20日

#1085 in 编码

Download history 370/week @ 2024-04-06 289/week @ 2024-04-13 186/week @ 2024-04-20 231/week @ 2024-04-27 186/week @ 2024-05-04 258/week @ 2024-05-11 344/week @ 2024-05-18 315/week @ 2024-05-25 246/week @ 2024-06-01 175/week @ 2024-06-08 412/week @ 2024-06-15 378/week @ 2024-06-22 464/week @ 2024-06-29 371/week @ 2024-07-06 924/week @ 2024-07-13 353/week @ 2024-07-20

2,166 monthly downloads
3 个crate中使用

自定义许可

11KB
242

serde-humanize-rs

使用crate humanize-rs 实现时长和字节数的Serde反序列化器。

用法

  1. 将此库作为依赖项添加
[dependencies]
serde-humanize-rs = "0.1"
  1. 添加crate引用
extern crate serde_humanize_rs;

示例

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:

使用crate humanize-rs 实现时长和字节数的Serde反序列化器。

示例

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)
);

依赖项

~155–395KB