diff --git a/Cargo.toml b/Cargo.toml index f7141970..85efc353 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,23 +1,24 @@ [package] name = "bytesize" description = "an utility for human-readable bytes representations" -version = "1.3.0-dev" +version = "1.3.3" authors = ["Hyunsik Choi "] -homepage = "https://github.com/hyunsik/bytesize/" +homepage = "https://github.com/bytesize-rs/bytesize/" documentation = "https://docs.rs/bytesize/" -repository = "https://github.com/hyunsik/bytesize/" +repository = "https://github.com/bytesize-rs/bytesize/" readme = "README.md" keywords = ["byte", "byte-size", "utility", "human-readable", "format"] license = "Apache-2.0" +rust-version = "1.61" [dependencies] -serde = { version = "1.0", optional = true } +serde = { version = "1.0.185", optional = true } [dev-dependencies] -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" -toml = "0.5" +serde = { version = "1.0.185", features = ["derive"] } +serde_json = "1.0.105" +toml = "0.7.6" [features] default = [] diff --git a/src/lib.rs b/src/lib.rs index 01c4440e..25db65b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ //! //! ## Example //! -//! ```ignore +//! ```no_run //! extern crate bytesize; //! //! use bytesize::ByteSize; @@ -14,17 +14,15 @@ //! //! let plus = x + y; //! print!("{} bytes", plus.as_u64()); -//! -//! let minus = ByteSize::tb(100) - ByteSize::gb(4); -//! print!("{} bytes", minus.as_u64()); //! } //! ``` //! //! It also provides its human readable string as follows: //! -//! ```ignore= -//! assert_eq!("482 GiB".to_string(), ByteSize::gb(518).to_string(true)); -//! assert_eq!("518 GB".to_string(), ByteSize::gb(518).to_string(false)); +//! ```no_run +//! # use bytesize::ByteSize; +//! assert_eq!("482 GiB".to_string(), ByteSize::gb(518).to_string()); +//! assert_eq!("518 GB".to_string(), ByteSize::gb(518).to_string()); //! ``` mod parse; @@ -208,7 +206,7 @@ pub fn to_string(bytes: u64, si_prefix: bool) -> String { } impl Display for ByteSize { - fn fmt(&self, f: &mut Formatter) ->fmt::Result { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.pad(&to_string(self.0, false)) } } @@ -261,7 +259,9 @@ impl AddAssign for ByteSize { } impl Add for ByteSize - where T: Into { +where + T: Into, +{ type Output = ByteSize; #[inline(always)] fn add(self, rhs: T) -> ByteSize { @@ -270,7 +270,9 @@ impl Add for ByteSize } impl AddAssign for ByteSize - where T: Into { +where + T: Into, +{ #[inline(always)] fn add_assign(&mut self, rhs: T) { self.0 += rhs.into() as u64; @@ -278,7 +280,9 @@ impl AddAssign for ByteSize } impl Mul for ByteSize - where T: Into { +where + T: Into, +{ type Output = ByteSize; #[inline(always)] fn mul(self, rhs: T) -> ByteSize { @@ -287,7 +291,9 @@ impl Mul for ByteSize } impl MulAssign for ByteSize - where T: Into { +where + T: Into, +{ #[inline(always)] fn mul_assign(&mut self, rhs: T) { self.0 *= rhs.into() as u64; diff --git a/src/parse.rs b/src/parse.rs index a5cca5b3..6296fe38 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -10,9 +10,7 @@ impl std::str::FromStr for ByteSize { let number = take_while(value, |c| c.is_ascii_digit() || c == '.'); match number.parse::() { Ok(v) => { - let suffix = skip_while(value, |c| { - c.is_whitespace() || c.is_ascii_digit() || c == '.' - }); + let suffix = skip_while(&value[number.len()..], char::is_whitespace); match suffix.parse::() { Ok(u) => Ok(Self((v * u) as u64)), Err(error) => Err(format!( @@ -220,6 +218,9 @@ mod tests { assert!(parse("").is_err()); assert!(parse("a124GB").is_err()); + assert!(parse("1.3 42.0 B").is_err()); + assert!(parse("1.3 ... B").is_err()); + assert!(parse("1 000 B").is_err()); } #[test]