diff --git a/src/input.rs b/src/input.rs index 70ce12f..943e447 100644 --- a/src/input.rs +++ b/src/input.rs @@ -40,7 +40,7 @@ pub fn parse(s: &str) -> Result<(f64, f64, i8, bool), Error> { } } -pub fn arithmetic(input: (f64, f64, i8, bool)) -> (bool, i64) { +pub fn arithmetic(input: (f64, f64, i8, bool)) -> (bool, T) where T: From { let iec = input.3; let power_of = input.2; let multiplier = input.1; @@ -52,5 +52,5 @@ pub fn arithmetic(input: (f64, f64, i8, bool)) -> (bool, i64) { power += 1; } - (iec, value as i64) + (iec, T::from(value as i64)) } diff --git a/src/lib.rs b/src/lib.rs index 5d77dfd..a3b1c8e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,41 +26,14 @@ simplebyteunit = "0.2.0" Generate a human-readable, formatted ByteUnit: - ```rust use simplebyteunit::simplebyteunit::*; let byteunit_var = 500000.to_byteunit(SI); -println!("{byteunit_var}"); - +assert_eq!(byteunit_var.to_string(), "500.00 kB"); ``` -Output: - -```shell -500 kB -```` - -## Parsing strings into ByteUnits - -And then you can parse formatted strings back into a ByteUnit - -```rust -use simplebyteunit::simplebyteunit::*; - -let byteunit_var: ByteUnit = "500 kB".into(); - -println!("{byteunit_var}"); - -``` - -Output: - -```shell -500 kB -```` - ## Simple arithmetic operations Addition, subtraction, multiplication, subtraction, and division are supported on this type. @@ -68,18 +41,10 @@ Addition, subtraction, multiplication, subtraction, and division are supported o ```rust use simplebyteunit::simplebyteunit::*; -let a: ByteUnit = ByteUnit::SI(5000000); -let b: ByteUnit = ByteUnit::SI(5000000); -let byteunit_sum = a + b; +let a: ByteUnit = ByteUnit::SI(500000); +let b: ByteUnit = ByteUnit::SI(500000); -println!("{byteunit_sum}"); - -``` - -Output: - -```shell -1.0 MB +assert_eq!(a + b, "1.0 MB".into()); ``` ## Equal/and/or operations @@ -91,17 +56,10 @@ use simplebyteunit::simplebyteunit::*; let a: ByteUnit = "500 KiB".into(); let b: ByteUnit = "500 KiB".into(); -let byteunit_bool = a == b; - -println!("{byteunit_bool}"); +assert_eq!(a == b, true); ``` -Output: -```shell -true -```` - Or operations are also supported on this type: ```rust @@ -109,17 +67,9 @@ use simplebyteunit::simplebyteunit::*; let a: ByteUnit = 5000000.to_byteunit(IEC); let b: ByteUnit = 5000000.to_byteunit(IEC); -let byteunit_bool = a >= b; - -println!("{byteunit_bool}"); +assert_eq!(a >= b, true); ``` - -Output: -```shell -true -``` - */ pub mod simplebyteunit; diff --git a/src/simplebyteunit.rs b/src/simplebyteunit.rs index 0bbe8b2..5cb2e30 100644 --- a/src/simplebyteunit.rs +++ b/src/simplebyteunit.rs @@ -16,7 +16,7 @@ Fast, stupid simple ByteUnit implementation Provides a simple way to encapsulate primitives as byteunits. */ -use std::{fmt::{Display, Formatter}, +use std::{fmt::{Display, Formatter, Debug}, ops::{Mul, Div, Add, Sub}, str::FromStr}; use crate::{input, output}; @@ -166,17 +166,31 @@ impl Display for ByteUnit where i64: From, T: Copy { } } -impl From<&str> for ByteUnit where i64: From, i64: Copy { +/// Debug implementation with a maximum power of 6 (Exa/Exbi) +impl Debug for ByteUnit where i64: From, T: Copy { + fn fmt(&self, f:&mut Formatter<'_>) -> std::fmt::Result { + let arithmetic = output::arithmetic(self.value(), MAX); + let bytes = arithmetic.1; + let index = arithmetic.0; + + match index { + B => write!(f, "'{:.0} {}'", bytes, output::prefix(self, index)), + _ => write!(f, "'{:.2} {}'", bytes, output::prefix(self, index)), + } + } +} + +impl From<&str> for ByteUnit where i64: From, T: Copy, T: From { fn from(value: &str) -> Self { ByteUnit::from_str(value).unwrap() } } -impl FromStr for ByteUnit where i64: From, i64: Copy { +impl FromStr for ByteUnit where i64: From, T: Copy, i64: From, T: From { type Err = Error; fn from_str(s: &str) -> Result { - let input = input::arithmetic(input::parse(s)?); + let input = input::arithmetic::(input::parse(s)?); match input.0 { true => Ok(ByteUnit::IEC(input.1)), false => Ok(ByteUnit::SI(input.1))