From de01f5c7494a955129428aa21497fad79586afcb Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Fri, 14 Apr 2023 11:42:47 -0500 Subject: [PATCH] port SymbologyIdentifier test --- src/common/cpp_essentials/decoder_result.rs | 19 ++++++++++++++++--- src/common/cpp_essentials/util.rs | 16 +++++++++------- src/exceptions.rs | 2 +- src/qrcode/cpp_port/decoder.rs | 16 +++------------- src/qrcode/cpp_port/detector.rs | 4 +++- 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/common/cpp_essentials/decoder_result.rs b/src/common/cpp_essentials/decoder_result.rs index c1d25d3..a2e85fb 100644 --- a/src/common/cpp_essentials/decoder_result.rs +++ b/src/common/cpp_essentials/decoder_result.rs @@ -1,6 +1,6 @@ use std::{any::Any, rc::Rc}; -use crate::common::ECIStringBuilder; +use crate::{common::ECIStringBuilder, Exceptions}; use super::StructuredAppendInfo; @@ -18,6 +18,7 @@ where readerInit: bool, // = false; //Error _error; //std::shared_ptr _extra; + error: Option, extra: Rc, } @@ -34,6 +35,7 @@ where structuredAppend: Default::default(), isMirrored: false, readerInit: false, + error: None, extra: Default::default(), } } @@ -53,7 +55,7 @@ where } pub fn isValid(&self) -> bool { - self.content.symbology.code != 0 + self.content.symbology.code != 0 && self.error.is_none() //return includeErrors || (_content.symbology.code != 0 && !_error); } @@ -196,6 +198,17 @@ where self } + pub fn error(&self) -> &Option { + &self.error + } + pub fn setError(&mut self, error: Option) { + self.error = error + } + pub fn withError(mut self, error: Option) -> DecoderResult { + self.setError(error); + self + } + // pub fn build(self) -> DecoderResult { // } @@ -214,7 +227,7 @@ where if s.code > 0 { format!( "]{}{}", - vec!['1'; s.code as usize].into_iter().collect::(), + char::from(s.code), char::from( s.modifier + if self.content.has_eci { diff --git a/src/common/cpp_essentials/util.rs b/src/common/cpp_essentials/util.rs index 3947164..97ddde1 100644 --- a/src/common/cpp_essentials/util.rs +++ b/src/common/cpp_essentials/util.rs @@ -41,26 +41,28 @@ pub fn UpdateMinMaxFloat(min: &mut f64, max: &mut f64, val: f64) { // template>> pub fn ToString>(val: T, len: usize) -> Result { - let mut len = len; - let mut val = val.into(); + let mut len = len as isize; + let val = val.into(); + let mut val = val as isize; - let mut result: String = vec!['0'; len].iter().collect(); + let mut result = vec!['0'; len as usize]; len -= 1; // std::string result(len--, '0'); - if (val < 0) { + if val < 0 { return Err(Exceptions::format_with("Invalid value")); } while len >= 0 && val != 0 { - result.replace_range(len..len, &char::from(b'0' + (val % 10) as u8).to_string()); + result[len as usize] = char::from(b'0' + (val % 10) as u8); + // result.replace_range((len as usize)..(len as usize), &char::from(b'0' + (val % 10) as u8).to_string()); len -= 1; val /= 10; } // for (; len >= 0 && val != 0; --len, val /= 10) { // result[len] = '0' + val % 10;} - if (val != 0) { + if val != 0 { return Err(Exceptions::format_with("Invalid value")); } - Ok(result) + Ok(result.iter().collect()) } diff --git a/src/exceptions.rs b/src/exceptions.rs index 6001fa5..ae2788d 100644 --- a/src/exceptions.rs +++ b/src/exceptions.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; use thiserror::Error; #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[derive(Error, Debug, PartialEq, Eq)] +#[derive(Error, Debug, PartialEq, Eq, Clone)] pub enum Exceptions { #[error("IllegalArgumentException{}", if .0.is_empty() { String::new() } else { format!(" - {}", .0) })] IllegalArgumentException(String), diff --git a/src/qrcode/cpp_port/decoder.rs b/src/qrcode/cpp_port/decoder.rs index 279b4cf..0dd0e4d 100644 --- a/src/qrcode/cpp_port/decoder.rs +++ b/src/qrcode/cpp_port/decoder.rs @@ -297,7 +297,7 @@ pub fn DecodeBitStream( let mut structuredAppend = StructuredAppendInfo::default(); let modeBitLength = Mode::get_codec_mode_bits_length(version); - let _res = (|| { + let res = (|| { while (!IsEndOfStream(&mut bits, version)?) { let mode: Mode; if (modeBitLength == 0) { @@ -380,23 +380,13 @@ pub fn DecodeBitStream( } } Ok(()) - })()?; - // } catch (std::out_of_range& e) { // see BitSource::readBits - // error = FormatError("Truncated bit stream"); - // } catch (Error e) { - // error = std::move(e); - // } + })(); Ok(DecoderResult::with_eci_string_builder(result) + .withError(res.err()) .withEcLevel(ecLevel.to_string()) .withVersionNumber(version.getVersionNumber()) .withStructuredAppend(structuredAppend)) - - // return DecoderResult(std::move(result)) - // .setError(std::move(error)) - // .setEcLevel(ToString(ecLevel)) - // .setVersionNumber(version.versionNumber()) - // .setStructuredAppend(structuredAppend); } pub fn Decode(bits: &BitMatrix) -> Result> { diff --git a/src/qrcode/cpp_port/detector.rs b/src/qrcode/cpp_port/detector.rs index f0e159a..718791b 100644 --- a/src/qrcode/cpp_port/detector.rs +++ b/src/qrcode/cpp_port/detector.rs @@ -308,6 +308,7 @@ pub fn EstimateDimension( } } +/// This function can panic pub fn TraceLine(image: &BitMatrix, p: Point, d: Point, edge: i32) -> impl RegressionLineTrait { let mut cur = EdgeTracer::new(image, p, d - p); let mut line = RegressionLine::default(); @@ -337,7 +338,8 @@ pub fn TraceLine(image: &BitMatrix, p: Point, d: Point, edge: i32) -> impl Regre let mut c = EdgeTracer::new(image, curI.p, curI.direction(dir)); let stepCount = (Point::maxAbsComponent(cur.p - p)) as i32; loop { - line.add(Point::centered(c.p)); + line.add(Point::centered(c.p)) + .expect("could not add point on line"); if !(--stepCount > 0 && c.stepAlongEdge(dir, Some(true))) { break;