Crates.io | deserialize-file-size |
lib.rs | deserialize-file-size |
version | 1.0.0 |
source | src |
created_at | 2022-10-08 01:50:01.451479 |
updated_at | 2022-10-08 01:50:01.451479 |
description | A serde helper function for deserializing file size input flexibly and robustly. |
homepage | |
repository | https://git.shipyard.rs/jstrong/deserialize-file-size |
max_upload_size | |
id | 683245 |
size | 9,869 |
A serde helper function for deserializing file size input flexibly and robustly.
Accepts either:
use serde::Deserialize;
use deserialize_file_size::deserialize_file_size;
#[derive(Deserialize, Debug, PartialEq)]
struct FileSize {
#[serde(deserialize_with = "deserialize_file_size")]
sz: usize,
}
let size_str = r#"{"sz": "42mb"}"#;
assert_eq!(
serde_json::from_str::<FileSize>(size_str).unwrap(),
FileSize { sz: 1024 * 1024 * 42 },
);
let int_bytes = r#"{"sz": 4096}"#;
assert_eq!(
serde_json::from_str::<FileSize>(int_bytes).unwrap(),
FileSize { sz: 4096 },
);