Skip to content

Commit

Permalink
add support for nano and micro
Browse files Browse the repository at this point in the history
  • Loading branch information
philgebhardt committed Aug 3, 2024
1 parent acb8cb0 commit 079550d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ fn parse_suffix(input: &str) -> IResult<&str, (Format, Scale)> {
tag("Ti"),
tag("Pi"),
tag("Ei"),
tag("n"),
tag("u"),
tag("m"),
tag("k"),
tag("M"),
Expand All @@ -114,6 +116,8 @@ fn parse_suffix(input: &str) -> IResult<&str, (Format, Scale)> {
"Pi" => (Format::BinarySI, Scale::Peta),
"Ei" => (Format::BinarySI, Scale::Exa),
//
"n" => (Format::DecimalSI, Scale::Nano),
"u" => (Format::DecimalSI, Scale::Micro),
"m" => (Format::DecimalSI, Scale::Milli),
"" => (Format::DecimalSI, Scale::One),
"k" => (Format::DecimalSI, Scale::Kilo),
Expand Down Expand Up @@ -202,6 +206,32 @@ mod tests {
assert_eq!(quantity.to_string(), "0".to_owned());
}

#[test]
fn test_nano_quantity() {
let quantity = parse_quantity_string("100000000n");
assert!(quantity.is_ok());

let quantity = quantity.unwrap().1;
assert_eq!(quantity.value, Decimal::new(100000000, 0));
assert_eq!(quantity.scale, Scale::Nano);
assert_eq!(quantity.format, Format::DecimalSI);

assert_eq!(quantity.to_string(), "100000000n");
}

#[test]
fn test_micro_quantity() {
let quantity = parse_quantity_string("100000u");
assert!(quantity.is_ok());

let quantity = quantity.unwrap().1;
assert_eq!(quantity.value, Decimal::new(100000, 0));
assert_eq!(quantity.scale, Scale::Micro);
assert_eq!(quantity.format, Format::DecimalSI);

assert_eq!(quantity.to_string(), "100000u");
}

#[test]
fn test_milli_quantity() {
let quantity = parse_quantity_string("100m");
Expand Down Expand Up @@ -245,6 +275,26 @@ mod tests {
assert_eq!(q3.to_string(), "2049Ki");
}

#[test]
fn test_quantity_addition_binary_si_mixed_scales_2() {
let q1 = parse_quantity_string("50m").unwrap().1;
let q2 = parse_quantity_string("50000000n").unwrap().1;

let q3 = q1 + q2;

assert_eq!(q3.to_string(), "100000000n");
}

#[test]
fn test_quantity_addition_binary_si_mixed_scales_3() {
let q1 = parse_quantity_string("1.5u").unwrap().1;
let q2 = parse_quantity_string("500n").unwrap().1;

let q3 = q1 + q2;

assert_eq!(q3.to_string(), "2000n");
}

#[test]
fn test_quantity_addition_binary_si_decimal_exponent() {
let q1 = parse_quantity_string("12Mi").unwrap().1;
Expand Down
6 changes: 6 additions & 0 deletions src/scale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/// scales are omitted for mathematical simplicity.
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Default)]
pub(crate) enum Scale {
Nano,
Micro,
Milli,
#[default]
One,
Expand All @@ -25,6 +27,8 @@ impl From<&Scale> for i32 {
fn from(value: &Scale) -> Self {
// https://en.wikipedia.org/wiki/Kilobyte
match value {
Scale::Nano => -3,
Scale::Micro => -2,
Scale::Milli => -1,
Scale::One => 0,
Scale::Kilo => 1,
Expand All @@ -42,6 +46,8 @@ impl TryFrom<i32> for Scale {

fn try_from(value: i32) -> Result<Self, Self::Error> {
match value {
-3 => Ok(Scale::Nano),
-2 => Ok(Scale::Micro),
-1 => Ok(Scale::Milli),
0 => Ok(Scale::One),
1 => Ok(Scale::Kilo),
Expand Down
4 changes: 4 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::{format::Format, scale::Scale};
pub(crate) fn scale_format_to_string(scale: &Scale, format: &Format) -> String {
match format {
Format::BinarySI => match scale {
Scale::Nano => "n".to_owned(),
Scale::Micro => "u".to_owned(),
Scale::Milli => "".to_owned(),
Scale::One => "".to_owned(),
Scale::Kilo => "Ki".to_owned(),
Expand All @@ -14,6 +16,8 @@ pub(crate) fn scale_format_to_string(scale: &Scale, format: &Format) -> String {
Scale::Exa => "Ei".to_owned(),
},
Format::DecimalSI => match scale {
Scale::Nano => "n".to_owned(),
Scale::Micro => "u".to_owned(),
Scale::Milli => "m".to_owned(),
Scale::One => "".to_owned(),
Scale::Kilo => "k".to_owned(),
Expand Down

0 comments on commit 079550d

Please sign in to comment.