Skip to content

Commit

Permalink
chore: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hschimke committed Dec 26, 2024
1 parent 8227af4 commit 363528b
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/client/result/ResultParser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ mod tests {
);
let p_res = parse_result_with_parser(&result, |v| {
Some(ParsedClientResult::Other(OtherParsedResult::new(Box::new(
v.getRawBytes().clone(),
v.getRawBytes().to_vec(),
))))
})
.unwrap();
Expand Down
12 changes: 12 additions & 0 deletions src/common/bit_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ impl BitArray {
/**
* Clears all bits (sets to false).
*/
#[inline]
pub fn clear(&mut self) {
self.bits.fill(0);
}
Expand Down Expand Up @@ -522,3 +523,14 @@ impl std::io::Write for BitArray {
Ok(())
}
}

impl std::io::Seek for BitArray {
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
self.read_offset = match pos {
std::io::SeekFrom::Start(s) => s as usize,
std::io::SeekFrom::End(e) => self.size - e as usize,
std::io::SeekFrom::Current(c) => self.read_offset + c as usize,
};
Ok(self.read_offset as u64)
}
}
8 changes: 4 additions & 4 deletions src/multi/by_quadrant_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<T: Reader> Reader for ByQuadrantReader<T> {
match result {
Ok(res) => {
let points = Self::makeAbsolute(res.getPoints(), halfWidth as f32, 0.0);
return Ok(RXingResult::new_from_existing_result(res, points));
return Ok(res.with_point(points));
}
Err(Exceptions::NotFoundException(_)) => {}
_ => return result,
Expand All @@ -75,7 +75,7 @@ impl<T: Reader> Reader for ByQuadrantReader<T> {
match result {
Ok(res) => {
let points = Self::makeAbsolute(res.getPoints(), 0.0, halfHeight as f32);
return Ok(RXingResult::new_from_existing_result(res, points));
return Ok(res.with_point(points));
}
Err(Exceptions::NotFoundException(_)) => {}
_ => return result,
Expand All @@ -90,7 +90,7 @@ impl<T: Reader> Reader for ByQuadrantReader<T> {
Ok(res) => {
let points =
Self::makeAbsolute(res.getPoints(), halfWidth as f32, halfHeight as f32);
return Ok(RXingResult::new_from_existing_result(res, points));
return Ok(res.with_point(points));
}
Err(Exceptions::NotFoundException(_)) => {}
_ => return result,
Expand All @@ -106,7 +106,7 @@ impl<T: Reader> Reader for ByQuadrantReader<T> {
quarterWidth as f32,
quarterHeight as f32,
);
Ok(RXingResult::new_from_existing_result(result, points))
Ok(result.with_point(points))
}

fn reset(&mut self) {
Expand Down
6 changes: 3 additions & 3 deletions src/multi/generic_multiple_barcode_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl<T: Reader> GenericMultipleBarcodeReader<T> {
return;
};

let resultPoints = result.getPoints().clone();
let resultPoints = result.getPoints().to_vec();

let possible_new_result = Self::translatePoints(result, xOffset, yOffset);

Expand All @@ -151,7 +151,7 @@ impl<T: Reader> GenericMultipleBarcodeReader<T> {
let mut minY: f32 = height as f32;
let mut maxX: f32 = 0.0;
let mut maxY: f32 = 0.0;
for point in &resultPoints {
for point in resultPoints.into_iter() {
let x = point.x;
let y = point.y;

Expand Down Expand Up @@ -220,7 +220,7 @@ impl<T: Reader> GenericMultipleBarcodeReader<T> {

let mut newRXingResult = RXingResult::new_complex(
result.getText(),
result.getRawBytes().clone(),
result.getRawBytes().to_vec(),
result.getNumBits(),
newPoints,
*result.getBarcodeFormat(),
Expand Down
4 changes: 2 additions & 2 deletions src/oned/multi_format_upc_ean_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ impl MultiFormatUPCEANReader {
// Transfer the metadata across
let mut resultUPCA = RXingResult::new(
&result.getText()[1..],
result.getRawBytes().clone(),
result.getPoints().clone(),
result.getRawBytes().to_vec(),
result.getPoints().to_vec(),
BarcodeFormat::UPC_A,
);
resultUPCA.putAllMetadata(result.getRXingResultMetadata().clone());
Expand Down
14 changes: 9 additions & 5 deletions src/oned/telepen_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

use std::io::Read;

use rxing_one_d_proc_derive::OneDReader;

use crate::common::{BitArray, Result};
Expand All @@ -33,14 +35,14 @@ use super::OneDReader;
#[derive(OneDReader)]
pub struct TelepenReader {
// Keep some instance variables to avoid reallocations
counters: Vec<u32>,
counters: Box<[u32]>,
counterLength: usize,
}

impl Default for TelepenReader {
fn default() -> Self {
Self {
counters: vec![0; 80],
counters: Box::new([0; 80]),
counterLength: 0,
}
}
Expand Down Expand Up @@ -171,7 +173,9 @@ impl OneDReader for TelepenReader {
}

let mut bytes: Vec<u8> = vec![0; byteLength];
bits.toBytes(0, bytes.as_mut_slice(), 0, byteLength);
// bits.toBytes(0, bytes.as_mut_slice(), 0, byteLength);
bits.read_exact(&mut bytes)
.map_err(|_| Exceptions::ILLEGAL_STATE)?;

j = 0;

Expand Down Expand Up @@ -258,7 +262,7 @@ impl OneDReader for TelepenReader {
impl TelepenReader {
pub fn new() -> Self {
Self {
counters: vec![0; 80], //Vec::with_capacity(80),
counters: Box::new([0; 80]), //Vec::with_capacity(80),
counterLength: 0,
}
}
Expand Down Expand Up @@ -318,7 +322,7 @@ impl TelepenReader {
if self.counterLength >= self.counters.len() {
let mut temp = vec![0; self.counterLength * 2];
temp[0..self.counterLength].clone_from_slice(&self.counters[..]);
self.counters = temp;
self.counters = temp.into_boxed_slice();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/oned/upc_ean_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub trait UPCEANReader: OneDReader {
),
);
decodeRXingResult.putAllMetadata(extensionRXingResult.getRXingResultMetadata().clone());
decodeRXingResult.addPoints(&mut extensionRXingResult.getPoints().clone());
decodeRXingResult.addPoints(&mut extensionRXingResult.getPoints().to_vec());
extensionLength = extensionRXingResult.getText().chars().count();
Ok(())
};
Expand Down
2 changes: 1 addition & 1 deletion src/qrcode/encoder/qrcode_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @author [email protected] (Satoru Takabayashi) - creator
* @author [email protected] (Daniel Switkin) - ported from C++
*/
use std::collections::HashMap;
use std::{collections::HashMap, io::Seek};

use unicode_segmentation::UnicodeSegmentation;

Expand Down
4 changes: 1 addition & 3 deletions src/result_point_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,5 @@ pub fn orderBestPatterns<T: Into<Point> + Copy>(patterns: &mut [T; 3]) {
std::mem::swap(&mut pointA, &mut pointC);
}

patterns[0] = pointA;
patterns[1] = pointB;
patterns[2] = pointC;
*patterns = [pointA, pointB, pointC];
}
41 changes: 19 additions & 22 deletions src/rxing_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use crate::{
RXingResultMetadataType, RXingResultMetadataValue,
};

pub type RXingResultMetaDataDictionary = HashMap<RXingResultMetadataType, RXingResultMetadataValue>;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand All @@ -37,7 +39,7 @@ pub struct RXingResult {
numBits: usize,
resultPoints: Vec<Point>,
format: BarcodeFormat,
resultMetadata: HashMap<RXingResultMetadataType, RXingResultMetadataValue>,
resultMetadata: RXingResultMetaDataDictionary,
timestamp: u128,
line_count: usize,
}
Expand Down Expand Up @@ -88,16 +90,16 @@ impl RXingResult {
}
}

pub fn new_from_existing_result(prev: Self, points: Vec<Point>) -> Self {
pub fn with_point(self, points: Vec<Point>) -> Self {
Self {
text: prev.text,
rawBytes: prev.rawBytes,
numBits: prev.numBits,
text: self.text,
rawBytes: self.rawBytes,
numBits: self.numBits,
resultPoints: points,
format: prev.format,
resultMetadata: prev.resultMetadata,
timestamp: prev.timestamp,
line_count: prev.line_count,
format: self.format,
resultMetadata: self.resultMetadata,
timestamp: self.timestamp,
line_count: self.line_count,
}
}

Expand Down Expand Up @@ -149,7 +151,7 @@ impl RXingResult {
/**
* @return raw bytes encoded by the barcode, if applicable, otherwise {@code null}
*/
pub fn getRawBytes(&self) -> &Vec<u8> {
pub fn getRawBytes(&self) -> &[u8] {
&self.rawBytes
}

Expand All @@ -166,21 +168,21 @@ impl RXingResult {
* identifying finder patterns or the corners of the barcode. The exact meaning is
* specific to the type of barcode that was decoded.
*/
pub fn getPoints(&self) -> &Vec<Point> {
pub fn getPoints(&self) -> &[Point] {
&self.resultPoints
}

pub fn getPointsMut(&mut self) -> &mut Vec<Point> {
pub fn getPointsMut(&mut self) -> &mut [Point] {
&mut self.resultPoints
}

/** Currently necessary because the external OneDReader proc macro uses it. */
pub fn getRXingResultPoints(&self) -> &Vec<Point> {
pub fn getRXingResultPoints(&self) -> &[Point] {
&self.resultPoints
}

/** Currently necessary because the external OneDReader proc macro uses it. */
pub fn getRXingResultPointsMut(&mut self) -> &mut Vec<Point> {
pub fn getRXingResultPointsMut(&mut self) -> &mut [Point] {
&mut self.resultPoints
}

Expand All @@ -196,9 +198,7 @@ impl RXingResult {
* {@code null}. This contains optional metadata about what was detected about the barcode,
* like orientation.
*/
pub fn getRXingResultMetadata(
&self,
) -> &HashMap<RXingResultMetadataType, RXingResultMetadataValue> {
pub fn getRXingResultMetadata(&self) -> &RXingResultMetaDataDictionary {
&self.resultMetadata
}

Expand All @@ -210,12 +210,9 @@ impl RXingResult {
self.resultMetadata.insert(md_type, value);
}

pub fn putAllMetadata(
&mut self,
metadata: HashMap<RXingResultMetadataType, RXingResultMetadataValue>,
) {
pub fn putAllMetadata(&mut self, metadata: RXingResultMetaDataDictionary) {
if self.resultMetadata.is_empty() {
self.resultMetadata = metadata;
let _ = std::mem::replace(&mut self.resultMetadata, metadata);
} else {
for (key, value) in metadata.into_iter() {
self.resultMetadata.insert(key, value);
Expand Down
7 changes: 0 additions & 7 deletions src/rxing_result_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,6 @@ impl Hash for Point {
}
}

// impl PartialEq for Point {
// fn eq(&self, other: &Self) -> bool {
// self.x == other.x && self.y == other.y
// }
// }
// impl Eq for Point {}

impl<T> PartialEq for PointT<T>
where
T: PartialEq,
Expand Down

0 comments on commit 363528b

Please sign in to comment.