Skip to content

Commit

Permalink
Removed parsed quantity multiplication
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Kosiewski <[email protected]>
  • Loading branch information
ThomasK33 committed Sep 28, 2024
1 parent fd266ad commit 45d79d8
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 97 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ categories = ["algorithms", "data-structures", "encoding"]
[dependencies]
k8s-openapi = { version = "0.23.0", default-features = false }
nom = "7.1.3"
rust_decimal = "1.32.0"
rust_decimal = "1.36.0"
thiserror = "1.0.64"

[dev-dependencies]
k8s-openapi = { version = "0.23.0", default-features = false, features = [
"v1_29",
"latest",
] }

[features]
__check = ["k8s-openapi/v1_29"]
__check = ["k8s-openapi/latest"]

[package.metadata.docs.rs]
features = ["k8s-openapi/v1_29"]
features = ["k8s-openapi/latest"]
94 changes: 90 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

[![Crates.io](https://img.shields.io/crates/v/kube_quantity)](https://crates.io/crates/kube_quantity)

`kube_quantity` is a library adding arithmetic operations to the [`Quantity`](https://arnavion.github.io/k8s-openapi/v0.17.x/k8s_openapi/apimachinery/pkg/api/resource/struct.Quantity.html#) type from the [`k8s-openapi`](https://crates.io/crates/k8s-openapi) crate.
`kube_quantity` is a library adding arithmetic operations to the [`Quantity`](https://arnavion.github.io/k8s-openapi/v0.17.x/k8s_openapi/apimachinery/pkg/api/resource/struct.Quantity.html#)
type from the [`k8s-openapi`](https://crates.io/crates/k8s-openapi) crate.

## Installation

Run the following Cargo command in your project directory to add the latest stable version:
Run the following Cargo command in your project directory to add the latest
stable version:

```bash
cargo add kube_quantity
Expand All @@ -21,7 +23,8 @@ kube_quantity = "0.7.1"

## Upgrading

Please check the [CHANGELOG](https://github.com/ThomasK33/kube-quantity-rs/blob/main/CHANGELOG.md) when upgrading.
Please check the [CHANGELOG](https://github.com/ThomasK33/kube-quantity-rs/blob/main/CHANGELOG.md)
when upgrading.

## Usage

Expand Down Expand Up @@ -69,6 +72,22 @@ assert_eq!(q3.0, "3Ki");
use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use kube_quantity::{ParseQuantityError, ParsedQuantity};

// Try parsing k8s quantities
let q1: Result<ParsedQuantity, ParseQuantityError> = Quantity("4.2Ki".to_string()).try_into();
let q2: Result<ParsedQuantity, ParseQuantityError> = Quantity("2.1Ki".to_string()).try_into();

// Add parsed quantities
let q3: ParsedQuantity = q1.unwrap() + q2.unwrap();
// Convert parsed quantity back into a k8s quantity
let q3: Quantity = q3.into();

assert_eq!(q3.0, "6.3Ki");
```

```rust
use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use kube_quantity::{ParseQuantityError, ParsedQuantity};

let q1: Result<ParsedQuantity, ParseQuantityError> = Quantity("5M".to_string()).try_into();
let q2: Result<ParsedQuantity, ParseQuantityError> = Quantity("7M".to_string()).try_into();

Expand All @@ -81,6 +100,72 @@ assert_eq!(q1.0, "12M");

```

### Multiplication of quantities

```rust
use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use kube_quantity::{ParseQuantityError, ParsedQuantity};

// Try parsing k8s quantities
let q1: Result<ParsedQuantity, ParseQuantityError> = Quantity("3k".to_string()).try_into();

// Multiply parsed quantities
let q1: ParsedQuantity = q1.unwrap() * 2;
// Convert parsed quantity back into a k8s quantity
let q2: Quantity = q1.into();

assert_eq!(q2.0, "6k");
```

```rust
use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use kube_quantity::{ParseQuantityError, ParsedQuantity};

// Try parsing k8s quantities
let q1: Result<ParsedQuantity, ParseQuantityError> = Quantity("3k".to_string()).try_into();
let mut q1: ParsedQuantity = q1.unwrap();

// Multiply parsed quantities
q1 *= 2;
// Convert parsed quantity back into a k8s quantity
let q2: Quantity = q1.into();

assert_eq!(q2.0, "6k");
```

### Division of quantities

```rust
use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use kube_quantity::{ParseQuantityError, ParsedQuantity};

// Try parsing k8s quantities
let q1: Result<ParsedQuantity, ParseQuantityError> = Quantity("4k".to_string()).try_into();

// Multiply parsed quantities
let q1: ParsedQuantity = q1.unwrap() / 2;
// Convert parsed quantity back into a k8s quantity
let q2: Quantity = q1.into();

assert_eq!(q2.0, "2k");
```

```rust
use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use kube_quantity::{ParseQuantityError, ParsedQuantity};

// Try parsing k8s quantities
let q1: Result<ParsedQuantity, ParseQuantityError> = Quantity("3k".to_string()).try_into();
let mut q1: ParsedQuantity = q1.unwrap();

// Multiply parsed quantities
q1 /= 3;
// Convert parsed quantity back into a k8s quantity
let q2: Quantity = q1.into();

assert_eq!(q2.0, "1k");
```

### Subtraction of quantities

```rust
Expand Down Expand Up @@ -159,4 +244,5 @@ assert_eq!(q1, q2);

## License

Apache 2.0 licensed. See [LICENSE](https://github.com/ThomasK33/kube-quantity-rs/blob/main/LICENSE) for details.
Apache 2.0 licensed. See [LICENSE](https://github.com/ThomasK33/kube-quantity-rs/blob/main/LICENSE)
for details.
99 changes: 81 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ impl From<ParsedQuantity> for Quantity {
#[cfg(test)]
mod tests {
use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use rust_decimal::Decimal;
use rust_decimal::prelude::FromPrimitive;
use rust_decimal::Decimal;

use crate::{ParseQuantityError, ParsedQuantity};
use crate::format::Format;
use crate::scale::Scale;
use crate::{ParseQuantityError, ParsedQuantity};

#[test]
fn test_quantity_addition() {
Expand Down Expand Up @@ -163,7 +163,6 @@ mod tests {
assert_eq!(q.unwrap_err().to_string(), "quantity parsing failed");
}


#[test]
fn test_div() {
let exp_result = ParsedQuantity {
Expand All @@ -177,36 +176,68 @@ mod tests {
scale: Scale::Kilo,
format: Format::BinarySI,
};
let q2 = ParsedQuantity {
let q2 = Decimal::from_f32(2.0).unwrap();

let result = q1 / q2;

assert_eq!(result, exp_result);
}

#[test]
fn test_div_decimal_f32() {
let exp_result = ParsedQuantity {
value: Decimal::from_f32(2.0).unwrap(),
scale: Scale::One,
scale: Scale::Kilo,
format: Format::BinarySI,
};

let q1 = ParsedQuantity {
value: Decimal::from_f32(5.0).unwrap(),
scale: Scale::Kilo,
format: Format::BinarySI,
};
let q2 = Decimal::from_f32(2.5).unwrap();

let result = q1 / q2;

assert_eq!(result, exp_result);
}

#[test]
fn test_div_assign() {
fn test_div_decimal_u8() {
let exp_result = ParsedQuantity {
value: Decimal::from_f32(2.0).unwrap(),
scale: Scale::Kilo,
format: Format::BinarySI,
};

let mut q1 = ParsedQuantity {
value: Decimal::from_f32(4.0).unwrap(),
let q1 = ParsedQuantity {
value: Decimal::from_f32(6.0).unwrap(),
scale: Scale::Kilo,
format: Format::BinarySI,
};
let q2 = ParsedQuantity {
let q2 = 3;

let result = q1 / q2;

assert_eq!(result, exp_result);
}

#[test]
fn test_div_assign() {
let exp_result = ParsedQuantity {
value: Decimal::from_f32(2.0).unwrap(),
scale: Scale::One,
scale: Scale::Kilo,
format: Format::BinarySI,
};

let mut q1 = ParsedQuantity {
value: Decimal::from_f32(4.0).unwrap(),
scale: Scale::Kilo,
format: Format::BinarySI,
};
let q2 = Decimal::from_f32(2.0).unwrap();

q1 /= q2;

assert_eq!(q1, exp_result);
Expand All @@ -225,12 +256,48 @@ mod tests {
scale: Scale::Kilo,
format: Format::BinarySI,
};
let q2 = ParsedQuantity {
value: Decimal::from_f32(2.0).unwrap(),
scale: Scale::One,
let q2 = Decimal::from_f32(2.0).unwrap();

let result = q1 * q2;

assert_eq!(result, exp_result);
}

#[test]
fn test_mul_f32() {
let exp_result = ParsedQuantity {
value: Decimal::from_f32(10.0).unwrap(),
scale: Scale::Kilo,
format: Format::BinarySI,
};

let q1 = ParsedQuantity {
value: Decimal::from_f32(5.0).unwrap(),
scale: Scale::Kilo,
format: Format::BinarySI,
};
let q2 = Decimal::from_f32(2.0).unwrap();

let result = q1 * q2;

assert_eq!(result, exp_result);
}

#[test]
fn test_mul_u8() {
let exp_result = ParsedQuantity {
value: Decimal::from_f32(9.0).unwrap(),
scale: Scale::Kilo,
format: Format::BinarySI,
};

let q1 = ParsedQuantity {
value: Decimal::from_f32(4.5).unwrap(),
scale: Scale::Kilo,
format: Format::BinarySI,
};
let q2 = 2;

let result = q1 * q2;

assert_eq!(result, exp_result);
Expand All @@ -249,11 +316,7 @@ mod tests {
scale: Scale::Kilo,
format: Format::BinarySI,
};
let q2 = ParsedQuantity {
value: Decimal::from_f32(2.0).unwrap(),
scale: Scale::One,
format: Format::BinarySI,
};
let q2 = Decimal::from_f32(2.0).unwrap();

q1 *= q2;

Expand Down
Loading

0 comments on commit 45d79d8

Please sign in to comment.