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
|
17
src/input.rs
17
src/input.rs
|
@ -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("eb") => ("eb", E, 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() {
|
||||
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 power_of = input.2;
|
||||
let multiplier = input.1;
|
||||
|
|
|
@ -21,7 +21,7 @@ pub fn prefix<'a, T: Copy>(unit: &ByteUnit<T>, i: i8) -> &'a str {
|
|||
T => "TiB",
|
||||
P => "PiB",
|
||||
E => "EiB",
|
||||
_ => "B"
|
||||
_ => "B",
|
||||
},
|
||||
ByteUnit::SI(_) => match i {
|
||||
K => "kB",
|
||||
|
@ -30,12 +30,14 @@ pub fn prefix<'a, T: Copy>(unit: &ByteUnit<T>, i: i8) -> &'a str {
|
|||
T => "TB",
|
||||
P => "PB",
|
||||
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)
|
||||
where
|
||||
i64: From<T>, {
|
||||
let bytes: i64 = value.0.into();
|
||||
let diviser = value.1;
|
||||
let positive = bytes > -1;
|
||||
|
@ -47,5 +49,8 @@ pub fn arithmetic<T: Copy>(value: (T, f64), power_of: i8) -> (i8, f64) where i64
|
|||
power += 1;
|
||||
}
|
||||
|
||||
match positive { true => (power, bytes), false => (power, -bytes) }
|
||||
match positive {
|
||||
true => (power, bytes),
|
||||
false => (power, -bytes),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,9 +16,12 @@ Fast, stupid simple ByteUnit implementation
|
|||
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 std::{
|
||||
fmt::{Debug, Display, Formatter},
|
||||
ops::{Add, Div, Mul, Sub},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
/// IEC ByteUnit (x*1024)
|
||||
pub const IEC: ByteUnit<()> = ByteUnit::IEC(());
|
||||
|
@ -58,7 +61,10 @@ pub trait ToByteUnit<T: Copy> {
|
|||
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> {
|
||||
match unit {
|
||||
ByteUnit::IEC(()) => ByteUnit::IEC(self),
|
||||
|
@ -67,7 +73,10 @@ impl ToByteUnit<u32> for u32 where i64: From<u32> {
|
|||
}
|
||||
}
|
||||
|
||||
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> {
|
||||
match unit {
|
||||
ByteUnit::IEC(()) => ByteUnit::IEC(self),
|
||||
|
@ -76,7 +85,10 @@ impl ToByteUnit<i32> for i32 where i64: From<i32> {
|
|||
}
|
||||
}
|
||||
|
||||
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> {
|
||||
match unit {
|
||||
ByteUnit::IEC(()) => ByteUnit::IEC(self),
|
||||
|
@ -85,11 +97,15 @@ impl ToByteUnit<i64> for i64 where i64: From<i64> {
|
|||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
match self {
|
||||
Self::IEC(val) => (*val, 1024.0),
|
||||
Self::SI(val) => (*val, 1000.0)
|
||||
Self::SI(val) => (*val, 1000.0),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -105,7 +121,7 @@ impl <T>ByteUnit<T> where i64: From<T>, T: Copy {
|
|||
|
||||
/// Acquire and return base value of encapsulated primitive
|
||||
pub fn val(&self) -> T {
|
||||
match self {
|
||||
match self {
|
||||
Self::IEC(val) => *val,
|
||||
Self::SI(val) => *val,
|
||||
}
|
||||
|
@ -153,8 +169,12 @@ impl <T>ByteUnit<T> where i64: From<T>, T: Copy {
|
|||
}
|
||||
|
||||
/// Display implementation with a maximum power of 6 (Exa/Exbi)
|
||||
impl<T> Display for ByteUnit<T> where i64: From<T>, T: Copy {
|
||||
fn fmt(&self, f:&mut Formatter<'_>) -> std::fmt::Result {
|
||||
impl<T> Display 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;
|
||||
|
@ -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)
|
||||
impl<T> Debug for ByteUnit<T> where i64: From<T>, T: Copy {
|
||||
fn fmt(&self, f:&mut Formatter<'_>) -> std::fmt::Result {
|
||||
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;
|
||||
|
@ -180,25 +204,41 @@ 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 {
|
||||
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;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let input = input::arithmetic::<T>(input::parse(s)?);
|
||||
|
||||
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>
|
||||
where
|
||||
i64: From<T>,
|
||||
T: Copy + PartialEq,
|
||||
{
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
let value = i64::from(self.val());
|
||||
let other = i64::from(other.val());
|
||||
|
@ -207,13 +247,21 @@ 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>
|
||||
where
|
||||
i64: From<T>,
|
||||
T: Copy + PartialEq,
|
||||
{
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
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;
|
||||
|
||||
fn add(self, input: Self) -> Self::Output {
|
||||
|
@ -224,7 +272,11 @@ 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;
|
||||
|
||||
fn sub(self, input: Self) -> Self::Output {
|
||||
|
@ -235,7 +287,11 @@ 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;
|
||||
|
||||
fn mul(self, input: Self) -> Self::Output {
|
||||
|
@ -246,7 +302,11 @@ 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;
|
||||
|
||||
fn div(self, input: Self) -> Self::Output {
|
||||
|
|
|
@ -10,12 +10,11 @@
|
|||
* 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 NEGATIVE_5B: i64 = -5000;
|
||||
const POSITIVE_5K: i64 = 5000000;
|
||||
const NEGATIVE_5B: i64 = -5000;
|
||||
const POSITIVE_5K: i64 = 5000000;
|
||||
const NEGATIVE_5K: i64 = -5000000;
|
||||
const POSITIVE_5G: i64 = 5000000000;
|
||||
const NEGATIVE_5G: i64 = -5000000000;
|
||||
|
@ -53,7 +52,6 @@ fn format_all() {
|
|||
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!(NEGATIVE_5B.to_byteunit(IEC).to_string(), "-4.88 KiB");
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -118,8 +116,8 @@ fn e() {
|
|||
|
||||
#[test]
|
||||
fn eq() {
|
||||
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_5G.to_byteunit(SI) == POSITIVE_5G.to_byteunit(SI), true);
|
||||
assert_eq!(POSITIVE_5B.to_byteunit(SI) == POSITIVE_5G.to_byteunit(SI), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -156,7 +154,6 @@ fn sub() {
|
|||
let division = a - b;
|
||||
|
||||
assert_eq!(division.to_string(), "0 B");
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue