You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2024/02/27 09:50:41 UTC

(avro) 01/01: AVRO-3949: [Rust]: Add support for serde to apache_avro::Decimal

This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch avro-3949-serde-for-decimal
in repository https://gitbox.apache.org/repos/asf/avro.git

commit eece4f31af7e083ccbf5e569edd9c4c6398533f7
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
AuthorDate: Tue Feb 27 11:49:56 2024 +0200

    AVRO-3949: [Rust]: Add support for serde to apache_avro::Decimal
    
    Signed-off-by: Martin Tzvetanov Grigorov <mg...@apache.org>
---
 lang/rust/Cargo.lock          |  1 +
 lang/rust/avro/Cargo.toml     |  2 +-
 lang/rust/avro/src/decimal.rs | 14 +++++++++++++-
 3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/lang/rust/Cargo.lock b/lang/rust/Cargo.lock
index d35261e07..b5d4f258c 100644
--- a/lang/rust/Cargo.lock
+++ b/lang/rust/Cargo.lock
@@ -821,6 +821,7 @@ dependencies = [
  "autocfg",
  "num-integer",
  "num-traits",
+ "serde",
 ]
 
 [[package]]
diff --git a/lang/rust/avro/Cargo.toml b/lang/rust/avro/Cargo.toml
index f73e902b9..bfb2c9573 100644
--- a/lang/rust/avro/Cargo.toml
+++ b/lang/rust/avro/Cargo.toml
@@ -61,7 +61,7 @@ crc32fast = { default-features = false, version = "1.4.0", optional = true }
 digest = { default-features = false, version = "0.10.7", features = ["core-api"] }
 libflate = { default-features = false, version = "2.0.0", features = ["std"] }
 log = { workspace = true }
-num-bigint = { default-features = false, version = "0.4.4" }
+num-bigint = { default-features = false, version = "0.4.4", features = ["std", "serde"] }
 regex-lite = { default-features = false, version = "0.1.5", features = ["std", "string"] }
 serde = { workspace = true }
 serde_json = { workspace = true }
diff --git a/lang/rust/avro/src/decimal.rs b/lang/rust/avro/src/decimal.rs
index c0646590e..f5dd07cbf 100644
--- a/lang/rust/avro/src/decimal.rs
+++ b/lang/rust/avro/src/decimal.rs
@@ -18,7 +18,7 @@
 use crate::{AvroResult, Error};
 use num_bigint::{BigInt, Sign};
 
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Eq, serde::Serialize, serde::Deserialize)]
 pub struct Decimal {
     value: BigInt,
     len: usize,
@@ -132,4 +132,16 @@ mod tests {
 
         Ok(())
     }
+
+    #[test]
+    fn avro_3949_decimal_serde() -> TestResult {
+        let decimal = Decimal::from(&[1, 2, 3]);
+
+        let ser = serde_json::to_string(&decimal)?;
+        let de = serde_json::from_str(&ser)?;
+        std::assert_eq!(decimal, de);
+
+        Ok(())
+    }
+
 }