switch to using cargo version of one-d-proc-derive

This commit is contained in:
Henry Schimke
2022-12-31 14:43:48 -06:00
parent d11f32d693
commit d08f8aaf73
19 changed files with 20 additions and 211 deletions

View File

@@ -1,6 +1,9 @@
[package]
name = "rxing"
version = "0.1.0"
description="A rust port of the zxing barcode library."
license="Apache-2.0"
repository="https://github.com/hschimke/rxing"
edition = "2021"
exclude = [
"test_resources/*",
@@ -22,7 +25,7 @@ image = {version = "0.24", optional = true}
imageproc = {version = "0.23.0", optional = true}
unicode-segmentation = "1.10.0"
codepage-437 = "0.1.0"
one-d-proc-derive = { path = "one-d-proc-derive" }
rxing-one-d-proc-derive = "0.1"
num = "0.4.0"
[dev-dependencies]

View File

@@ -1,13 +0,0 @@
[package]
name = "one-d-proc-derive"
version = "0.1.0"
edition = "2021"
[lib]
proc-macro = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
syn = "1.0"
quote = "1.0"

View File

@@ -1,181 +0,0 @@
use proc_macro::TokenStream;
use quote::quote;
use syn;
#[proc_macro_derive(OneDReader)]
pub fn one_d_reader_derive(input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree
// that we can manipulate
let ast = syn::parse(input).unwrap();
// Build the trait implementation
impl_one_d_reader_macro(&ast)
}
fn impl_one_d_reader_macro(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
use std::collections::HashMap;
use crate::result_point::ResultPoint;
use crate::DecodeHintType;
use crate::DecodingHintDictionary;
use crate::RXingResultMetadataType;
use crate::RXingResultMetadataValue;
use crate::RXingResultPoint;
use crate::Reader;
impl Reader for #name {
fn decode(&mut self, image: &crate::BinaryBitmap) -> Result<crate::RXingResult, Exceptions> {
self.decode_with_hints(image, &HashMap::new())
}
// Note that we don't try rotation without the try harder flag, even if rotation was supported.
fn decode_with_hints(
&mut self,
image: &crate::BinaryBitmap,
hints: &DecodingHintDictionary,
) -> Result<crate::RXingResult, Exceptions> {
if let Ok(res) = self.doDecode(image, hints) {
Ok(res)
}else {
let tryHarder = hints.contains_key(&DecodeHintType::TRY_HARDER);
if tryHarder && image.isRotateSupported() {
let rotatedImage = image.rotateCounterClockwise();
let mut result = self.doDecode(&rotatedImage, hints)?;
// Record that we found it rotated 90 degrees CCW / 270 degrees CW
let metadata = result.getRXingResultMetadata();
let mut orientation = 270;
if metadata.contains_key(&RXingResultMetadataType::ORIENTATION) {
// But if we found it reversed in doDecode(), add in that result here:
orientation = (orientation +
if let Some(crate::RXingResultMetadataValue::Orientation(or)) = metadata.get(&RXingResultMetadataType::ORIENTATION) {
*or
}else {
0
}) % 360;
}
result.putMetadata(RXingResultMetadataType::ORIENTATION, RXingResultMetadataValue::Orientation(orientation));
// Update result points
// let points = result.getRXingResultPoints();
// if points != null {
let height = rotatedImage.getHeight();
// for point in result.getRXingResultPointsMut().iter_mut() {
let total_points = result.getRXingResultPoints().len();
let points = result.getRXingResultPointsMut();
for i in 0..total_points{
// for (int i = 0; i < points.length; i++) {
points[i] = RXingResultPoint::new(height as f32- points[i].getY() - 1.0, points[i].getX());
}
// }
Ok(result)
} else {
return Err(Exceptions::NotFoundException("".to_owned()))
}
}
}
}
};
gen.into()
}
#[proc_macro_derive(EANReader)]
pub fn ean_reader_derive(input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree
// that we can manipulate
let ast = syn::parse(input).unwrap();
// Build the trait implementation
impl_ean_reader_macro(&ast)
}
fn impl_ean_reader_macro(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
impl super::OneDReader for #name {
fn decodeRow(
&mut self,
rowNumber: u32,
row: &crate::common::BitArray,
hints: &crate::DecodingHintDictionary,
) -> Result<crate::RXingResult, crate::Exceptions> {
self.decodeRowWithGuardRange(rowNumber, row, &self.findStartGuardPattern(row)?, hints)
}
}
};
gen.into()
}
#[proc_macro_derive(OneDWriter)]
pub fn one_d_writer_derive(input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree
// that we can manipulate
let ast = syn::parse(input).unwrap();
// Build the trait implementation
impl_one_d_writer_macro(&ast)
}
fn impl_one_d_writer_macro(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
use crate::{
EncodeHintType, EncodeHintValue, Exceptions, Writer,
};
use std::collections::HashMap;
impl Writer for #name {
fn encode(
&self,
contents: &str,
format: &crate::BarcodeFormat,
width: i32,
height: i32,
) -> Result<crate::common::BitMatrix, crate::Exceptions> {
self.encode_with_hints(contents, format, width, height, &HashMap::new())
}
fn encode_with_hints(
&self,
contents: &str,
format: &crate::BarcodeFormat,
width: i32,
height: i32,
hints: &crate::EncodingHintDictionary,
) -> Result<crate::common::BitMatrix, crate::Exceptions> {
if contents.is_empty() {
return Err(Exceptions::IllegalArgumentException(
"Found empty contents".to_owned(),
));
}
if width < 0 || height < 0 {
return Err(Exceptions::IllegalArgumentException(format!(
"Negative size is not allowed. Input: {}x{}",
width, height
)));
}
if let Some(supportedFormats) = self.getSupportedWriteFormats() {
if !supportedFormats.contains(format) {
return Err(Exceptions::IllegalArgumentException(format!(
"Can only encode {:?}, but got {:?}",
supportedFormats, format
)));
}
}
let mut sidesMargin = self.getDefaultMargin();
if let Some(EncodeHintValue::Margin(margin)) = hints.get(&EncodeHintType::MARGIN) {
sidesMargin = margin.parse::<u32>().unwrap();
}
// if hints.contains_key(&EncodeHintType::MARGIN) {
// sidesMargin = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
// }
let code = self.encode_oned_with_hints(contents, hints)?;
Self::renderRXingResult(&code, width, height, sidesMargin)
}
}
};
gen.into()
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
use one_d_proc_derive::OneDReader;
use rxing_one_d_proc_derive::OneDReader;
use crate::common::BitArray;
use crate::BarcodeFormat;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
use one_d_proc_derive::OneDWriter;
use rxing_one_d_proc_derive::OneDWriter;
use crate::BarcodeFormat;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
use one_d_proc_derive::OneDReader;
use rxing_one_d_proc_derive::OneDReader;
use crate::{common::BitArray, BarcodeFormat, Exceptions, RXingResult};

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
use one_d_proc_derive::OneDWriter;
use rxing_one_d_proc_derive::OneDWriter;
use crate::BarcodeFormat;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
use one_d_proc_derive::OneDReader;
use rxing_one_d_proc_derive::OneDReader;
use crate::common::BitArray;
use crate::{BarcodeFormat, Exceptions, RXingResult};

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
use one_d_proc_derive::OneDWriter;
use rxing_one_d_proc_derive::OneDWriter;
use crate::BarcodeFormat;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
use one_d_proc_derive::OneDReader;
use rxing_one_d_proc_derive::OneDReader;
use crate::{common::BitArray, BarcodeFormat, Exceptions, RXingResult};

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
use one_d_proc_derive::OneDWriter;
use rxing_one_d_proc_derive::OneDWriter;
use crate::BarcodeFormat;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
use one_d_proc_derive::{EANReader, OneDReader};
use rxing_one_d_proc_derive::{EANReader, OneDReader};
use super::UPCEANReader;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
use one_d_proc_derive::OneDWriter;
use rxing_one_d_proc_derive::OneDWriter;
use crate::{
oned::{upc_ean_reader, EAN13Reader},

View File

@@ -16,7 +16,7 @@
use super::OneDReader;
use crate::{BarcodeFormat, Exceptions};
use one_d_proc_derive::{EANReader, OneDReader};
use rxing_one_d_proc_derive::{EANReader, OneDReader};
use super::upc_ean_reader;
use super::UPCEANReader;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
use one_d_proc_derive::OneDWriter;
use rxing_one_d_proc_derive::OneDWriter;
use crate::{
oned::{EAN8Reader, UPCEANReader},

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
use one_d_proc_derive::OneDReader;
use rxing_one_d_proc_derive::OneDReader;
use crate::{common::BitArray, BarcodeFormat, DecodeHintValue, Exceptions, RXingResult};

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
use one_d_proc_derive::OneDWriter;
use rxing_one_d_proc_derive::OneDWriter;
use crate::BarcodeFormat;

View File

@@ -16,7 +16,7 @@
use super::{OneDReader, UPCEANReader, L_AND_G_PATTERNS};
use crate::{BarcodeFormat, Exceptions};
use one_d_proc_derive::{EANReader, OneDReader};
use rxing_one_d_proc_derive::{EANReader, OneDReader};
/**
* <p>Implements decoding of the UPC-E format.</p>

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
use one_d_proc_derive::OneDWriter;
use rxing_one_d_proc_derive::OneDWriter;
use crate::BarcodeFormat;