Skip to content

Commit 130cc5f

Browse files
authored
Merge pull request #48 from innoave/feat/number-assertions-for-bigdecimal
feat: number specific assertions for `bigdecimal::BigDecimal` and `bigdecimal::BigDecimalRef`
2 parents 48d7981 + 43487c0 commit 130cc5f

9 files changed

Lines changed: 336 additions & 6 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ jobs:
9494
uses: actions-rs/cargo@v1
9595
with:
9696
command: test
97-
args: --no-default-features --features "colored, float-cmp, num-bigint, rust-decimal"
97+
args: --no-default-features --features "colored, float-cmp, num-bigint, rust-decimal, bigdecimal"
9898

9999
msrv:
100100
name: Build with MSRV

Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,25 @@ rustdoc-args = ["--cfg", "docsrs"]
2020

2121
[features]
2222
default = ["std", "colored", "float-cmp", "panic", "regex"]
23+
bigdecimal = ["dep:bigdecimal"]
2324
colored = ["dep:sdiff"]
2425
float-cmp = ["dep:float-cmp"]
2526
num-bigint = ["dep:num-bigint", "dep:lazy_static"]
2627
rust-decimal = ["dep:rust_decimal"]
2728
panic = ["std"]
2829
regex = ["dep:regex"]
29-
std = []
30+
std = ["bigdecimal/std", "rust_decimal/std"]
3031

3132
[dependencies]
3233
hashbrown = "0.15"
3334

3435
# optional
36+
bigdecimal = { version = "0.4", optional = true, default-features = false }
3537
float-cmp = { version = "0.10", optional = true }
3638
lazy_static = { version = "1", optional = true }
37-
num-bigint = { version = "0.4", default-features = false, optional = true }
38-
rust_decimal = { version = "1", default-features = false, optional = true }
39+
num-bigint = { version = "0.4", optional = true, default-features = false }
3940
regex = { version = "1", optional = true }
41+
rust_decimal = { version = "1", optional = true, default-features = false }
4042
sdiff = { version = "0.1", optional = true }
4143

4244
[dev-dependencies]

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ for numbers of types
168168
* integer primitives: `i8`, `i16`, `i32`, `i64`, `i128` and `isize`
169169
* floating point numbers: `f32` and `f64`
170170
* `num_bigint::BigInt` (requires crate feature `num-bigint`)
171+
* `bigdecimal:BigDecimal` and `bigdecimal:BigDecimalRef` (requires crate feature `bigdecimal`)
172+
* `rust_decimal::Decimal` (requires crate feature `rust-decimal`)
171173

172174
| assertion | description |
173175
|-----------------|------------------------------------------------------|
@@ -182,6 +184,7 @@ for numbers of types
182184
and `usize`
183185
* floating point numbers: `f32` and `f64`
184186
* `num_bigint::BigInt` and `num_bigint::BigUint` (requires crate feature `num-bigint`)
187+
* `bigdecimal:BigDecimal` and `bigdecimal:BigDecimalRef` (requires crate feature `bigdecimal`)
185188
* `rust_decimal::Decimal` (requires crate feature `rust-decimal`)
186189

187190
| assertion | description |

examples/fixture/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Rust issue [#95513](https://github.com/rust-lang/rust/issues/95513) is fixed
33
mod dummy_extern_uses {
44
use anyhow as _;
5+
#[cfg(feature = "bigdecimal")]
6+
use bigdecimal as _;
57
#[cfg(feature = "float-cmp")]
68
use float_cmp as _;
79
use hashbrown as _;

justfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ lint-all-features:
3737

3838
# linting code using Clippy for no-std environment
3939
lint-no-std:
40-
cargo clippy --all-targets --no-default-features --features "colored, float-cmp, num-bigint, rust-decimal"
40+
cargo clippy --all-targets --no-default-features --features "colored, float-cmp, num-bigint, rust-decimal, bigdecimal"
4141

4242
# linting code using Clippy with no features enabled
4343
lint-no-features:
@@ -55,7 +55,7 @@ test-all-features:
5555

5656
# run tests for no-std environment
5757
test-no-std:
58-
cargo test --no-default-features --features "colored, float-cmp, num-bigint, rust-decimal"
58+
cargo test --no-default-features --features "colored, float-cmp, num-bigint, rust-decimal, bigdecimal"
5959

6060
# run tests with no features enabled
6161
test-no-features:

src/bigdecimal/mod.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use crate::properties::{AdditiveIdentityProperty, MultiplicativeIdentityProperty, SignumProperty};
2+
use bigdecimal::num_bigint::Sign;
3+
use bigdecimal::{BigDecimal, BigDecimalRef, One, Zero};
4+
use lazy_static::lazy_static;
5+
6+
lazy_static! {
7+
static ref BIGDECIMAL_ZERO: BigDecimal = bigdecimal_zero();
8+
static ref BIGDECIMAL_ONE: BigDecimal = bigdecimal_one();
9+
}
10+
11+
#[inline]
12+
fn bigdecimal_zero() -> BigDecimal {
13+
BigDecimal::zero()
14+
}
15+
16+
#[inline]
17+
fn bigdecimal_one() -> BigDecimal {
18+
BigDecimal::one()
19+
}
20+
21+
impl SignumProperty for BigDecimal {
22+
fn is_negative_property(&self) -> bool {
23+
self.sign() == Sign::Minus
24+
}
25+
26+
fn is_positive_property(&self) -> bool {
27+
self.sign() == Sign::Plus
28+
}
29+
}
30+
31+
impl AdditiveIdentityProperty for BigDecimal {
32+
fn additive_identity() -> Self {
33+
bigdecimal_zero()
34+
}
35+
}
36+
37+
impl AdditiveIdentityProperty for &BigDecimal {
38+
fn additive_identity() -> Self {
39+
&BIGDECIMAL_ZERO
40+
}
41+
}
42+
43+
impl MultiplicativeIdentityProperty for BigDecimal {
44+
fn multiplicative_identity() -> Self {
45+
bigdecimal_one()
46+
}
47+
}
48+
49+
impl MultiplicativeIdentityProperty for &BigDecimal {
50+
fn multiplicative_identity() -> Self {
51+
&BIGDECIMAL_ONE
52+
}
53+
}
54+
55+
impl SignumProperty for BigDecimalRef<'_> {
56+
fn is_negative_property(&self) -> bool {
57+
self.sign() == Sign::Minus
58+
}
59+
60+
fn is_positive_property(&self) -> bool {
61+
self.sign() == Sign::Plus
62+
}
63+
}
64+
65+
impl AdditiveIdentityProperty for BigDecimalRef<'_> {
66+
fn additive_identity() -> Self {
67+
BIGDECIMAL_ZERO.to_ref()
68+
}
69+
}
70+
71+
impl MultiplicativeIdentityProperty for BigDecimalRef<'_> {
72+
fn multiplicative_identity() -> Self {
73+
BIGDECIMAL_ONE.to_ref()
74+
}
75+
}
76+
77+
#[cfg(test)]
78+
mod tests;

0 commit comments

Comments
 (0)