chore: Fix formatting
This commit is contained in:
parent
09c330312f
commit
30ad3e7d5c
6 changed files with 201 additions and 120 deletions
16
.rustfmt.toml
Normal file
16
.rustfmt.toml
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
unstable_features = true
|
||||||
|
indent_style = "Block"
|
||||||
|
imports_indent = "Block"
|
||||||
|
imports_layout = "HorizontalVertical"
|
||||||
|
imports_granularity = "Crate"
|
||||||
|
brace_style = "PreferSameLine"
|
||||||
|
match_arm_leading_pipes = "Never"
|
||||||
|
match_arm_blocks = false
|
||||||
|
condense_wildcard_suffixes = true
|
||||||
|
overflow_delimited_expr = false
|
||||||
|
spaces_around_ranges = true
|
||||||
|
reorder_imports = true
|
||||||
|
hard_tabs = false
|
||||||
|
max_width = 130
|
||||||
|
fn_call_width = 120
|
||||||
|
chain_width = 90
|
23
src/input.rs
23
src/input.rs
|
@ -1,8 +1,8 @@
|
||||||
/*
|
/*
|
||||||
* SimpleByteUnit
|
* SimpleByteUnit
|
||||||
*
|
*
|
||||||
* Copyright (C) 2023-2025 Xavier Moffett <sapphirus@azorium.net>
|
* Copyright (C) 2023-2025 Xavier Moffett <sapphirus@azorium.net>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
|
@ -27,20 +27,23 @@ pub fn parse(s: &str) -> Result<(f64, f64, i8, bool), Error> {
|
||||||
string if string.ends_with("pb") => ("pb", P, false),
|
string if string.ends_with("pb") => ("pb", P, false),
|
||||||
string if string.ends_with("eb") => ("eb", E, false),
|
string if string.ends_with("eb") => ("eb", E, false),
|
||||||
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 multiplier = match v.2 {
|
||||||
|
true => 1024.0,
|
||||||
|
false => 1000.0,
|
||||||
};
|
};
|
||||||
let s = s.to_lowercase()
|
|
||||||
.replace(v.0, "")
|
|
||||||
.replace(" ", "");
|
|
||||||
let multiplier = match v.2 { true => 1024.0, false => 1000.0 };
|
|
||||||
|
|
||||||
match s.parse() {
|
match s.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> {
|
pub fn arithmetic<T>(input: (f64, f64, i8, bool)) -> (bool, T)
|
||||||
|
where
|
||||||
|
T: From<i64>, {
|
||||||
let iec = input.3;
|
let iec = input.3;
|
||||||
let power_of = input.2;
|
let power_of = input.2;
|
||||||
let multiplier = input.1;
|
let multiplier = input.1;
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
/*
|
/*
|
||||||
* SimpleByteUnit
|
* SimpleByteUnit
|
||||||
*
|
*
|
||||||
* Copyright (C) 2023-2025 Xavier Moffett <sapphirus@azorium.net>
|
* Copyright (C) 2023-2025 Xavier Moffett <sapphirus@azorium.net>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
|
@ -24,7 +24,7 @@ simplebyteunit = "0.2.0"
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
Generate a human-readable, formatted ByteUnit:
|
Generate a human-readable, formatted ByteUnit:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use simplebyteunit::simplebyteunit::*;
|
use simplebyteunit::simplebyteunit::*;
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
/*
|
/*
|
||||||
* SimpleByteUnit
|
* SimpleByteUnit
|
||||||
*
|
*
|
||||||
* Copyright (C) 2023-2025 Xavier Moffett <sapphirus@azorium.net>
|
* Copyright (C) 2023-2025 Xavier Moffett <sapphirus@azorium.net>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
|
@ -21,7 +21,7 @@ pub fn prefix<'a, T: Copy>(unit: &ByteUnit<T>, i: i8) -> &'a str {
|
||||||
T => "TiB",
|
T => "TiB",
|
||||||
P => "PiB",
|
P => "PiB",
|
||||||
E => "EiB",
|
E => "EiB",
|
||||||
_ => "B"
|
_ => "B",
|
||||||
},
|
},
|
||||||
ByteUnit::SI(_) => match i {
|
ByteUnit::SI(_) => match i {
|
||||||
K => "kB",
|
K => "kB",
|
||||||
|
@ -30,22 +30,27 @@ pub fn prefix<'a, T: Copy>(unit: &ByteUnit<T>, i: i8) -> &'a str {
|
||||||
T => "TB",
|
T => "TB",
|
||||||
P => "PB",
|
P => "PB",
|
||||||
E => "EB",
|
E => "EB",
|
||||||
_ => "B"
|
_ => "B",
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn arithmetic<T: Copy>(value: (T, f64), power_of: i8) -> (i8, f64) where i64: From<T> {
|
pub fn arithmetic<T: Copy>(value: (T, f64), power_of: i8) -> (i8, f64)
|
||||||
let bytes: i64 = value.0.into();
|
where
|
||||||
let diviser = value.1;
|
i64: From<T>, {
|
||||||
|
let bytes: i64 = value.0.into();
|
||||||
|
let diviser = value.1;
|
||||||
let positive = bytes > -1;
|
let positive = bytes > -1;
|
||||||
let mut bytes: f64 = if positive { bytes } else { -bytes } as f64;
|
let mut bytes: f64 = if positive { bytes } else { -bytes } as f64;
|
||||||
let mut power = 0;
|
let mut power = 0;
|
||||||
|
|
||||||
while bytes >= diviser && power < power_of {
|
while bytes >= diviser && power < power_of {
|
||||||
bytes /= diviser;
|
bytes /= diviser;
|
||||||
power += 1;
|
power += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
match positive { true => (power, bytes), false => (power, -bytes) }
|
match positive {
|
||||||
|
true => (power, bytes),
|
||||||
|
false => (power, -bytes),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
/*
|
/*
|
||||||
* SimpleByteUnit
|
* SimpleByteUnit
|
||||||
*
|
*
|
||||||
* Copyright (C) 2023-2025 Xavier Moffett <sapphirus@azorium.net>
|
* Copyright (C) 2023-2025 Xavier Moffett <sapphirus@azorium.net>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
|
@ -10,15 +10,18 @@
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Fast, stupid simple ByteUnit implementation
|
Fast, stupid simple ByteUnit implementation
|
||||||
|
|
||||||
Provides a simple way to encapsulate primitives as byteunits.
|
Provides a simple way to encapsulate primitives as byteunits.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use std::{fmt::{Display, Formatter, Debug},
|
|
||||||
ops::{Mul, Div, Add, Sub}, str::FromStr};
|
|
||||||
use crate::{input, output};
|
use crate::{input, output};
|
||||||
|
use std::{
|
||||||
|
fmt::{Debug, Display, Formatter},
|
||||||
|
ops::{Add, Div, Mul, Sub},
|
||||||
|
str::FromStr,
|
||||||
|
};
|
||||||
|
|
||||||
/// IEC ByteUnit (x*1024)
|
/// IEC ByteUnit (x*1024)
|
||||||
pub const IEC: ByteUnit<()> = ByteUnit::IEC(());
|
pub const IEC: ByteUnit<()> = ByteUnit::IEC(());
|
||||||
|
@ -36,12 +39,12 @@ pub const G: i8 = 3;
|
||||||
pub const M: i8 = 2;
|
pub const M: i8 = 2;
|
||||||
/// Power of 1 (Kilo/Kibi)
|
/// Power of 1 (Kilo/Kibi)
|
||||||
pub const K: i8 = 1;
|
pub const K: i8 = 1;
|
||||||
/// Base unit
|
/// Base unit
|
||||||
pub const B: i8 = 0;
|
pub const B: i8 = 0;
|
||||||
/// Maximum supported power
|
/// Maximum supported power
|
||||||
pub const MAX: i8 = E;
|
pub const MAX: i8 = E;
|
||||||
|
|
||||||
/// Thin encapsulate of a supported, primitive integer to provide simple byteunit facilities
|
/// Thin encapsulate of a supported, primitive integer to provide simple byteunit facilities
|
||||||
pub enum ByteUnit<T: Copy> {
|
pub enum ByteUnit<T: Copy> {
|
||||||
IEC(T),
|
IEC(T),
|
||||||
SI(T),
|
SI(T),
|
||||||
|
@ -58,45 +61,58 @@ pub trait ToByteUnit<T: Copy> {
|
||||||
fn to_byteunit(self, byte: ByteUnit<()>) -> ByteUnit<T>;
|
fn to_byteunit(self, byte: ByteUnit<()>) -> ByteUnit<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToByteUnit<u32> for u32 where i64: From<u32> {
|
impl ToByteUnit<u32> for u32
|
||||||
|
where
|
||||||
|
i64: From<u32>,
|
||||||
|
{
|
||||||
fn to_byteunit(self, unit: ByteUnit<()>) -> ByteUnit<u32> {
|
fn to_byteunit(self, unit: ByteUnit<()>) -> ByteUnit<u32> {
|
||||||
match unit {
|
match unit {
|
||||||
ByteUnit::IEC(()) => ByteUnit::IEC(self),
|
ByteUnit::IEC(()) => ByteUnit::IEC(self),
|
||||||
ByteUnit::SI(()) => ByteUnit::SI(self),
|
ByteUnit::SI(()) => ByteUnit::SI(self),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToByteUnit<i32> for i32 where i64: From<i32> {
|
impl ToByteUnit<i32> for i32
|
||||||
|
where
|
||||||
|
i64: From<i32>,
|
||||||
|
{
|
||||||
fn to_byteunit(self, unit: ByteUnit<()>) -> ByteUnit<i32> {
|
fn to_byteunit(self, unit: ByteUnit<()>) -> ByteUnit<i32> {
|
||||||
match unit {
|
match unit {
|
||||||
ByteUnit::IEC(()) => ByteUnit::IEC(self),
|
ByteUnit::IEC(()) => ByteUnit::IEC(self),
|
||||||
ByteUnit::SI(()) => ByteUnit::SI(self),
|
ByteUnit::SI(()) => ByteUnit::SI(self),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToByteUnit<i64> for i64 where i64: From<i64> {
|
impl ToByteUnit<i64> for i64
|
||||||
|
where
|
||||||
|
i64: From<i64>,
|
||||||
|
{
|
||||||
fn to_byteunit(self, unit: ByteUnit<()>) -> ByteUnit<i64> {
|
fn to_byteunit(self, unit: ByteUnit<()>) -> ByteUnit<i64> {
|
||||||
match unit {
|
match unit {
|
||||||
ByteUnit::IEC(()) => ByteUnit::IEC(self),
|
ByteUnit::IEC(()) => ByteUnit::IEC(self),
|
||||||
ByteUnit::SI(()) => ByteUnit::SI(self),
|
ByteUnit::SI(()) => ByteUnit::SI(self),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T>ByteUnit<T> where i64: From<T>, T: Copy {
|
impl<T> ByteUnit<T>
|
||||||
|
where
|
||||||
|
i64: From<T>,
|
||||||
|
T: Copy,
|
||||||
|
{
|
||||||
fn value(&self) -> (T, f64) {
|
fn value(&self) -> (T, f64) {
|
||||||
match self {
|
match self {
|
||||||
Self::IEC(val) => (*val, 1024.0),
|
Self::IEC(val) => (*val, 1024.0),
|
||||||
Self::SI(val) => (*val, 1000.0)
|
Self::SI(val) => (*val, 1000.0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format(&self, arithmetic: (i8, f64)) -> String {
|
fn format(&self, arithmetic: (i8, f64)) -> String {
|
||||||
let power = arithmetic.0;
|
let power = arithmetic.0;
|
||||||
let value = arithmetic.1;
|
let value = arithmetic.1;
|
||||||
|
|
||||||
match power {
|
match power {
|
||||||
B => format!("{:.0} {}", value, output::prefix(self, power)),
|
B => format!("{:.0} {}", value, output::prefix(self, power)),
|
||||||
_ => format!("{:.2} {}", value, output::prefix(self, power)),
|
_ => format!("{:.2} {}", value, output::prefix(self, power)),
|
||||||
|
@ -105,10 +121,10 @@ impl <T>ByteUnit<T> where i64: From<T>, T: Copy {
|
||||||
|
|
||||||
/// Acquire and return base value of encapsulated primitive
|
/// Acquire and return base value of encapsulated primitive
|
||||||
pub fn val(&self) -> T {
|
pub fn val(&self) -> T {
|
||||||
match self {
|
match self {
|
||||||
Self::IEC(val) => *val,
|
Self::IEC(val) => *val,
|
||||||
Self::SI(val) => *val,
|
Self::SI(val) => *val,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a formatted string with up-to a maximum supported power.
|
/// Returns a formatted string with up-to a maximum supported power.
|
||||||
|
@ -121,7 +137,7 @@ impl <T>ByteUnit<T> where i64: From<T>, T: Copy {
|
||||||
self.format(output::arithmetic(self.value(), power_of))
|
self.format(output::arithmetic(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(output::arithmetic(self.value(), K))
|
||||||
}
|
}
|
||||||
|
@ -131,17 +147,17 @@ impl <T>ByteUnit<T> where i64: From<T>, T: Copy {
|
||||||
self.format(output::arithmetic(self.value(), M))
|
self.format(output::arithmetic(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(output::arithmetic(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(output::arithmetic(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(output::arithmetic(self.value(), T))
|
||||||
}
|
}
|
||||||
|
@ -153,8 +169,12 @@ impl <T>ByteUnit<T> where i64: From<T>, T: Copy {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Display implementation with a maximum power of 6 (Exa/Exbi)
|
/// Display implementation with a maximum power of 6 (Exa/Exbi)
|
||||||
impl<T> Display for ByteUnit<T> where i64: From<T>, T: Copy {
|
impl<T> Display for ByteUnit<T>
|
||||||
fn fmt(&self, f:&mut Formatter<'_>) -> std::fmt::Result {
|
where
|
||||||
|
i64: From<T>,
|
||||||
|
T: Copy,
|
||||||
|
{
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
let arithmetic = output::arithmetic(self.value(), MAX);
|
let arithmetic = output::arithmetic(self.value(), MAX);
|
||||||
let bytes = arithmetic.1;
|
let bytes = arithmetic.1;
|
||||||
let index = arithmetic.0;
|
let index = arithmetic.0;
|
||||||
|
@ -167,8 +187,12 @@ impl<T> Display for ByteUnit<T> where i64: From<T>, T: Copy {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Debug implementation with a maximum power of 6 (Exa/Exbi)
|
/// Debug implementation with a maximum power of 6 (Exa/Exbi)
|
||||||
impl<T> Debug for ByteUnit<T> where i64: From<T>, T: Copy {
|
impl<T> Debug for ByteUnit<T>
|
||||||
fn fmt(&self, f:&mut Formatter<'_>) -> std::fmt::Result {
|
where
|
||||||
|
i64: From<T>,
|
||||||
|
T: Copy,
|
||||||
|
{
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
let arithmetic = output::arithmetic(self.value(), MAX);
|
let arithmetic = output::arithmetic(self.value(), MAX);
|
||||||
let bytes = arithmetic.1;
|
let bytes = arithmetic.1;
|
||||||
let index = arithmetic.0;
|
let index = arithmetic.0;
|
||||||
|
@ -180,26 +204,42 @@ impl<T> Debug for ByteUnit<T> where i64: From<T>, T: Copy {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T>From<&str> for ByteUnit<T> where i64: From<T>, T: Copy, T: From<i64> {
|
impl<T> From<&str> for ByteUnit<T>
|
||||||
|
where
|
||||||
|
i64: From<T>,
|
||||||
|
T: Copy,
|
||||||
|
T: From<i64>,
|
||||||
|
{
|
||||||
fn from(value: &str) -> Self {
|
fn from(value: &str) -> Self {
|
||||||
ByteUnit::from_str(value).unwrap()
|
ByteUnit::from_str(value).unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T>FromStr for ByteUnit<T> where i64: From<T>, T: Copy, i64: From<T>, T: From<i64> {
|
impl<T> FromStr for ByteUnit<T>
|
||||||
|
where
|
||||||
|
i64: From<T>,
|
||||||
|
T: Copy,
|
||||||
|
i64: From<T>,
|
||||||
|
T: From<i64>,
|
||||||
|
{
|
||||||
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 = input::arithmetic::<T>(input::parse(s)?);
|
||||||
|
|
||||||
match input.0 {
|
match input.0 {
|
||||||
true => Ok(ByteUnit::IEC(input.1)), false => Ok(ByteUnit::SI(input.1))
|
true => Ok(ByteUnit::IEC(input.1)),
|
||||||
|
false => Ok(ByteUnit::SI(input.1)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T>PartialOrd for ByteUnit<T> where i64: From<T>, T: Copy + PartialEq {
|
impl<T> PartialOrd for ByteUnit<T>
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
where
|
||||||
|
i64: From<T>,
|
||||||
|
T: Copy + PartialEq,
|
||||||
|
{
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||||
let value = i64::from(self.val());
|
let value = i64::from(self.val());
|
||||||
let other = i64::from(other.val());
|
let other = i64::from(other.val());
|
||||||
|
|
||||||
|
@ -207,15 +247,23 @@ impl <T>PartialOrd for ByteUnit<T> where i64: From<T>, T: Copy + PartialEq {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T>PartialEq for ByteUnit<T> where i64: From<T>, T: Copy + PartialEq {
|
impl<T> PartialEq for ByteUnit<T>
|
||||||
fn eq(&self, other: &Self) -> bool {
|
where
|
||||||
|
i64: From<T>,
|
||||||
|
T: Copy + PartialEq,
|
||||||
|
{
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
other.val().eq(&self.val())
|
other.val().eq(&self.val())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T>Add for ByteUnit<T> where i64: From<T>, T: Copy + Add<Output = T> {
|
impl<T> Add for ByteUnit<T>
|
||||||
|
where
|
||||||
|
i64: From<T>,
|
||||||
|
T: Copy + Add<Output = T>,
|
||||||
|
{
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn add(self, input: Self) -> Self::Output {
|
fn add(self, input: Self) -> Self::Output {
|
||||||
match self {
|
match self {
|
||||||
Self::IEC(value) => Self::IEC(value + input.val()),
|
Self::IEC(value) => Self::IEC(value + input.val()),
|
||||||
|
@ -224,9 +272,13 @@ impl <T>Add for ByteUnit<T> where i64: From<T>, T: Copy + Add<Output = T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T>Sub for ByteUnit<T> where i64: From<T>, T: Copy + Sub<Output = T> {
|
impl<T> Sub for ByteUnit<T>
|
||||||
|
where
|
||||||
|
i64: From<T>,
|
||||||
|
T: Copy + Sub<Output = T>,
|
||||||
|
{
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn sub(self, input: Self) -> Self::Output {
|
fn sub(self, input: Self) -> Self::Output {
|
||||||
match self {
|
match self {
|
||||||
Self::IEC(value) => Self::IEC(value - input.val()),
|
Self::IEC(value) => Self::IEC(value - input.val()),
|
||||||
|
@ -235,9 +287,13 @@ impl <T>Sub for ByteUnit<T> where i64: From<T>, T: Copy + Sub<Output = T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T>Mul for ByteUnit<T> where i64: From<T>, T: Copy + Mul<Output = T> {
|
impl<T> Mul for ByteUnit<T>
|
||||||
|
where
|
||||||
|
i64: From<T>,
|
||||||
|
T: Copy + Mul<Output = T>,
|
||||||
|
{
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn mul(self, input: Self) -> Self::Output {
|
fn mul(self, input: Self) -> Self::Output {
|
||||||
match self {
|
match self {
|
||||||
Self::IEC(value) => Self::IEC(value * input.val()),
|
Self::IEC(value) => Self::IEC(value * input.val()),
|
||||||
|
@ -246,9 +302,13 @@ impl <T>Mul for ByteUnit<T> where i64: From<T>, T: Copy + Mul<Output = T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T>Div for ByteUnit<T> where i64: From<T>, T: Copy + Div<Output = T> {
|
impl<T> Div for ByteUnit<T>
|
||||||
|
where
|
||||||
|
i64: From<T>,
|
||||||
|
T: Copy + Div<Output = T>,
|
||||||
|
{
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn div(self, input: Self) -> Self::Output {
|
fn div(self, input: Self) -> Self::Output {
|
||||||
match self {
|
match self {
|
||||||
Self::IEC(value) => Self::IEC(value / input.val()),
|
Self::IEC(value) => Self::IEC(value / input.val()),
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
/*
|
/*
|
||||||
* SimpleByteUnit
|
* SimpleByteUnit
|
||||||
*
|
*
|
||||||
* Copyright (C) 2023-2025 Xavier Moffett <sapphirus@azorium.net>
|
* Copyright (C) 2023-2025 Xavier Moffett <sapphirus@azorium.net>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
|
@ -10,12 +10,11 @@
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use simplebyteunit::simplebyteunit::*;
|
use simplebyteunit::simplebyteunit::{ToByteUnit, *};
|
||||||
use simplebyteunit::simplebyteunit::ToByteUnit;
|
|
||||||
|
|
||||||
const POSITIVE_5B: i64 = 5000;
|
const POSITIVE_5B: i64 = 5000;
|
||||||
const NEGATIVE_5B: i64 = -5000;
|
const NEGATIVE_5B: i64 = -5000;
|
||||||
const POSITIVE_5K: i64 = 5000000;
|
const POSITIVE_5K: i64 = 5000000;
|
||||||
const NEGATIVE_5K: i64 = -5000000;
|
const NEGATIVE_5K: i64 = -5000000;
|
||||||
const POSITIVE_5G: i64 = 5000000000;
|
const POSITIVE_5G: i64 = 5000000000;
|
||||||
const NEGATIVE_5G: i64 = -5000000000;
|
const NEGATIVE_5G: i64 = -5000000000;
|
||||||
|
@ -28,52 +27,51 @@ const NEGATIVE_5E: i64 = -5000000000000000000;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn format_all() {
|
fn format_all() {
|
||||||
assert_eq!(NEGATIVE_5E.to_byteunit(SI).to_string(), "-5.00 EB");
|
assert_eq!(NEGATIVE_5E.to_byteunit(SI).to_string(), "-5.00 EB");
|
||||||
assert_eq!(POSITIVE_5E.to_byteunit(SI).to_string(), "5.00 EB");
|
assert_eq!(POSITIVE_5E.to_byteunit(SI).to_string(), "5.00 EB");
|
||||||
assert_eq!(NEGATIVE_5P.to_byteunit(SI).to_string(), "-5.00 PB");
|
assert_eq!(NEGATIVE_5P.to_byteunit(SI).to_string(), "-5.00 PB");
|
||||||
assert_eq!(POSITIVE_5P.to_byteunit(SI).to_string(), "5.00 PB");
|
assert_eq!(POSITIVE_5P.to_byteunit(SI).to_string(), "5.00 PB");
|
||||||
assert_eq!(NEGATIVE_5T.to_byteunit(SI).to_string(), "-5.00 TB");
|
assert_eq!(NEGATIVE_5T.to_byteunit(SI).to_string(), "-5.00 TB");
|
||||||
assert_eq!(POSITIVE_5T.to_byteunit(SI).to_string(), "5.00 TB");
|
assert_eq!(POSITIVE_5T.to_byteunit(SI).to_string(), "5.00 TB");
|
||||||
assert_eq!(NEGATIVE_5G.to_byteunit(SI).to_string(), "-5.00 GB");
|
assert_eq!(NEGATIVE_5G.to_byteunit(SI).to_string(), "-5.00 GB");
|
||||||
assert_eq!(POSITIVE_5G.to_byteunit(SI).to_string(), "5.00 GB");
|
assert_eq!(POSITIVE_5G.to_byteunit(SI).to_string(), "5.00 GB");
|
||||||
assert_eq!(POSITIVE_5K.to_byteunit(SI).to_string(), "5.00 MB");
|
assert_eq!(POSITIVE_5K.to_byteunit(SI).to_string(), "5.00 MB");
|
||||||
assert_eq!(NEGATIVE_5K.to_byteunit(SI).to_string(), "-5.00 MB");
|
assert_eq!(NEGATIVE_5K.to_byteunit(SI).to_string(), "-5.00 MB");
|
||||||
assert_eq!(POSITIVE_5B.to_byteunit(SI).to_string(), "5.00 kB");
|
assert_eq!(POSITIVE_5B.to_byteunit(SI).to_string(), "5.00 kB");
|
||||||
assert_eq!(NEGATIVE_5B.to_byteunit(SI).to_string(), "-5.00 kB");
|
assert_eq!(NEGATIVE_5B.to_byteunit(SI).to_string(), "-5.00 kB");
|
||||||
assert_eq!(NEGATIVE_5E.to_byteunit(IEC).to_string(), "-4.34 EiB");
|
assert_eq!(NEGATIVE_5E.to_byteunit(IEC).to_string(), "-4.34 EiB");
|
||||||
assert_eq!(POSITIVE_5E.to_byteunit(IEC).to_string(), "4.34 EiB");
|
assert_eq!(POSITIVE_5E.to_byteunit(IEC).to_string(), "4.34 EiB");
|
||||||
assert_eq!(NEGATIVE_5P.to_byteunit(IEC).to_string(), "-4.44 PiB");
|
assert_eq!(NEGATIVE_5P.to_byteunit(IEC).to_string(), "-4.44 PiB");
|
||||||
assert_eq!(POSITIVE_5P.to_byteunit(IEC).to_string(), "4.44 PiB");
|
assert_eq!(POSITIVE_5P.to_byteunit(IEC).to_string(), "4.44 PiB");
|
||||||
assert_eq!(NEGATIVE_5T.to_byteunit(IEC).to_string(), "-4.55 TiB");
|
assert_eq!(NEGATIVE_5T.to_byteunit(IEC).to_string(), "-4.55 TiB");
|
||||||
assert_eq!(NEGATIVE_5T.to_byteunit(IEC).to_string(), "-4.55 TiB");
|
assert_eq!(NEGATIVE_5T.to_byteunit(IEC).to_string(), "-4.55 TiB");
|
||||||
assert_eq!(POSITIVE_5T.to_byteunit(IEC).to_string(), "4.55 TiB");
|
assert_eq!(POSITIVE_5T.to_byteunit(IEC).to_string(), "4.55 TiB");
|
||||||
assert_eq!(NEGATIVE_5G.to_byteunit(IEC).to_string(), "-4.66 GiB");
|
assert_eq!(NEGATIVE_5G.to_byteunit(IEC).to_string(), "-4.66 GiB");
|
||||||
assert_eq!(POSITIVE_5G.to_byteunit(IEC).to_string(), "4.66 GiB");
|
assert_eq!(POSITIVE_5G.to_byteunit(IEC).to_string(), "4.66 GiB");
|
||||||
assert_eq!(POSITIVE_5K.to_byteunit(IEC).to_string(), "4.77 MiB");
|
assert_eq!(POSITIVE_5K.to_byteunit(IEC).to_string(), "4.77 MiB");
|
||||||
assert_eq!(NEGATIVE_5K.to_byteunit(IEC).to_string(), "-4.77 MiB");
|
assert_eq!(NEGATIVE_5K.to_byteunit(IEC).to_string(), "-4.77 MiB");
|
||||||
assert_eq!(POSITIVE_5B.to_byteunit(IEC).to_string(), "4.88 KiB");
|
assert_eq!(POSITIVE_5B.to_byteunit(IEC).to_string(), "4.88 KiB");
|
||||||
assert_eq!(NEGATIVE_5B.to_byteunit(IEC).to_string(), "-4.88 KiB");
|
assert_eq!(NEGATIVE_5B.to_byteunit(IEC).to_string(), "-4.88 KiB");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn bytes() {
|
fn bytes() {
|
||||||
assert_eq!(NEGATIVE_5B.to_byteunit(IEC).pow(B), "-5000 B");
|
assert_eq!(NEGATIVE_5B.to_byteunit(IEC).pow(B), "-5000 B");
|
||||||
assert_eq!(NEGATIVE_5B.to_byteunit(SI).pow(B), "-5000 B");
|
assert_eq!(NEGATIVE_5B.to_byteunit(SI).pow(B), "-5000 B");
|
||||||
assert_eq!(POSITIVE_5B.to_byteunit(IEC).pow(B), "5000 B");
|
assert_eq!(POSITIVE_5B.to_byteunit(IEC).pow(B), "5000 B");
|
||||||
assert_eq!(POSITIVE_5B.to_byteunit(SI).pow(B), "5000 B");
|
assert_eq!(POSITIVE_5B.to_byteunit(SI).pow(B), "5000 B");
|
||||||
assert_eq!(NEGATIVE_5E.to_byteunit(IEC).pow(B), "-5000000000000000000 B");
|
assert_eq!(NEGATIVE_5E.to_byteunit(IEC).pow(B), "-5000000000000000000 B");
|
||||||
assert_eq!(NEGATIVE_5E.to_byteunit(SI).pow(B), "-5000000000000000000 B");
|
assert_eq!(NEGATIVE_5E.to_byteunit(SI).pow(B), "-5000000000000000000 B");
|
||||||
assert_eq!(POSITIVE_5E.to_byteunit(IEC).pow(B), "5000000000000000000 B");
|
assert_eq!(POSITIVE_5E.to_byteunit(IEC).pow(B), "5000000000000000000 B");
|
||||||
assert_eq!(POSITIVE_5E.to_byteunit(SI).pow(B), "5000000000000000000 B");
|
assert_eq!(POSITIVE_5E.to_byteunit(SI).pow(B), "5000000000000000000 B");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn k() {
|
fn k() {
|
||||||
assert_eq!(NEGATIVE_5K.to_byteunit(IEC).pow(K), "-4882.81 KiB");
|
assert_eq!(NEGATIVE_5K.to_byteunit(IEC).pow(K), "-4882.81 KiB");
|
||||||
assert_eq!(POSITIVE_5K.to_byteunit(IEC).pow(K), "4882.81 KiB");
|
assert_eq!(POSITIVE_5K.to_byteunit(IEC).pow(K), "4882.81 KiB");
|
||||||
assert_eq!(NEGATIVE_5K.to_byteunit(SI).pow(K), "-5000.00 kB");
|
assert_eq!(NEGATIVE_5K.to_byteunit(SI).pow(K), "-5000.00 kB");
|
||||||
assert_eq!(POSITIVE_5K.to_byteunit(SI).pow(K), "5000.00 kB");
|
assert_eq!(POSITIVE_5K.to_byteunit(SI).pow(K), "5000.00 kB");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -87,9 +85,9 @@ fn m() {
|
||||||
#[test]
|
#[test]
|
||||||
fn g() {
|
fn g() {
|
||||||
assert_eq!(NEGATIVE_5T.to_byteunit(IEC).pow(G), "-4656.61 GiB");
|
assert_eq!(NEGATIVE_5T.to_byteunit(IEC).pow(G), "-4656.61 GiB");
|
||||||
assert_eq!(POSITIVE_5T.to_byteunit(IEC).pow(G), "4656.61 GiB");
|
assert_eq!(POSITIVE_5T.to_byteunit(IEC).pow(G), "4656.61 GiB");
|
||||||
assert_eq!(NEGATIVE_5T.to_byteunit(SI).pow(G), "-5000.00 GB");
|
assert_eq!(NEGATIVE_5T.to_byteunit(SI).pow(G), "-5000.00 GB");
|
||||||
assert_eq!(POSITIVE_5T.to_byteunit(SI).pow(G), "5000.00 GB");
|
assert_eq!(POSITIVE_5T.to_byteunit(SI).pow(G), "5000.00 GB");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -102,24 +100,24 @@ fn t() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn p() {
|
fn p() {
|
||||||
assert_eq!(NEGATIVE_5E.to_byteunit(IEC).pow(P), "-4440.89 PiB");
|
assert_eq!(NEGATIVE_5E.to_byteunit(IEC).pow(P), "-4440.89 PiB");
|
||||||
assert_eq!(POSITIVE_5E.to_byteunit(IEC).pow(P), "4440.89 PiB");
|
assert_eq!(POSITIVE_5E.to_byteunit(IEC).pow(P), "4440.89 PiB");
|
||||||
assert_eq!(NEGATIVE_5E.to_byteunit(SI).pow(P), "-5000.00 PB");
|
assert_eq!(NEGATIVE_5E.to_byteunit(SI).pow(P), "-5000.00 PB");
|
||||||
assert_eq!(POSITIVE_5E.to_byteunit(SI).pow(P), "5000.00 PB");
|
assert_eq!(POSITIVE_5E.to_byteunit(SI).pow(P), "5000.00 PB");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn e() {
|
fn e() {
|
||||||
assert_eq!(NEGATIVE_5E.to_byteunit(IEC).pow(E), "-4.34 EiB");
|
assert_eq!(NEGATIVE_5E.to_byteunit(IEC).pow(E), "-4.34 EiB");
|
||||||
assert_eq!(POSITIVE_5E.to_byteunit(IEC).pow(E), "4.34 EiB");
|
assert_eq!(POSITIVE_5E.to_byteunit(IEC).pow(E), "4.34 EiB");
|
||||||
assert_eq!(NEGATIVE_5E.to_byteunit(SI).pow(E), "-5.00 EB");
|
assert_eq!(NEGATIVE_5E.to_byteunit(SI).pow(E), "-5.00 EB");
|
||||||
assert_eq!(POSITIVE_5E.to_byteunit(SI).pow(E), "5.00 EB");
|
assert_eq!(POSITIVE_5E.to_byteunit(SI).pow(E), "5.00 EB");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn eq() {
|
fn eq() {
|
||||||
assert_eq!(POSITIVE_5G.to_byteunit(SI) == POSITIVE_5G.to_byteunit(SI), true);
|
assert_eq!(POSITIVE_5G.to_byteunit(SI) == POSITIVE_5G.to_byteunit(SI), true);
|
||||||
assert_eq!(POSITIVE_5B.to_byteunit(SI) == POSITIVE_5G.to_byteunit(SI), false);
|
assert_eq!(POSITIVE_5B.to_byteunit(SI) == POSITIVE_5G.to_byteunit(SI), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -156,7 +154,6 @@ fn sub() {
|
||||||
let division = a - b;
|
let division = a - b;
|
||||||
|
|
||||||
assert_eq!(division.to_string(), "0 B");
|
assert_eq!(division.to_string(), "0 B");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue