Improve BigDecimal parsing from Float with ryu crate (#1176)

This commit is contained in:
tyranron 2023-07-10 13:31:32 +03:00
parent d9dfae6d59
commit f1ecde260c
No known key found for this signature in database
GPG key ID: 762E144FB230A4F0
2 changed files with 4 additions and 2 deletions

View file

@ -31,7 +31,7 @@ default = [
"url",
"uuid",
]
bigdecimal = ["dep:bigdecimal"]
bigdecimal = ["dep:bigdecimal", "dep:ryu"]
bson = ["dep:bson"]
chrono = ["dep:chrono"]
chrono-clock = ["chrono", "chrono/clock"]
@ -59,6 +59,7 @@ graphql-parser = { version = "0.4", optional = true }
indexmap = { version = "2.0", features = ["serde"] }
juniper_codegen = { version = "0.16.0-dev", path = "../juniper_codegen" }
rust_decimal = { version = "1.20", default-features = false, optional = true }
ryu = { version = "1.0", optional = true }
serde = { version = "1.0.122", features = ["derive"] }
serde_json = { version = "1.0.18", default-features = false, optional = true }
smartstring = "1.0"

View file

@ -44,7 +44,8 @@ mod bigdecimal_scalar {
} else if let Some(f) = v.as_float_value() {
// See akubera/bigdecimal-rs#103 for details:
// https://github.com/akubera/bigdecimal-rs/issues/103
BigDecimal::from_str(&f.to_string())
let mut buf = ryu::Buffer::new();
BigDecimal::from_str(buf.format(f))
.map_err(|e| format!("Failed to parse `BigDecimal` from `Float`: {e}"))
} else {
v.as_string_value()