1个不稳定版本
0.6.1 | 2023年8月23日 |
---|
#1328 在 算法
47KB
930 行
kube_quantity - Kubernetes数量解析器
kube_quantity
是一个库,它向来自 Quantity
类型添加了算术操作。
安装
在您的项目目录中运行以下 Cargo 命令以添加最新稳定版本
cargo add kube_quantity
或将以下行添加到您的 Cargo.toml 文件中
[dependencies]
kube_quantity = "0.6.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 许可。有关详细信息,请参阅 LICENSE。
依赖项
~46MB
~746K SLoC