chore: clippy fixes, largely removing unecessary return statements

This commit is contained in:
Henry Schimke
2024-01-02 17:15:28 -06:00
parent c3618919d7
commit 4d00d51fff
3 changed files with 24 additions and 26 deletions

View File

@@ -10,11 +10,11 @@ pub fn calculate_checksum(contents: &str) -> char {
let remainder = sum % 127; let remainder = sum % 127;
let diff = 127 - remainder; let diff = 127 - remainder;
return if diff != 127 { if diff != 127 {
diff as u8 as char diff as u8 as char
} else { } else {
0 as char 0 as char
}; }
} }
pub fn ascii_to_numeric(contents: &str) -> String { pub fn ascii_to_numeric(contents: &str) -> String {
@@ -28,7 +28,7 @@ pub fn ascii_to_numeric(contents: &str) -> String {
} }
} }
return number; number
} }
pub fn numeric_to_ascii(contents: &str) -> Result<String> { pub fn numeric_to_ascii(contents: &str) -> Result<String> {
@@ -45,21 +45,21 @@ pub fn numeric_to_ascii(contents: &str) -> Result<String> {
let first = contents.chars().nth(i).unwrap() as u8; let first = contents.chars().nth(i).unwrap() as u8;
let second = contents.chars().nth(i + 1).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); 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); ascii.push((27 + (first - 48) * 10 + (second - 48)) as char);
} else { } else {
return Err(Exceptions::illegal_argument_with(format!( return Err(Exceptions::illegal_argument_with(format!(
"Input contains an invalid character around position {}.", "Input contains an invalid character around position {}.",
i.to_string() i
))); )));
} }
i += 2; i += 2;
} }
return Ok(ascii); Ok(ascii)
} }
#[test] #[test]

View File

@@ -122,14 +122,12 @@ impl OneDReader for TelepenReader {
// Narrow white: . // Narrow white: .
pattern[patternLength] = 0; pattern[patternLength] = 0;
} }
} else if isBlack {
// Wide black: BBB
pattern[patternLength] = 3;
} else { } else {
if isBlack { // Wide white: ...
// Wide black: BBB pattern[patternLength] = 2;
pattern[patternLength] = 3;
} else {
// Wide white: ...
pattern[patternLength] = 2;
}
} }
patternLength += 1; patternLength += 1;
@@ -356,7 +354,7 @@ impl TelepenReader {
j = 0; j = 0;
let median = minBar as f32 + maxBar as f32 / 2.0; let median = minBar + maxBar / 2.0;
let mut passed = true; let mut passed = true;
// First 10 items must be: // First 10 items must be:
@@ -410,13 +408,13 @@ impl TelepenReader {
i = start + 20; i = start + 20;
while i < self.counterLength { 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); return Ok((i - 1) as u32);
} }
i += 1; i += 1;
} }
} }
return Ok((self.counterLength - 1) as u32); Ok((self.counterLength - 1) as u32)
} }
} }

View File

@@ -46,7 +46,7 @@ impl OneDimensionalCodeWriter for TelepenWriter {
hints.get(&EncodeHintType::TELEPEN_AS_NUMERIC), hints.get(&EncodeHintType::TELEPEN_AS_NUMERIC),
Some(EncodeHintValue::TelepenAsNumeric(true)) Some(EncodeHintValue::TelepenAsNumeric(true))
) { ) {
decodedContents = telepen_common::numeric_to_ascii(&contents)?; decodedContents = telepen_common::numeric_to_ascii(contents)?;
} }
// Calculate the checksum character // Calculate the checksum character
@@ -61,9 +61,9 @@ impl OneDimensionalCodeWriter for TelepenWriter {
// Content // Content
for c in decodedContents.chars() { for c in decodedContents.chars() {
if c as u32 > 127 { if c as u32 > 127 {
return Err(Exceptions::illegal_argument_with(format!( return Err(Exceptions::illegal_argument_with(
"Telepen only supports ASCII characters." "Telepen only supports ASCII characters.".to_string(),
))); ));
} }
binary = self.add_to_binary(c, binary) binary = self.add_to_binary(c, binary)
@@ -129,9 +129,9 @@ impl OneDimensionalCodeWriter for TelepenWriter {
resultPosition += 4; resultPosition += 4;
} }
"0" => { "0" => {
return Err(Exceptions::illegal_argument_with(format!( return Err(Exceptions::illegal_argument_with(
"Invalid bit combination!" "Invalid bit combination!".to_string(),
))); ));
} }
_ => { _ => {
// B... (B.)* B... // B... (B.)* B...
@@ -185,7 +185,7 @@ impl TelepenWriter {
// of 1s in the 8 bits. // of 1s in the 8 bits.
bits[7] = oneCount % 2 != 0; bits[7] = oneCount % 2 != 0;
return bits; bits
} }
fn add_to_binary(&self, c: char, mut binary: String) -> String { fn add_to_binary(&self, c: char, mut binary: String) -> String {
@@ -196,7 +196,7 @@ impl TelepenWriter {
binary.push(if bits[i] { '1' } else { '0' }); binary.push(if bits[i] { '1' } else { '0' });
} }
return binary; binary
} }
} }