kube_quantity - Kubernetes Quantity Parser

Crates.io

kube_quantity is a library adding arithmetic operations to the Quantity type from the k8s-openapi crate.

Installation

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

bash cargo add kube_quantity

Or add the following line to your Cargo.toml:

toml [dependencies] kube_quantity = "0.5.0"

Upgrading

Please check the CHANGELOG when upgrading.

Usage

Parsing of quantities

```rust use k8sopenapi::apimachinery::pkg::api::resource::Quantity; use kubequantity::{ParseQuantityError, ParsedQuantity};

// Parse from &str let quantity = "1Ki"; let quantity: Result = quantity.tryinto(); asserteq!(quantity.unwrap().to_string(), "1Ki");

// Parse from a String let quantity = String::from("2Mi"); let quantity: Result = quantity.tryinto(); asserteq!(quantity.unwrap().to_string(), "2Mi");

// Parse from a k8s_openapi Quantity let quantity = Quantity("2.5Gi".tostring()); let quantity: Result = quantity.tryinto(); asserteq!(quantity.unwrap().tostring(), "2.5Gi"); ```

Addition of quantities

```rust use k8sopenapi::apimachinery::pkg::api::resource::Quantity; use kubequantity::{ParseQuantityError, ParsedQuantity};

// Try parsing k8s quantities let q1: Result = Quantity("1Ki".tostring()).tryinto(); let q2: Result = Quantity("2Ki".tostring()).tryinto();

// 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, "3Ki"); ```

```rust use k8sopenapi::apimachinery::pkg::api::resource::Quantity; use kubequantity::{ParseQuantityError, ParsedQuantity};

let q1: Result = Quantity("5M".tostring()).tryinto(); let q2: Result = Quantity("7M".tostring()).tryinto();

let mut q1 = q1.unwrap(); q1 += q2.unwrap();

let q1: Quantity = q1.into();

assert_eq!(q1.0, "12M");

```

Subtraction of quantities

```rust use k8sopenapi::apimachinery::pkg::api::resource::Quantity; use kubequantity::{ParseQuantityError, ParsedQuantity};

// Try parsing k8s quantities let q1: Result = Quantity("1M".tostring()).tryinto(); let q2: Result = Quantity("500k".tostring()).tryinto();

// Subtract 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, "500k"); ```

```rust use k8sopenapi::apimachinery::pkg::api::resource::Quantity; use kubequantity::{ParseQuantityError, ParsedQuantity};

// Try parsing k8s quantities let q1: Result = Quantity("10G".tostring()).tryinto(); let q2: Result = Quantity("500M".tostring()).tryinto();

let mut q1 = q1.unwrap(); q1 -= q2.unwrap();

let q1: Quantity = q1.into();

assert_eq!(q1.0, "9500M"); ```

Comparison of quantities

```rust use k8sopenapi::apimachinery::pkg::api::resource::Quantity; use kubequantity::{ParseQuantityError, ParsedQuantity};

// Parse directly from &str let q1 = "5Ki"; let q1: Result = q1.tryinto(); let q1 = q1.unwrap(); asserteq!(q1.to_string(), "5Ki");

// Parse from a k8s_openapi Quantity let q2 = Quantity("2.5Gi".tostring()); let q2: Result = q2.tryinto(); let q2 = q2.unwrap(); asserteq!(q2.tostring(), "2.5Gi");

// Compare byte amount equality assert!(q1 < q2); ```

```rust use k8sopenapi::apimachinery::pkg::api::resource::Quantity; use kubequantity::{ParseQuantityError, ParsedQuantity};

// Parse directly from &str let q1 = "1Ki"; let q1: Result = q1.tryinto(); let q1 = q1.unwrap(); asserteq!(q1.to_string(), "1Ki");

// Parse from a k8s_openapi Quantity let q2 = Quantity("1024".tostring()); let q2: Result = q2.tryinto(); let q2 = q2.unwrap(); asserteq!(q2.tostring(), "1024");

// Compare byte amount equality assert_eq!(q1, q2); ```

License

Apache 2.0 licensed. See LICENSE for details.