Skip to content

Commit

Permalink
chore: clippy fixes, largely removing unecessary return statements
Browse files Browse the repository at this point in the history
  • Loading branch information
hschimke committed Jan 2, 2024
1 parent c361891 commit 4d00d51
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
14 changes: 7 additions & 7 deletions src/oned/telepen_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ pub fn calculate_checksum(contents: &str) -> char {

let remainder = sum % 127;
let diff = 127 - remainder;
return if diff != 127 {
if diff != 127 {
diff as u8 as char
} else {
0 as char
};
}
}

pub fn ascii_to_numeric(contents: &str) -> String {
Expand All @@ -28,7 +28,7 @@ pub fn ascii_to_numeric(contents: &str) -> String {
}
}

return number;
number
}

pub fn numeric_to_ascii(contents: &str) -> Result<String> {
Expand All @@ -45,21 +45,21 @@ pub fn numeric_to_ascii(contents: &str) -> Result<String> {
let first = contents.chars().nth(i).unwrap() as u8;
let second = contents.chars().nth(i + 1).unwrap() as u8;

if second == 88 && first >= 48 && first <= 57 {
if second == 88 && (48..=57).contains(&first) {
ascii.push((17 + first - 48) as char);
} else if second >= 48 && second <= 57 && first >= 48 && first <= 57 {
} else if (48..=57).contains(&second) && (48..=57).contains(&first) {
ascii.push((27 + (first - 48) * 10 + (second - 48)) as char);
} else {
return Err(Exceptions::illegal_argument_with(format!(
"Input contains an invalid character around position {}.",
i.to_string()
i
)));
}

i += 2;
}

return Ok(ascii);
Ok(ascii)
}

#[test]
Expand Down
18 changes: 8 additions & 10 deletions src/oned/telepen_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,12 @@ impl OneDReader for TelepenReader {
// Narrow white: .
pattern[patternLength] = 0;
}
} else if isBlack {
// Wide black: BBB
pattern[patternLength] = 3;
} else {
if isBlack {
// Wide black: BBB
pattern[patternLength] = 3;
} else {
// Wide white: ...
pattern[patternLength] = 2;
}
// Wide white: ...
pattern[patternLength] = 2;
}

patternLength += 1;
Expand Down Expand Up @@ -356,7 +354,7 @@ impl TelepenReader {

j = 0;

let median = minBar as f32 + maxBar as f32 / 2.0;
let median = minBar + maxBar / 2.0;
let mut passed = true;

// First 10 items must be:
Expand Down Expand Up @@ -410,13 +408,13 @@ impl TelepenReader {
i = start + 20;

while i < self.counterLength {
if (self.counters[i] as f32) > (maxBar as f32 * (1.0 + TOLERANCE)) {
if (self.counters[i] as f32) > (maxBar * (1.0 + TOLERANCE)) {
return Ok((i - 1) as u32);
}

i += 1;
}
}
return Ok((self.counterLength - 1) as u32);
Ok((self.counterLength - 1) as u32)
}
}
18 changes: 9 additions & 9 deletions src/oned/telepen_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl OneDimensionalCodeWriter for TelepenWriter {
hints.get(&EncodeHintType::TELEPEN_AS_NUMERIC),
Some(EncodeHintValue::TelepenAsNumeric(true))
) {
decodedContents = telepen_common::numeric_to_ascii(&contents)?;
decodedContents = telepen_common::numeric_to_ascii(contents)?;
}

// Calculate the checksum character
Expand All @@ -61,9 +61,9 @@ impl OneDimensionalCodeWriter for TelepenWriter {
// Content
for c in decodedContents.chars() {
if c as u32 > 127 {
return Err(Exceptions::illegal_argument_with(format!(
"Telepen only supports ASCII characters."
)));
return Err(Exceptions::illegal_argument_with(
"Telepen only supports ASCII characters.".to_string(),
));
}

binary = self.add_to_binary(c, binary)
Expand Down Expand Up @@ -129,9 +129,9 @@ impl OneDimensionalCodeWriter for TelepenWriter {
resultPosition += 4;
}
"0" => {
return Err(Exceptions::illegal_argument_with(format!(
"Invalid bit combination!"
)));
return Err(Exceptions::illegal_argument_with(
"Invalid bit combination!".to_string(),
));
}
_ => {
// B... (B.)* B...
Expand Down Expand Up @@ -185,7 +185,7 @@ impl TelepenWriter {
// of 1s in the 8 bits.
bits[7] = oneCount % 2 != 0;

return bits;
bits
}

fn add_to_binary(&self, c: char, mut binary: String) -> String {
Expand All @@ -196,7 +196,7 @@ impl TelepenWriter {
binary.push(if bits[i] { '1' } else { '0' });
}

return binary;
binary
}
}

Expand Down

0 comments on commit 4d00d51

Please sign in to comment.