From d5f1b3eb3f31ea02a6b60743072482557bc495e0 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Amiel Date: Fri, 25 Oct 2024 08:03:21 +0200 Subject: [PATCH] Remove the crate float-cmp. We can do a "classic" float comparison as clippy suggests https://rust-lang.github.io/rust-clippy/rust-1.72.0/index.html#/float_cmp and remove `float-cmp` from ```rust let error_margin = f64::EPSILON; // Use an epsilon for comparison // Or, if Rust <= 1.42, use `std::f64::EPSILON` constant instead. // let error_margin = std::f64::EPSILON; if (y - 1.23f64).abs() < error_margin { } if (y - x).abs() > error_margin { } ``` Note: we compare `f64` for predicate value here https://github.com/Orange-OpenSource/hurl/blob/07512a9cbfbba17cd16df3706f3a0c5c98b67d3d/packages/hurl/src/runner/number.rs#L32-L40 so there is no reason to use `float-cmp` in jsonpath module --- Cargo.lock | 11 ----------- packages/hurl/Cargo.toml | 1 - packages/hurl/src/jsonpath/eval/selector.rs | 8 +++----- packages/hurl_core/Cargo.toml | 1 - 4 files changed, 3 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1c4c1aec513..1711f0b65f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -429,15 +429,6 @@ version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" -[[package]] -name = "float-cmp" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b09cf3155332e944990140d967ff5eceb70df778b34f77d8075db46e4704e6d8" -dependencies = [ - "num-traits", -] - [[package]] name = "fnv" version = "1.0.7" @@ -514,7 +505,6 @@ dependencies = [ "curl", "curl-sys", "encoding", - "float-cmp", "glob", "hex", "hex-literal", @@ -542,7 +532,6 @@ name = "hurl_core" version = "6.0.0-SNAPSHOT" dependencies = [ "colored", - "float-cmp", "libxml", "regex", ] diff --git a/packages/hurl/Cargo.toml b/packages/hurl/Cargo.toml index 8c806b93b05..a677d05a0ee 100644 --- a/packages/hurl/Cargo.toml +++ b/packages/hurl/Cargo.toml @@ -24,7 +24,6 @@ clap = { version = "4.5.20", features = ["cargo", "string", "wrap_help"] } curl = "0.4.47" curl-sys = "0.4.77" encoding = "0.2.33" -float-cmp = "0.10.0" glob = "0.3.1" hex = "0.4.3" hex-literal = "0.4.1" diff --git a/packages/hurl/src/jsonpath/eval/selector.rs b/packages/hurl/src/jsonpath/eval/selector.rs index 71e0ba7e0d0..0728483f9c3 100644 --- a/packages/hurl/src/jsonpath/eval/selector.rs +++ b/packages/hurl/src/jsonpath/eval/selector.rs @@ -16,8 +16,6 @@ * */ -use float_cmp::approx_eq; - use crate::jsonpath::ast::{Predicate, PredicateFunc, Selector, Slice}; use crate::jsonpath::JsonpathResult; @@ -154,8 +152,8 @@ impl Predicate { match (value, self.func.clone()) { (_, PredicateFunc::KeyExist) => true, (serde_json::Value::Number(v), PredicateFunc::Equal(ref num)) => { - approx_eq!(f64, v.as_f64().unwrap(), num.to_f64(), ulps = 2) - } //v.as_f64().unwrap() == num.to_f64(), + (v.as_f64().unwrap() - num.to_f64()).abs() < f64::EPSILON + } (serde_json::Value::Number(v), PredicateFunc::GreaterThan(ref num)) => { v.as_f64().unwrap() > num.to_f64() } @@ -176,7 +174,7 @@ impl Predicate { v != *s } (serde_json::Value::Number(v), PredicateFunc::NotEqual(ref num)) => { - !approx_eq!(f64, v.as_f64().unwrap(), num.to_f64(), ulps = 2) + (v.as_f64().unwrap() - num.to_f64()).abs() >= f64::EPSILON } (serde_json::Value::Bool(v), PredicateFunc::EqualBool(ref s)) => v == *s, _ => false, diff --git a/packages/hurl_core/Cargo.toml b/packages/hurl_core/Cargo.toml index cb0b064eed2..6114d6ec633 100644 --- a/packages/hurl_core/Cargo.toml +++ b/packages/hurl_core/Cargo.toml @@ -11,7 +11,6 @@ repository = "https://github.com/Orange-OpenSource/hurl" [dependencies] colored = "2.1.0" -float-cmp = "0.10.0" libxml = "0.3.3" regex = "1.11.1"