Skip to main content

iban_check/
lib.rs

1//! A dependency-free IBAN (International Bank Account Number) validation library for Rust.
2//!
3//! This crate validates IBANs according to the ISO 13616 standard. It performs the following checks:
4//!
5//! - Verifies the country code is known and registered.
6//! - Verifies the IBAN length matches the country's specification.
7//! - Verifies all characters are valid alphanumeric characters.
8//! - Verifies the Mod-97-10 checksum.
9//!
10//! # Quick Start
11//!
12//! ```
13//! use iban_check::validate;
14//!
15//! // Valid German IBAN
16//! assert!(validate("DE89370400440532013000").is_ok());
17//!
18//! // With spaces (whitespace is ignored)
19//! assert!(validate("DE89 3704 0044 0532 0130 00").is_ok());
20//!
21//! // Invalid IBAN (bad checksum)
22//! assert!(validate("DE89370400440532013001").is_err());
23//! ```
24//!
25//! # Using the `Iban` Type
26//!
27//! For a type-safe way to represent validated IBANs:
28//!
29//! ```
30//! use iban_check::Iban;
31//!
32//! let iban = Iban::new("GB82WEST12345698765432").unwrap();
33//! assert_eq!(iban.country_code(), "GB");
34//! assert_eq!(iban.check_digits(), "82");
35//! assert_eq!(iban.bban(), "WEST12345698765432");
36//! ```
37//!
38//! # Supported Countries
39//!
40//! This library supports all IBAN countries as of 2024, including:
41//! Germany (DE), United Kingdom (GB), France (FR), Switzerland (CH),
42//! Netherlands (NL), Belgium (BE), Austria (AT), and many more (73 countries total).
43//!
44//! # Error Handling
45//!
46//! Validation returns structured errors so you know exactly why an IBAN is invalid:
47//!
48//! ```
49//! use iban_check::{validate, ValidationError};
50//!
51//! match validate("XX89INVALID") {
52//!     Err(ValidationError::InvalidCountryCode) => println!("Unknown country!"),
53//!     Err(ValidationError::InvalidLength { expected, found }) => {
54//!         println!("Wrong length: expected {}, found {}", expected, found)
55//!     }
56//!     Err(ValidationError::InvalidChecksum) => println!("Bad checksum!"),
57//!     Ok(()) => println!("Valid IBAN"),
58//!     _ => {}
59//! }
60//! ```
61
62pub mod error;
63pub mod validator;
64
65// Private module for country data
66mod countries;
67
68// Re-export public API
69pub use error::ValidationError;
70pub use validator::Iban;
71pub use validator::validate;