# iban-validator
A dependency-free Rust library for validating International Bank Account Numbers (IBAN) according to ISO 13616.
## Features
- **Zero dependencies**: No external crates required.
- **Fast and lightweight**: Optimized for performance with zero allocations on the fast path.
- **Comprehensive**: Supports all 73 registered IBAN countries.
- **Type-safe**: Optional `Iban` type guarantees validity at compile time.
- **Robust error handling**: Know exactly why an IBAN is invalid.
## Installation
Add this to your `Cargo.toml`:
```toml
[dependencies]
iban-validator = "0.1.0"
```
## Usage
### Basic Validation
```rust
use iban_validator::validate;
// Valid German IBAN
assert!(validate("DE89370400440532013000").is_ok());
// With spaces (whitespace is ignored)
assert!(validate("DE89 3704 0044 0532 0130 00").is_ok());
// Invalid IBAN (bad checksum)
assert!(validate("DE89370400440532013001").is_err());
```
### Using the `Iban` Type
```rust
use iban_validator::Iban;
let iban = Iban::new("GB82WEST12345698765432").unwrap();
assert_eq!(iban.country_code(), "GB");
assert_eq!(iban.check_digits(), "82");
assert_eq!(iban.bban(), "WEST12345698765432");
```
### Error Handling
```rust
use iban_validator::{validate, ValidationError};
match validate("XX89INVALID") {
Err(ValidationError::InvalidCountryCode) => println!("Unknown country!"),
Err(ValidationError::InvalidLength { expected, found }) => {
println!("Wrong length: expected {}, found {}", expected, found)
}
Err(ValidationError::InvalidChecksum) => println!("Bad checksum!"),
Ok(()) => println!("Valid IBAN"),
_ => {}
}
```
## Supported Countries
All IBAN countries as of 2024 (73 total), including:
- Germany (DE), United Kingdom (GB), France (FR), Switzerland (CH)
- Netherlands (NL), Belgium (BE), Austria (AT), Ireland (IE)
- And many more across Europe, Middle East, Africa, and the Americas.
## Validation Logic
1. **Sanitize**: Remove all whitespace characters.
2. **Country Check**: Verify the country code is known.
3. **Length Check**: Ensure the IBAN matches the expected length for the country.
4. **Character Check**: Ensure all characters are alphanumeric.
5. **Checksum**: Verify the Mod-97-10 checksum (move first 4 chars to end, convert letters to numbers, divide by 97, check remainder == 1).
## License
This project is licensed under either of:
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE))
- MIT license ([LICENSE-MIT](LICENSE-MIT))
at your option.