Skip to content

Commit

Permalink
cleaned up the end of Iban::from_str
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnPeel committed Apr 19, 2023
1 parent 76765a2 commit b3eca56
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,10 @@ impl FromStr for Iban {
return Err(ParseError::InvalidLength);
}

let iban = Self(iban);

let checksum = iban
.bban()
.bytes()
.chain(iban.country_code().bytes())
.chain(iban.check_digits().bytes())
let iban_bytes = iban.as_bytes();
let checksum = iban_bytes[4..]
.iter()
.chain(iban_bytes[..4].iter())
.flat_map(|character| match character {
b'0'..=b'9' => digits(character - b'0'),
b'a'..=b'z' => digits(character - b'a' + 10),
Expand All @@ -338,14 +335,13 @@ impl FromStr for Iban {
} else {
checksum
}
})
% 97;
});

if checksum != 1 {
if checksum % 97 != 1 {
return Err(ParseError::WrongChecksum);
}

Ok(iban)
Ok(Self(iban))
}
}

Expand Down

0 comments on commit b3eca56

Please sign in to comment.