chore: Some refactoring work
This commit is contained in:
parent
1a745cbb2a
commit
77b749c896
5 changed files with 97 additions and 96 deletions
48
src/arithmetic.rs
Normal file
48
src/arithmetic.rs
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
/*
|
||||||
|
* SimpleByteUnit
|
||||||
|
*
|
||||||
|
* Copyright (C) 2023-2025 Xavier Moffett <sapphirus@azorium.net>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
pub fn multiplier<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;
|
||||||
|
let mut value = input.0;
|
||||||
|
let mut power: i8 = 0;
|
||||||
|
|
||||||
|
while power < power_of {
|
||||||
|
value *= multiplier;
|
||||||
|
power += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
(iec, T::from(value as i64))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn divisor<T: Copy>(value: (T, f64), power_of: i8) -> (i8, f64)
|
||||||
|
where
|
||||||
|
i64: From<T>, {
|
||||||
|
let bytes: i64 = value.0.into();
|
||||||
|
let divisor = value.1;
|
||||||
|
let positive = bytes > -1;
|
||||||
|
let mut bytes: f64 = if positive { bytes } else { -bytes } as f64;
|
||||||
|
let mut power = 0;
|
||||||
|
|
||||||
|
while bytes >= divisor && power < power_of {
|
||||||
|
bytes /= divisor;
|
||||||
|
power += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
match positive {
|
||||||
|
true => (power, bytes),
|
||||||
|
false => (power, -bytes),
|
||||||
|
}
|
||||||
|
}
|
|
@ -74,5 +74,5 @@ assert_eq!(a >= b, true);
|
||||||
|
|
||||||
pub mod simplebyteunit;
|
pub mod simplebyteunit;
|
||||||
|
|
||||||
mod input;
|
mod arithmetic;
|
||||||
mod output;
|
mod suffix;
|
||||||
|
|
|
@ -1,56 +0,0 @@
|
||||||
/*
|
|
||||||
* SimpleByteUnit
|
|
||||||
*
|
|
||||||
* Copyright (C) 2023-2025 Xavier Moffett <sapphirus@azorium.net>
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
use crate::simplebyteunit::*;
|
|
||||||
|
|
||||||
pub fn prefix<'a, T: Copy>(unit: &ByteUnit<T>, i: i8) -> &'a str {
|
|
||||||
match unit {
|
|
||||||
ByteUnit::IEC(_) => match i {
|
|
||||||
K => "KiB",
|
|
||||||
M => "MiB",
|
|
||||||
G => "GiB",
|
|
||||||
T => "TiB",
|
|
||||||
P => "PiB",
|
|
||||||
E => "EiB",
|
|
||||||
_ => "B",
|
|
||||||
},
|
|
||||||
ByteUnit::SI(_) => match i {
|
|
||||||
K => "kB",
|
|
||||||
M => "MB",
|
|
||||||
G => "GB",
|
|
||||||
T => "TB",
|
|
||||||
P => "PB",
|
|
||||||
E => "EB",
|
|
||||||
_ => "B",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn arithmetic<T: Copy>(value: (T, f64), power_of: i8) -> (i8, f64)
|
|
||||||
where
|
|
||||||
i64: From<T>, {
|
|
||||||
let bytes: i64 = value.0.into();
|
|
||||||
let diviser = value.1;
|
|
||||||
let positive = bytes > -1;
|
|
||||||
let mut bytes: f64 = if positive { bytes } else { -bytes } as f64;
|
|
||||||
let mut power = 0;
|
|
||||||
|
|
||||||
while bytes >= diviser && power < power_of {
|
|
||||||
bytes /= diviser;
|
|
||||||
power += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
match positive {
|
|
||||||
true => (power, bytes),
|
|
||||||
false => (power, -bytes),
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -15,14 +15,17 @@ Fast, stupid simple ByteUnit implementation
|
||||||
|
|
||||||
Provides a simple way to encapsulate primitives as byteunits.
|
Provides a simple way to encapsulate primitives as byteunits.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use crate::{input, output};
|
|
||||||
use std::{
|
use std::{
|
||||||
fmt::{Debug, Display, Formatter},
|
fmt::{Debug, Display, Formatter},
|
||||||
ops::{Add, Div, Mul, Sub},
|
ops::{Add, Div, Mul, Sub},
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
arithmetic::{divisor, multiplier},
|
||||||
|
suffix::{parse, suffix},
|
||||||
|
};
|
||||||
|
|
||||||
/// IEC ByteUnit (x*1024)
|
/// IEC ByteUnit (x*1024)
|
||||||
pub const IEC: ByteUnit<()> = ByteUnit::IEC(());
|
pub const IEC: ByteUnit<()> = ByteUnit::IEC(());
|
||||||
/// SI ByteUnit (x*1000)
|
/// SI ByteUnit (x*1000)
|
||||||
|
@ -114,8 +117,8 @@ where
|
||||||
let value = arithmetic.1;
|
let value = arithmetic.1;
|
||||||
|
|
||||||
match power {
|
match power {
|
||||||
B => format!("{:.0} {}", value, output::prefix(self, power)),
|
B => format!("{:.0} {}", value, suffix(self, power)),
|
||||||
_ => format!("{:.2} {}", value, output::prefix(self, power)),
|
_ => format!("{:.2} {}", value, suffix(self, power)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,42 +132,42 @@ where
|
||||||
|
|
||||||
/// Returns a formatted string with up-to a maximum supported power.
|
/// Returns a formatted string with up-to a maximum supported power.
|
||||||
pub fn max(&self) -> String {
|
pub fn max(&self) -> String {
|
||||||
self.format(output::arithmetic(self.value(), MAX))
|
self.format(divisor(self.value(), MAX))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a formatted string with a maximum of the specified power.
|
/// Returns a formatted string with a maximum of the specified power.
|
||||||
pub fn pow(&self, power_of: i8) -> String {
|
pub fn pow(&self, power_of: i8) -> String {
|
||||||
self.format(output::arithmetic(self.value(), power_of))
|
self.format(divisor(self.value(), power_of))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a formatted string with a maximum power of 1 (Kilo/Kibi)
|
/// Returns a formatted string with a maximum power of 1 (Kilo/Kibi)
|
||||||
pub fn k(&self) -> String {
|
pub fn k(&self) -> String {
|
||||||
self.format(output::arithmetic(self.value(), K))
|
self.format(divisor(self.value(), K))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a formatted string with a maximum power of 2 (Mega/Mebi)
|
/// Returns a formatted string with a maximum power of 2 (Mega/Mebi)
|
||||||
pub fn m(&self) -> String {
|
pub fn m(&self) -> String {
|
||||||
self.format(output::arithmetic(self.value(), M))
|
self.format(divisor(self.value(), M))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a formatted string with a maximum power of 3 (Giga/Gibi)
|
/// Returns a formatted string with a maximum power of 3 (Giga/Gibi)
|
||||||
pub fn g(&self) -> String {
|
pub fn g(&self) -> String {
|
||||||
self.format(output::arithmetic(self.value(), G))
|
self.format(divisor(self.value(), G))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a formatted string with a maximum power of 4 (Tera/Tebi)
|
/// Returns a formatted string with a maximum power of 4 (Tera/Tebi)
|
||||||
pub fn p(&self) -> String {
|
pub fn p(&self) -> String {
|
||||||
self.format(output::arithmetic(self.value(), P))
|
self.format(divisor(self.value(), P))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a formatted string with a maximum power of 5 (Peta/Pebi)
|
/// Returns a formatted string with a maximum power of 5 (Peta/Pebi)
|
||||||
pub fn t(&self) -> String {
|
pub fn t(&self) -> String {
|
||||||
self.format(output::arithmetic(self.value(), T))
|
self.format(divisor(self.value(), T))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a formatted string with a maximum power of 6 (Exa/Exbi)
|
/// Returns a formatted string with a maximum power of 6 (Exa/Exbi)
|
||||||
pub fn e(&self) -> String {
|
pub fn e(&self) -> String {
|
||||||
self.format(output::arithmetic(self.value(), E))
|
self.format(divisor(self.value(), E))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,13 +178,13 @@ where
|
||||||
T: Copy,
|
T: Copy,
|
||||||
{
|
{
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
let arithmetic = output::arithmetic(self.value(), MAX);
|
let arithmetic = divisor(self.value(), MAX);
|
||||||
let bytes = arithmetic.1;
|
let bytes = arithmetic.1;
|
||||||
let index = arithmetic.0;
|
let index = arithmetic.0;
|
||||||
|
|
||||||
match index {
|
match index {
|
||||||
B => write!(f, "{:.0} {}", bytes, output::prefix(self, index)),
|
B => write!(f, "{:.0} {}", bytes, suffix(self, index)),
|
||||||
_ => write!(f, "{:.2} {}", bytes, output::prefix(self, index)),
|
_ => write!(f, "{:.2} {}", bytes, suffix(self, index)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -193,13 +196,13 @@ where
|
||||||
T: Copy,
|
T: Copy,
|
||||||
{
|
{
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
let arithmetic = output::arithmetic(self.value(), MAX);
|
let arithmetic = divisor(self.value(), MAX);
|
||||||
let bytes = arithmetic.1;
|
let bytes = arithmetic.1;
|
||||||
let index = arithmetic.0;
|
let index = arithmetic.0;
|
||||||
|
|
||||||
match index {
|
match index {
|
||||||
B => write!(f, "'{:.0} {}'", bytes, output::prefix(self, index)),
|
B => write!(f, "'{:.0} {}'", bytes, suffix(self, index)),
|
||||||
_ => write!(f, "'{:.2} {}'", bytes, output::prefix(self, index)),
|
_ => write!(f, "'{:.2} {}'", bytes, suffix(self, index)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -225,7 +228,7 @@ where
|
||||||
type Err = Error;
|
type Err = Error;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
let input = input::arithmetic::<T>(input::parse(s)?);
|
let input = multiplier::<T>(parse(s)?);
|
||||||
|
|
||||||
match input.0 {
|
match input.0 {
|
||||||
true => Ok(ByteUnit::IEC(input.1)),
|
true => Ok(ByteUnit::IEC(input.1)),
|
||||||
|
|
|
@ -12,6 +12,29 @@
|
||||||
|
|
||||||
use crate::simplebyteunit::*;
|
use crate::simplebyteunit::*;
|
||||||
|
|
||||||
|
pub fn suffix<'a, T: Copy>(unit: &ByteUnit<T>, i: i8) -> &'a str {
|
||||||
|
match unit {
|
||||||
|
ByteUnit::IEC(_) => match i {
|
||||||
|
K => "KiB",
|
||||||
|
M => "MiB",
|
||||||
|
G => "GiB",
|
||||||
|
T => "TiB",
|
||||||
|
P => "PiB",
|
||||||
|
E => "EiB",
|
||||||
|
_ => "B",
|
||||||
|
},
|
||||||
|
ByteUnit::SI(_) => match i {
|
||||||
|
K => "kB",
|
||||||
|
M => "MB",
|
||||||
|
G => "GB",
|
||||||
|
T => "TB",
|
||||||
|
P => "PB",
|
||||||
|
E => "EB",
|
||||||
|
_ => "B",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parse(s: &str) -> Result<(f64, f64, i8, bool), Error> {
|
pub fn parse(s: &str) -> Result<(f64, f64, i8, bool), Error> {
|
||||||
let v = match s.to_lowercase() {
|
let v = match s.to_lowercase() {
|
||||||
string if string.ends_with("kib") => ("kib", K, true),
|
string if string.ends_with("kib") => ("kib", K, true),
|
||||||
|
@ -29,31 +52,14 @@ pub fn parse(s: &str) -> Result<(f64, f64, i8, bool), Error> {
|
||||||
string if string.ends_with("b") => ("b", B, false),
|
string if string.ends_with("b") => ("b", B, false),
|
||||||
_ => Err(Error::InvalidUnit(format!("'{s}' contains no supported nor valid byteunits.")))?,
|
_ => Err(Error::InvalidUnit(format!("'{s}' contains no supported nor valid byteunits.")))?,
|
||||||
};
|
};
|
||||||
let s = s.to_lowercase().replace(v.0, "").replace(" ", "");
|
let s = s.to_lowercase().replace(v.0, "");
|
||||||
let multiplier = match v.2 {
|
let multiplier = match v.2 {
|
||||||
true => 1024.0,
|
true => 1024.0,
|
||||||
false => 1000.0,
|
false => 1000.0,
|
||||||
};
|
};
|
||||||
|
|
||||||
match s.parse() {
|
match s.trim().parse() {
|
||||||
Ok(val) => Ok((val, multiplier, v.1, v.2)),
|
Ok(val) => Ok((val, multiplier, v.1, v.2)),
|
||||||
Err(_) => Err(Error::ErroroneousInput(format!("'{s}' contains an invalid float or integer value."))),
|
Err(_) => Err(Error::ErroroneousInput(format!("'{s}' contains an invalid float or integer value."))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
let mut value = input.0;
|
|
||||||
let mut power: i8 = 0;
|
|
||||||
|
|
||||||
while power < power_of {
|
|
||||||
value *= multiplier;
|
|
||||||
power += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
(iec, T::from(value as i64))
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue