port SymbologyIdentifier test

This commit is contained in:
Henry Schimke
2023-04-14 11:42:47 -05:00
parent 23951ac87c
commit de01f5c749
5 changed files with 32 additions and 25 deletions

View File

@@ -1,6 +1,6 @@
use std::{any::Any, rc::Rc}; use std::{any::Any, rc::Rc};
use crate::common::ECIStringBuilder; use crate::{common::ECIStringBuilder, Exceptions};
use super::StructuredAppendInfo; use super::StructuredAppendInfo;
@@ -18,6 +18,7 @@ where
readerInit: bool, // = false; readerInit: bool, // = false;
//Error _error; //Error _error;
//std::shared_ptr<CustomData> _extra; //std::shared_ptr<CustomData> _extra;
error: Option<Exceptions>,
extra: Rc<T>, extra: Rc<T>,
} }
@@ -34,6 +35,7 @@ where
structuredAppend: Default::default(), structuredAppend: Default::default(),
isMirrored: false, isMirrored: false,
readerInit: false, readerInit: false,
error: None,
extra: Default::default(), extra: Default::default(),
} }
} }
@@ -53,7 +55,7 @@ where
} }
pub fn isValid(&self) -> bool { 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); //return includeErrors || (_content.symbology.code != 0 && !_error);
} }
@@ -196,6 +198,17 @@ where
self self
} }
pub fn error(&self) -> &Option<Exceptions> {
&self.error
}
pub fn setError(&mut self, error: Option<Exceptions>) {
self.error = error
}
pub fn withError(mut self, error: Option<Exceptions>) -> DecoderResult<T> {
self.setError(error);
self
}
// pub fn build(self) -> DecoderResult<T> { // pub fn build(self) -> DecoderResult<T> {
// } // }
@@ -214,7 +227,7 @@ where
if s.code > 0 { if s.code > 0 {
format!( format!(
"]{}{}", "]{}{}",
vec!['1'; s.code as usize].into_iter().collect::<String>(), char::from(s.code),
char::from( char::from(
s.modifier s.modifier
+ if self.content.has_eci { + if self.content.has_eci {

View File

@@ -41,26 +41,28 @@ pub fn UpdateMinMaxFloat(min: &mut f64, max: &mut f64, val: f64) {
// template<typename T, typename = std::enable_if_t<std::is_integral_v<T>>> // template<typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
pub fn ToString<T: Into<usize>>(val: T, len: usize) -> Result<String> { pub fn ToString<T: Into<usize>>(val: T, len: usize) -> Result<String> {
let mut len = len; let mut len = len as isize;
let mut val = val.into(); 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; len -= 1;
// std::string result(len--, '0'); // std::string result(len--, '0');
if (val < 0) { if val < 0 {
return Err(Exceptions::format_with("Invalid value")); return Err(Exceptions::format_with("Invalid value"));
} }
while len >= 0 && val != 0 { 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; len -= 1;
val /= 10; val /= 10;
} }
// for (; len >= 0 && val != 0; --len, val /= 10) { // for (; len >= 0 && val != 0; --len, val /= 10) {
// result[len] = '0' + val % 10;} // result[len] = '0' + val % 10;}
if (val != 0) { if val != 0 {
return Err(Exceptions::format_with("Invalid value")); return Err(Exceptions::format_with("Invalid value"));
} }
Ok(result) Ok(result.iter().collect())
} }

View File

@@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
use thiserror::Error; use thiserror::Error;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Error, Debug, PartialEq, Eq)] #[derive(Error, Debug, PartialEq, Eq, Clone)]
pub enum Exceptions { pub enum Exceptions {
#[error("IllegalArgumentException{}", if .0.is_empty() { String::new() } else { format!(" - {}", .0) })] #[error("IllegalArgumentException{}", if .0.is_empty() { String::new() } else { format!(" - {}", .0) })]
IllegalArgumentException(String), IllegalArgumentException(String),

View File

@@ -297,7 +297,7 @@ pub fn DecodeBitStream(
let mut structuredAppend = StructuredAppendInfo::default(); let mut structuredAppend = StructuredAppendInfo::default();
let modeBitLength = Mode::get_codec_mode_bits_length(version); let modeBitLength = Mode::get_codec_mode_bits_length(version);
let _res = (|| { let res = (|| {
while (!IsEndOfStream(&mut bits, version)?) { while (!IsEndOfStream(&mut bits, version)?) {
let mode: Mode; let mode: Mode;
if (modeBitLength == 0) { if (modeBitLength == 0) {
@@ -380,23 +380,13 @@ pub fn DecodeBitStream(
} }
} }
Ok(()) 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) Ok(DecoderResult::with_eci_string_builder(result)
.withError(res.err())
.withEcLevel(ecLevel.to_string()) .withEcLevel(ecLevel.to_string())
.withVersionNumber(version.getVersionNumber()) .withVersionNumber(version.getVersionNumber())
.withStructuredAppend(structuredAppend)) .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<DecoderResult<bool>> { pub fn Decode(bits: &BitMatrix) -> Result<DecoderResult<bool>> {

View File

@@ -308,6 +308,7 @@ pub fn EstimateDimension(
} }
} }
/// This function can panic
pub fn TraceLine(image: &BitMatrix, p: Point, d: Point, edge: i32) -> impl RegressionLineTrait { pub fn TraceLine(image: &BitMatrix, p: Point, d: Point, edge: i32) -> impl RegressionLineTrait {
let mut cur = EdgeTracer::new(image, p, d - p); let mut cur = EdgeTracer::new(image, p, d - p);
let mut line = RegressionLine::default(); 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 mut c = EdgeTracer::new(image, curI.p, curI.direction(dir));
let stepCount = (Point::maxAbsComponent(cur.p - p)) as i32; let stepCount = (Point::maxAbsComponent(cur.p - p)) as i32;
loop { 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))) { if !(--stepCount > 0 && c.stepAlongEdge(dir, Some(true))) {
break; break;