Skip to content
This repository was archived by the owner on Nov 3, 2025. It is now read-only.

Commit 782b7e6

Browse files
authored
Merge pull request #67 from artichoke/decode-checksum-match
Use a match statement for decode checksum validation
2 parents 2981dd6 + f7d7f9e commit 782b7e6

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/lib.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -306,17 +306,18 @@ pub fn decode<T: AsRef<[u8]>>(encoded: T) -> Result<Vec<u8>, DecodeError> {
306306
let a = VOWELS.find_byte(left).ok_or(DecodeError::ExpectedVowel)? as u8;
307307
let c = VOWELS.find_byte(right).ok_or(DecodeError::ExpectedVowel)? as u8;
308308

309-
if mid == b'x' {
310-
if a != checksum % 6 || c != checksum / 6 {
311-
return Err(DecodeError::ChecksumMismatch);
309+
match mid {
310+
b'x' if a != checksum % 6 || c != checksum / 6 => Err(DecodeError::ChecksumMismatch),
311+
b'x' => Ok(decoded),
312+
_ => {
313+
let b = CONSONANTS
314+
.find_byte(mid)
315+
.ok_or(DecodeError::ExpectedConsonant)? as u8;
316+
let byte = decode_3_tuple(a, b, c, checksum)?;
317+
decoded.push(byte);
318+
Ok(decoded)
312319
}
313-
} else {
314-
let b = CONSONANTS
315-
.find_byte(mid)
316-
.ok_or(DecodeError::ExpectedConsonant)? as u8;
317-
decoded.push(decode_3_tuple(a, b, c, checksum)?);
318320
}
319-
Ok(decoded)
320321
} else {
321322
Err(DecodeError::Corrupted)
322323
}

0 commit comments

Comments
 (0)