Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ThomasK33/kube-quantity-rs
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.5.1
Choose a base ref
...
head repository: ThomasK33/kube-quantity-rs
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Loading
Showing with 522 additions and 73 deletions.
  1. +2 −4 .github/workflows/audit.yml
  2. +0 −28 .github/workflows/automerge.yml
  3. +5 −5 .github/workflows/clippy.yml
  4. +1 −1 .github/workflows/post-release.yml
  5. +111 −1 CHANGELOG.md
  6. +8 −8 Cargo.toml
  7. +91 −5 README.md
  8. +2 −1 src/format.rs
  9. +164 −0 src/lib.rs
  10. +50 −0 src/parser.rs
  11. +78 −20 src/quantity.rs
  12. +6 −0 src/scale.rs
  13. +4 −0 src/utils.rs
6 changes: 2 additions & 4 deletions .github/workflows/audit.yml
Original file line number Diff line number Diff line change
@@ -8,15 +8,13 @@ on:
paths:
- "**/Cargo.toml"
- "**/Cargo.lock"
schedule:
- cron: "2 2 2 * *"

jobs:
security_audit:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- uses: actions-rs/audit-check@v1
uses: actions/checkout@v4
- uses: rustsec/audit-check@v2.0.0
with:
token: ${{ secrets.GITHUB_TOKEN }}
28 changes: 0 additions & 28 deletions .github/workflows/automerge.yml

This file was deleted.

10 changes: 5 additions & 5 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
@@ -8,9 +8,9 @@ jobs:
clippy_check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: rustup component add clippy
- uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features
- run: cargo clippy --all-features
env:
RUSTFLAGS: "-Dwarnings"
2 changes: 1 addition & 1 deletion .github/workflows/post-release.yml
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ jobs:
name: Update changelog
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
ref: main
- uses: rhysd/changelog-from-release/action@v3
112 changes: 111 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,108 @@
# Changelog

<a name="v0.8.0"></a>
## [v0.8.0](https://github.com/ThomasK33/kube-quantity-rs/releases/tag/v0.8.0) - 28 Sep 2024

## What's Changed
### Added
* Implement Multiplication and Division of Quantities by [@volfco](https://github.com/volfco) in https://github.com/ThomasK33/kube-quantity-rs/pull/15
* add support for nanocpus (fixes issue [#18](https://github.com/ThomasK33/kube-quantity-rs/issues/18)) by [@philgebhardt](https://github.com/philgebhardt) in https://github.com/ThomasK33/kube-quantity-rs/pull/19
### Changed
* Update k8s-openapi to latest version by [@xmakro](https://github.com/xmakro) in https://github.com/ThomasK33/kube-quantity-rs/pull/17

## New Contributors
* [@volfco](https://github.com/volfco) made their first contribution in https://github.com/ThomasK33/kube-quantity-rs/pull/15
* [@philgebhardt](https://github.com/philgebhardt) made their first contribution in https://github.com/ThomasK33/kube-quantity-rs/pull/19
* [@xmakro](https://github.com/xmakro) made their first contribution in https://github.com/ThomasK33/kube-quantity-rs/pull/17

**Full Changelog**: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.7.1...v0.8.0

[Changes][v0.8.0]


<a name="v0.7.1"></a>
## [v0.7.1](https://github.com/ThomasK33/kube-quantity-rs/releases/tag/v0.7.1) - 27 Feb 2024

- Updated k8s-openapi dependencies

**Full Changelog**: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.7.0...v0.7.1

[Changes][v0.7.1]


<a name="v0.7.0"></a>
## [v0.7.0](https://github.com/ThomasK33/kube-quantity-rs/releases/tag/v0.7.0) - 09 Sep 2023

<!-- Release notes generated using configuration in .github/release.yml at v0.7.0 -->

## What's Changed
### Changed
* bump openapi and k8s version by [@fluktuid](https://github.com/fluktuid) in https://github.com/ThomasK33/kube-quantity-rs/pull/9

## New Contributors
* [@fluktuid](https://github.com/fluktuid) made their first contribution in https://github.com/ThomasK33/kube-quantity-rs/pull/9

**Full Changelog**: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.6.1...v0.7.0

[Changes][v0.7.0]


<a name="v0.6.1"></a>
## [v0.6.1](https://github.com/ThomasK33/kube-quantity-rs/releases/tag/v0.6.1) - 14 May 2023

<!-- Release notes generated using configuration in .github/release.yml at v0.6.1 -->

- [Updated dependencies](https://github.com/ThomasK33/kube-quantity-rs/commit/1fa2dda85d20e8acb7bdc3d75a00dbee7dfad18b)

**Full Changelog**: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.6.0...v0.6.1

[Changes][v0.6.1]


<a name="v0.6.0"></a>
## [v0.6.0](https://github.com/ThomasK33/kube-quantity-rs/releases/tag/v0.6.0) - 27 Mar 2023

<!-- Release notes generated using configuration in .github/release.yml at v0.6.0 -->

## What's Changed
### Added
* Implemented default trait for parsed quantity by [@ThomasK33](https://github.com/ThomasK33) in https://github.com/ThomasK33/kube-quantity-rs/pull/6


**Full Changelog**: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.5.1...v0.6.0

[Changes][v0.6.0]


<a name="v0.5.1"></a>
## [v0.5.1](https://github.com/ThomasK33/kube-quantity-rs/releases/tag/v0.5.1) - 27 Mar 2023

<!-- Release notes generated using configuration in .github/release.yml at v0.5.1 -->

## What's Changed
### Fixed
* Fixed docs.rs doc generation missing a feature flag

**Full Changelog**: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.5.0...v0.5.1

[Changes][v0.5.1]


<a name="v0.5.0"></a>
## [v0.5.0](https://github.com/ThomasK33/kube-quantity-rs/releases/tag/v0.5.0) - 27 Mar 2023

<!-- Release notes generated using configuration in .github/release.yml at v0.5.0 -->

## What's Changed
### Fixed
* Moved k8s-openapi features to dev dependencies by [@ThomasK33](https://github.com/ThomasK33) in https://github.com/ThomasK33/kube-quantity-rs/pull/5


**Full Changelog**: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.4.0...v0.5.0

[Changes][v0.5.0]


<a name="v0.4.0"></a>
## [v0.4.0](https://github.com/ThomasK33/kube-quantity-rs/releases/tag/v0.4.0) - 27 Mar 2023

@@ -73,10 +176,17 @@
[Changes][v0.1.0]


[v0.8.0]: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.7.1...v0.8.0
[v0.7.1]: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.7.0...v0.7.1
[v0.7.0]: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.6.1...v0.7.0
[v0.6.1]: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.6.0...v0.6.1
[v0.6.0]: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.5.1...v0.6.0
[v0.5.1]: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.5.0...v0.5.1
[v0.5.0]: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.4.0...v0.5.0
[v0.4.0]: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.3.0...v0.4.0
[v0.3.0]: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.2.0...v0.3.0
[v0.2.0]: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.1.1...v0.2.0
[v0.1.1]: https://github.com/ThomasK33/kube-quantity-rs/compare/v0.1.0...v0.1.1
[v0.1.0]: https://github.com/ThomasK33/kube-quantity-rs/tree/v0.1.0

<!-- Generated by https://github.com/rhysd/changelog-from-release v3.7.0 -->
<!-- Generated by https://github.com/rhysd/changelog-from-release v3.7.2 -->
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kube_quantity"
version = "0.5.1"
version = "0.8.0"

authors = ["Thomas Kosiewski <thoma471@googlemail.com>"]
description = "kube_quantity is a library adding arithmetic operations to the Quantity type from the k8s-openapi crate."
@@ -16,18 +16,18 @@ categories = ["algorithms", "data-structures", "encoding"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
k8s-openapi = { version = "0.17.0", default-features = false }
k8s-openapi = { version = "0.23.0", default-features = false }
nom = "7.1.3"
rust_decimal = "1.29.0"
thiserror = "1.0.40"
rust_decimal = "1.36.0"
thiserror = "1.0.64"

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

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

[package.metadata.docs.rs]
features = ["k8s-openapi/v1_26"]
features = ["k8s-openapi/latest"]
96 changes: 91 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
@@ -16,12 +18,13 @@ Or add the following line to your Cargo.toml:

```toml
[dependencies]
kube_quantity = "0.5.1"
kube_quantity = "0.8.0"
```

## 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

@@ -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();

@@ -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
@@ -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.
3 changes: 2 additions & 1 deletion src/format.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// - Format -

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub(crate) enum Format {
/// e.g., 12Mi = (12 * 2^20) = (12 * 1024^2)
#[default]
BinarySI,
// /// e.g., 12e6 = (12 * 10^6)
// DecimalExponent,
Loading