You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Jonathan Strong
b6d10d07d9
|
2 years ago | |
---|---|---|
src | 2 years ago | |
.gitignore | 2 years ago | |
Cargo.toml | 2 years ago | |
LICENSE | 2 years ago | |
README.md | 2 years ago |
README.md
deserialize-file-size
A serde helper function for deserializing file size input flexibly and robustly.
Accepts either:
- a "human" size string, e.g. "1k", "5mb", "12GiB", etc.
- an integer number of bytes
Example
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 },
);