From 77b749c89679c43b2c38cae8c566bec834b899f4 Mon Sep 17 00:00:00 2001 From: Xavier Moffett Date: Sat, 19 Jul 2025 17:43:02 -0400 Subject: [PATCH] chore: Some refactoring work --- src/arithmetic.rs | 48 +++++++++++++++++++++++++++++++ src/lib.rs | 4 +-- src/output.rs | 56 ------------------------------------- src/simplebyteunit.rs | 41 ++++++++++++++------------- src/{input.rs => suffix.rs} | 44 ++++++++++++++++------------- 5 files changed, 97 insertions(+), 96 deletions(-) create mode 100644 src/arithmetic.rs delete mode 100644 src/output.rs rename src/{input.rs => suffix.rs} (73%) diff --git a/src/arithmetic.rs b/src/arithmetic.rs new file mode 100644 index 0000000..2f9c8e8 --- /dev/null +++ b/src/arithmetic.rs @@ -0,0 +1,48 @@ +/* + * SimpleByteUnit + * + * Copyright (C) 2023-2025 Xavier Moffett + * + * 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(input: (f64, f64, i8, bool)) -> (bool, T) +where + T: From, { + 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(value: (T, f64), power_of: i8) -> (i8, f64) +where + i64: From, { + 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), + } +} diff --git a/src/lib.rs b/src/lib.rs index 27fdaaa..ad70fa4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,5 +74,5 @@ assert_eq!(a >= b, true); pub mod simplebyteunit; -mod input; -mod output; +mod arithmetic; +mod suffix; diff --git a/src/output.rs b/src/output.rs deleted file mode 100644 index 687448b..0000000 --- a/src/output.rs +++ /dev/null @@ -1,56 +0,0 @@ -/* - * SimpleByteUnit - * - * Copyright (C) 2023-2025 Xavier Moffett - * - * 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, 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(value: (T, f64), power_of: i8) -> (i8, f64) -where - i64: From, { - 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), - } -} diff --git a/src/simplebyteunit.rs b/src/simplebyteunit.rs index d7c1945..278826f 100644 --- a/src/simplebyteunit.rs +++ b/src/simplebyteunit.rs @@ -15,14 +15,17 @@ Fast, stupid simple ByteUnit implementation Provides a simple way to encapsulate primitives as byteunits. */ - -use crate::{input, output}; use std::{ fmt::{Debug, Display, Formatter}, ops::{Add, Div, Mul, Sub}, str::FromStr, }; +use crate::{ + arithmetic::{divisor, multiplier}, + suffix::{parse, suffix}, +}; + /// IEC ByteUnit (x*1024) pub const IEC: ByteUnit<()> = ByteUnit::IEC(()); /// SI ByteUnit (x*1000) @@ -114,8 +117,8 @@ where let value = arithmetic.1; match power { - B => format!("{:.0} {}", value, output::prefix(self, power)), - _ => format!("{:.2} {}", value, output::prefix(self, power)), + B => format!("{:.0} {}", value, suffix(self, power)), + _ => format!("{:.2} {}", value, suffix(self, power)), } } @@ -129,42 +132,42 @@ where /// Returns a formatted string with up-to a maximum supported power. 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. 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) 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) 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) 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) 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) 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) 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, { 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 index = arithmetic.0; match index { - B => write!(f, "{:.0} {}", bytes, output::prefix(self, index)), - _ => write!(f, "{:.2} {}", bytes, output::prefix(self, index)), + B => write!(f, "{:.0} {}", bytes, suffix(self, index)), + _ => write!(f, "{:.2} {}", bytes, suffix(self, index)), } } } @@ -193,13 +196,13 @@ where T: Copy, { 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 index = arithmetic.0; match index { - B => write!(f, "'{:.0} {}'", bytes, output::prefix(self, index)), - _ => write!(f, "'{:.2} {}'", bytes, output::prefix(self, index)), + B => write!(f, "'{:.0} {}'", bytes, suffix(self, index)), + _ => write!(f, "'{:.2} {}'", bytes, suffix(self, index)), } } } @@ -225,7 +228,7 @@ where type Err = Error; fn from_str(s: &str) -> Result { - let input = input::arithmetic::(input::parse(s)?); + let input = multiplier::(parse(s)?); match input.0 { true => Ok(ByteUnit::IEC(input.1)), diff --git a/src/input.rs b/src/suffix.rs similarity index 73% rename from src/input.rs rename to src/suffix.rs index 18b11c7..b6a6360 100644 --- a/src/input.rs +++ b/src/suffix.rs @@ -12,6 +12,29 @@ use crate::simplebyteunit::*; +pub fn suffix<'a, T: Copy>(unit: &ByteUnit, 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> { let v = match s.to_lowercase() { 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), _ => 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 { true => 1024.0, false => 1000.0, }; - match s.parse() { + match s.trim().parse() { Ok(val) => Ok((val, multiplier, v.1, v.2)), Err(_) => Err(Error::ErroroneousInput(format!("'{s}' contains an invalid float or integer value."))), } } - -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; - let mut value = input.0; - let mut power: i8 = 0; - - while power < power_of { - value *= multiplier; - power += 1; - } - - (iec, T::from(value as i64)) -}