├── .gitignore ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE ├── README.md └── src ├── bytes ├── mod.rs └── tests.rs ├── duration ├── mod.rs └── tests.rs ├── lib.rs ├── num ├── checked.rs ├── mod.rs └── tests.rs └── time ├── mod.rs ├── tests.rs └── timezone.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ### v0.1.4 2 | implement [RFC3339](https://tools.ietf.org/html/rfc3339) datetime parsing 3 | 4 | ### v0.1.3 5 | add method `size` for `Bytes` 6 | 7 | ### v0.1.2 8 | implement duration parsing 9 | 10 | ### v0.1.1 11 | use `struct Bytes(T)` instead of `struct Bytes(usize)` 12 | 13 | ### v0.1.0 14 | implement bytes parsing 15 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "humanize-rs" 3 | description = """ 4 | For parsing human-readable strings to some types. 5 | """ 6 | readme = "README.md" 7 | license-file = "LICENSE" 8 | version = "0.1.5" 9 | homepage = "https://github.com/dtynn/humanize-rs" 10 | documentation = "https://docs.rs/humanize-rs" 11 | repository = "https://github.com/dtynn/humanize-rs" 12 | authors = ["dtynn "] 13 | keywords = ["humanize", "human", "human-friendly", "readable", "parser"] 14 | categories = ["parsing"] 15 | 16 | [dependencies] 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Caesar Wang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## humanize-rs 2 | [![Crates.io](https://img.shields.io/crates/v/humanize-rs.svg)](https://crates.io/crates/humanize-rs) 3 | [![Docs](https://docs.rs/humanize-rs/badge.svg)](https://docs.rs/humanize-rs) 4 | 5 | This lib aims at converting human-readable strings to specific types. 6 | 7 | It's mainly used in parsing config files. 8 | 9 | ### Usage 10 | 1. Add this lib as a dependency 11 | ``` 12 | [dependencies] 13 | humanize-rs = "0.1" 14 | ``` 15 | 16 | 2. Add the crate reference 17 | ``` 18 | extern crate humanize_rs; 19 | ``` 20 | 21 | 22 | ### Example 23 | 24 | 25 | #### Bytes 26 | ``` 27 | use humanize_rs::bytes::{Bytes, Unit}; 28 | 29 | let gigabytes1 = Bytes::new(1, Unit::GiByte); 30 | let gigabytes2 = "1 GiB".parse::(); 31 | assert_eq!(gigabytes1, gigabytes2); 32 | assert_eq!(gigabytes2.unwrap().size(), 1 << 30); 33 | ``` 34 | 35 | #### Duration 36 | ``` 37 | use humanize_rs::duration::parse; 38 | use std::time::Duration; 39 | 40 | assert_eq!(parse("1h 30m 71s"), Ok(Duration::from_secs(60 * 90 + 71))); 41 | ``` 42 | 43 | #### RFC3339 Datetime 44 | ``` 45 | use humanize_rs::time::{Time, TimeZone}; 46 | 47 | assert_eq!( 48 | "2018-09-21T16:56:44.234867232+08:00".parse::