This page shows how to add laravel-rust-iban to a project, validate a pasted IBAN and handle the errors.
Install
In a Laravel for Rust application, add the adapter crate:
[dependencies]
laravel-rust-iban = { git = "https://github.com/bowerbird-si/laravel-rust-iban" }
Turn on the minijinja feature to get the template filters:
[dependencies]
laravel-rust-iban = { git = "https://github.com/bowerbird-si/laravel-rust-iban", features = ["minijinja"] }
Outside Laravel, depend on the core crate only. It has no runtime dependencies:
[dependencies]
laravel-rust-iban-core = { git = "https://github.com/bowerbird-si/laravel-rust-iban" }
The adapter re-exports everything in the core, so the examples in these docs use laravel_rust_iban.
With the core crate, write laravel_rust_iban_core instead.
Parse an IBAN
Iban::parse accepts the way people paste an IBAN: lower case, spaces, dashes and a leading IBAN
tag. It returns a validated Iban or an Error.
use laravel_rust_iban::{Iban, NationalCheck};
fn main() -> Result<(), laravel_rust_iban::Error> {
let iban = Iban::parse("IBAN be68-5390-0754-7034")?;
assert_eq!(iban.to_string(), "BE68539007547034");
assert_eq!(iban.human_format(), "BE68 5390 0754 7034");
assert_eq!(iban.country().name(), "Belgium");
assert_eq!(iban.bank(), Some("539"));
assert_eq!(iban.national_check(), NationalCheck::Valid);
Ok(())
}
Iban also implements FromStr, so "be68 5390 0754 7034".parse::<Iban>() works the same way.
Its Display and AsRef<str> give the electronic form, and its Debug output masks the account
number, so a logged Iban shows Iban("BE** **** **** 7034").
To check without keeping the value, use verify:
fn main() {
assert!(laravel_rust_iban::verify("be68 5390 0754 7034"));
assert!(!laravel_rust_iban::verify("BE68 5390 0754 7035"));
}Errors
Parsing checks the input in a fixed order and returns the first problem it finds. The error messages never contain the input, so they are safe to log.
Error | When | Message |
|---|---|---|
InputTooLong | The input is longer than 1024 bytes | IBAN input exceeds 1024 bytes |
NotMachineFormat | Strict parsing found a byte other than A–Z or 0–9 | IBAN must use electronic format |
InvalidLength | Fewer than 5 or more than 34 characters, or not the country's IBAN length | IBAN input has an invalid length |
InvalidStructure | The first two characters are not letters, the check digits are not digits, or the BBAN does not fit the country's structure | IBAN input has an invalid structure |
UnknownCountry | The prefix is not in the registry | IBAN country is unsupported |
NonCanonicalCheckDigits | The check digits are 00, 01 or 99 | IBAN check digits must be between 02 and 98 |
InvalidCheckDigits | The MOD 97-10 check fails | IBAN check digits are invalid |
Two more variants come from the national check digit functions:
UnsupportedNationalCheck and InvalidNationalCheckDigits. The enum is #[non_exhaustive].
use laravel_rust_iban::{Error, Iban};
fn main() {
assert_eq!(Iban::parse("BE68 5390 0754 7035"), Err(Error::InvalidCheckDigits));
assert_eq!(Iban::parse("BE68 5390 0754 703"), Err(Error::InvalidLength));
assert_eq!(Iban::parse("BE01 5390 0754 7034"), Err(Error::NonCanonicalCheckDigits));
let error = Iban::parse("BE68 5390 0754 7035").unwrap_err();
assert_eq!(error.to_string(), "IBAN check digits are invalid");
}Next steps
- Working with IBANs: formats, parts, strict parsing and check digits.
- Validation and templates: the
IbanRuleand the template filters.