Cleaned up documentation, refactored FromStr impl, and added Debug impl

for ByteUnit type
This commit is contained in:
Xavier Moffett 2023-09-30 21:52:31 -04:00
parent 9bc0d7e758
commit 6d04507cb7
3 changed files with 26 additions and 62 deletions

View File

@ -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<T>(input: (f64, f64, i8, bool)) -> (bool, T) where T: From<i64> {
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))
}

View File

@ -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<i64> = "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<i64> = ByteUnit::SI(5000000);
let b: ByteUnit<i64> = ByteUnit::SI(5000000);
let byteunit_sum = a + b;
let a: ByteUnit<i64> = ByteUnit::SI(500000);
let b: ByteUnit<i64> = 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<i64> = "500 KiB".into();
let b: ByteUnit<i64> = "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<i64> = 5000000.to_byteunit(IEC);
let b: ByteUnit<i64> = 5000000.to_byteunit(IEC);
let byteunit_bool = a >= b;
println!("{byteunit_bool}");
assert_eq!(a >= b, true);
```
Output:
```shell
true
```
*/
pub mod simplebyteunit;

View File

@ -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<T> Display for ByteUnit<T> where i64: From<T>, T: Copy {
}
}
impl From<&str> for ByteUnit<i64> where i64: From<i64>, i64: Copy {
/// Debug implementation with a maximum power of 6 (Exa/Exbi)
impl<T> Debug for ByteUnit<T> where i64: From<T>, 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 <T>From<&str> for ByteUnit<T> where i64: From<T>, T: Copy, T: From<i64> {
fn from(value: &str) -> Self {
ByteUnit::from_str(value).unwrap()
}
}
impl FromStr for ByteUnit<i64> where i64: From<i64>, i64: Copy {
impl <T>FromStr for ByteUnit<T> where i64: From<T>, T: Copy, i64: From<T>, T: From<i64> {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let input = input::arithmetic(input::parse(s)?);
let input = input::arithmetic::<T>(input::parse(s)?);
match input.0 {
true => Ok(ByteUnit::IEC(input.1)), false => Ok(ByteUnit::SI(input.1))