From f9ec5c5dece3ce7c73650f4de0f59dcfbfcee83c Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Thu, 26 Jan 2023 13:52:31 -0600 Subject: [PATCH 1/2] add basic serde support --- Cargo.toml | 6 ++++-- src/barcode_format.rs | 4 ++++ src/datamatrix/encoder/symbol_shape_hint.rs | 4 ++++ src/decode_hints.rs | 6 ++++++ src/dimension.rs | 4 ++++ src/encode_hints.rs | 5 +++++ src/pdf417/encoder/dimensions.rs | 4 ++++ src/pdf417/pdf_417_result_metadata.rs | 4 ++++ src/rxing_result.rs | 9 ++++----- src/rxing_result_metadata.rs | 5 +++++ src/rxing_result_point.rs | 4 ++++ 11 files changed, 48 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 80faf1f..9a6e294 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ rxing-one-d-proc-derive = "0.3" num = "0.4.0" svg = {version = "0.13", optional = true} resvg = {version = "0.28.0", optional = true, default-features=false} +serde = { version = "1.0", features = ["derive", "rc"], optional = true } [dev-dependencies] java-properties = "1.4.1" @@ -37,10 +38,11 @@ java-rand = "0.2.0" rand = "0.8.5" [features] -default = ["image"] +default = ["image", "serde"] image = ["dep:image", "dep:imageproc"] allow_forced_iso_ied_18004_compliance = [] svg_write = ["dep:svg"] svg_read = ["dep:resvg", "image"] wasm_support = ["chrono/wasmbind"] -experimental_features = [] \ No newline at end of file +experimental_features = [] +serde = ["dep:serde"] \ No newline at end of file diff --git a/src/barcode_format.rs b/src/barcode_format.rs index 50b05e0..0f1f45f 100644 --- a/src/barcode_format.rs +++ b/src/barcode_format.rs @@ -18,11 +18,15 @@ use std::fmt::Display; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + /** * Enumerates barcode formats known to this package. Please keep alphabetized. * * @author Sean Owen */ +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] pub enum BarcodeFormat { /** Aztec 2D barcode format. */ diff --git a/src/datamatrix/encoder/symbol_shape_hint.rs b/src/datamatrix/encoder/symbol_shape_hint.rs index d0eabf5..aad6ea1 100644 --- a/src/datamatrix/encoder/symbol_shape_hint.rs +++ b/src/datamatrix/encoder/symbol_shape_hint.rs @@ -14,10 +14,14 @@ * limitations under the License. */ +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + /** * Enumeration for DataMatrix symbol shape hint. It can be used to force square or rectangular * symbols. */ +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum SymbolShapeHint { FORCE_NONE, diff --git a/src/decode_hints.rs b/src/decode_hints.rs index 905fdb7..53649a4 100644 --- a/src/decode_hints.rs +++ b/src/decode_hints.rs @@ -20,6 +20,9 @@ use std::collections::HashSet; use crate::{BarcodeFormat, RXingResultPointCallback}; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + /** * Encapsulates a type of hint that a caller may pass to a barcode reader to help it * more quickly or accurately decode it. It is up to implementations to decide what, @@ -29,6 +32,7 @@ use crate::{BarcodeFormat, RXingResultPointCallback}; * @author dswitkin@google.com (Daniel Switkin) * @see Reader#decode(BinaryBitmap,java.util.Map) */ +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Eq, PartialEq, Hash, Debug, Clone, Copy)] pub enum DecodeHintType { /** @@ -137,6 +141,7 @@ pub enum DecodeHintType { }*/ } +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Clone)] pub enum DecodeHintValue { /** @@ -196,6 +201,7 @@ pub enum DecodeHintValue { * The caller needs to be notified via callback when a possible {@link RXingResultPoint} * is found. Maps to a {@link RXingResultPointCallback}. */ + #[cfg_attr(feature = "serde", serde(skip_serializing, skip_deserializing))] NeedResultPointCallback(RXingResultPointCallback), /** diff --git a/src/dimension.rs b/src/dimension.rs index 2a21c50..ea439c7 100644 --- a/src/dimension.rs +++ b/src/dimension.rs @@ -20,9 +20,13 @@ use std::fmt; use crate::Exceptions; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + /** * Simply encapsulates a width and height. */ +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Eq, PartialEq, Hash, Copy, Clone)] pub struct Dimension(usize, usize); diff --git a/src/encode_hints.rs b/src/encode_hints.rs index 5bc529a..b18e5bc 100644 --- a/src/encode_hints.rs +++ b/src/encode_hints.rs @@ -18,11 +18,15 @@ use crate::{pdf417::encoder::Dimensions, Dimension}; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + /** * These are a set of hints that you may pass to Writers to specify their behavior. * * @author dswitkin@google.com (Daniel Switkin) */ +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] pub enum EncodeHintType { /** @@ -176,6 +180,7 @@ pub enum EncodeHintType { CODE128_COMPACT, } +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum EncodeHintValue { /** * Specifies what degree of error correction to use, for example in QR Codes. diff --git a/src/pdf417/encoder/dimensions.rs b/src/pdf417/encoder/dimensions.rs index c0c68dd..354be7a 100644 --- a/src/pdf417/encoder/dimensions.rs +++ b/src/pdf417/encoder/dimensions.rs @@ -14,11 +14,15 @@ * limitations under the License. */ +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + /** * Data object to specify the minimum and maximum number of rows and columns for a PDF417 barcode. * * @author qwandor@google.com (Andrew Walbran) */ +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Copy, Clone, Eq, PartialEq)] pub struct Dimensions { minCols: usize, diff --git a/src/pdf417/pdf_417_result_metadata.rs b/src/pdf417/pdf_417_result_metadata.rs index 7256eee..6796352 100644 --- a/src/pdf417/pdf_417_result_metadata.rs +++ b/src/pdf417/pdf_417_result_metadata.rs @@ -14,9 +14,13 @@ * limitations under the License. */ +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + /** * @author Guenther Grau */ +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug, PartialEq, Eq)] pub struct PDF417RXingResultMetadata { segmentIndex: usize, diff --git a/src/rxing_result.rs b/src/rxing_result.rs index 6225e78..2176aac 100644 --- a/src/rxing_result.rs +++ b/src/rxing_result.rs @@ -14,20 +14,19 @@ * limitations under the License. */ -//package com.google.zxing; - -//import java.util.EnumMap; -//import java.util.Map; - use std::{collections::HashMap, fmt}; use crate::{BarcodeFormat, RXingResultMetadataType, RXingResultMetadataValue, RXingResultPoint}; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + /** *

Encapsulates the result of decoding a barcode within an image.

* * @author Sean Owen */ +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Clone)] pub struct RXingResult { text: String, diff --git a/src/rxing_result_metadata.rs b/src/rxing_result_metadata.rs index ba0aa25..01dca20 100644 --- a/src/rxing_result_metadata.rs +++ b/src/rxing_result_metadata.rs @@ -20,12 +20,16 @@ use std::rc::Rc; use crate::pdf417::PDF417RXingResultMetadata; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + /** * Represents some type of metadata about the result of the decoding that the decoder * wishes to communicate back to the caller. * * @author Sean Owen */ +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Eq, PartialEq, Hash, Debug, Clone)] pub enum RXingResultMetadataType { /** @@ -126,6 +130,7 @@ impl From for RXingResultMetadataType { } } +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug, PartialEq, Eq, Clone)] pub enum RXingResultMetadataValue { /** diff --git a/src/rxing_result_point.rs b/src/rxing_result_point.rs index c0f3575..fb3128f 100644 --- a/src/rxing_result_point.rs +++ b/src/rxing_result_point.rs @@ -3,12 +3,16 @@ use std::{fmt, iter::Sum}; use crate::ResultPoint; use std::hash::Hash; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + /** *

Encapsulates a point of interest in an image containing a barcode. Typically, this * would be the location of a finder pattern or the corner of the barcode, for example.

* * @author Sean Owen */ +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, Default)] pub struct RXingResultPoint { pub(crate) x: f32, From e59dfebc83ab86390bcd0670a1c824541591eae5 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Thu, 26 Jan 2023 13:55:10 -0600 Subject: [PATCH 2/2] fix warning --- src/encode_hints.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/encode_hints.rs b/src/encode_hints.rs index b18e5bc..ed26d65 100644 --- a/src/encode_hints.rs +++ b/src/encode_hints.rs @@ -16,6 +16,8 @@ //package com.google.zxing; +#![allow(deprecated)] + use crate::{pdf417::encoder::Dimensions, Dimension}; #[cfg(feature = "serde")]