#arithmetic-operations #kubernetes #cncf

kube_quantity

kube_quantity 是一个库,它向来自 k8s-openapi crate 的 Quantity 类型添加了算术运算功能

11个版本 (6个破坏性更新)

0.7.1 2024年2月27日
0.7.0 2023年9月9日
0.6.1 2023年5月14日
0.6.0 2023年3月27日

#333 in 算法

Download history 69/week @ 2024-04-08 227/week @ 2024-04-15 227/week @ 2024-04-22 280/week @ 2024-04-29 148/week @ 2024-05-06 88/week @ 2024-05-13 146/week @ 2024-05-20 70/week @ 2024-05-27 661/week @ 2024-06-03 687/week @ 2024-06-10 165/week @ 2024-06-17 83/week @ 2024-06-24 110/week @ 2024-07-01 67/week @ 2024-07-08 71/week @ 2024-07-15 94/week @ 2024-07-22

342 个月下载量
git-remote-k8s 中使用

Apache-2.0

47KB
930

kube_quantity - Kubernetes数量解析器

Crates.io

kube_quantity 是一个库,它向 Quantity 类型添加了算术运算功能,该类型来自 k8s-openapi crate.

安装

在您的项目目录中运行以下Cargo命令以添加最新稳定版本

cargo add kube_quantity

或者将以下行添加到您的Cargo.toml中

[dependencies]
kube_quantity = "0.7.1"

升级

升级时请查阅 变更日志

用法

数量的解析

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

// Parse from &str
let quantity = "1Ki";
let quantity: Result<ParsedQuantity, ParseQuantityError> = quantity.try_into();
assert_eq!(quantity.unwrap().to_string(), "1Ki");

// Parse from a String
let quantity = String::from("2Mi");
let quantity: Result<ParsedQuantity, ParseQuantityError> = quantity.try_into();
assert_eq!(quantity.unwrap().to_string(), "2Mi");

// Parse from a `k8s_openapi` Quantity
let quantity = Quantity("2.5Gi".to_string());
let quantity: Result<ParsedQuantity, ParseQuantityError> = quantity.try_into();
assert_eq!(quantity.unwrap().to_string(), "2.5Gi");

数量的加法

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

// Try parsing k8s quantities
let q1: Result<ParsedQuantity, ParseQuantityError> = Quantity("1Ki".to_string()).try_into();
let q2: Result<ParsedQuantity, ParseQuantityError> = Quantity("2Ki".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, "3Ki");
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();

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

let q1: Quantity = q1.into();

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

数量的减法

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

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

// 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");
use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use kube_quantity::{ParseQuantityError, ParsedQuantity};

// Try parsing k8s quantities
let q1: Result<ParsedQuantity, ParseQuantityError> = Quantity("10G".to_string()).try_into();
let q2: Result<ParsedQuantity, ParseQuantityError> = Quantity("500M".to_string()).try_into();

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

let q1: Quantity = q1.into();

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

数量的比较

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

// Parse directly from &str
let q1 = "5Ki";
let q1: Result<ParsedQuantity, ParseQuantityError> = q1.try_into();
let q1 = q1.unwrap();
assert_eq!(q1.to_string(), "5Ki");

// Parse from a `k8s_openapi` Quantity
let q2 = Quantity("2.5Gi".to_string());
let q2: Result<ParsedQuantity, ParseQuantityError> = q2.try_into();
let q2 = q2.unwrap();
assert_eq!(q2.to_string(), "2.5Gi");

// Compare byte amount equality
assert!(q1 < q2);
use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use kube_quantity::{ParseQuantityError, ParsedQuantity};

// Parse directly from &str
let q1 = "1Ki";
let q1: Result<ParsedQuantity, ParseQuantityError> = q1.try_into();
let q1 = q1.unwrap();
assert_eq!(q1.to_string(), "1Ki");

// Parse from a `k8s_openapi` Quantity
let q2 = Quantity("1024".to_string());
let q2: Result<ParsedQuantity, ParseQuantityError> = q2.try_into();
let q2 = q2.unwrap();
assert_eq!(q2.to_string(), "1024");

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

许可

Apache 2.0 许可。有关详细信息,请参阅 许可

依赖

~46MB
~740K SLoC