Getting started

laravel-rust-iban Alpha Laravel for Rust

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.

ErrorWhenMessage
InputTooLongThe input is longer than 1024 bytesIBAN input exceeds 1024 bytes
NotMachineFormatStrict parsing found a byte other than A–Z or 0–9IBAN must use electronic format
InvalidLengthFewer than 5 or more than 34 characters, or not the country's IBAN lengthIBAN input has an invalid length
InvalidStructureThe first two characters are not letters, the check digits are not digits, or the BBAN does not fit the country's structureIBAN input has an invalid structure
UnknownCountryThe prefix is not in the registryIBAN country is unsupported
NonCanonicalCheckDigitsThe check digits are 00, 01 or 99IBAN check digits must be between 02 and 98
InvalidCheckDigitsThe MOD 97-10 check failsIBAN 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