pub struct Bic { /* private fields */ }Expand description
A validated Business Identifier Code (ISO 9362).
A Bic can only be created by Bic::parse (or the explicitly unchecked
Bic::from_bytes_unchecked), so a value of this type is a proof that 8
or 11 characters form a structurally valid BIC. It stores the identifier
inline as [u8; 11] with a len field; the unused tail bytes of an
8-character BIC are zeroed, so the derived PartialEq/Eq/Hash compare
only the significant characters. It is Copy and allocates nothing.
§Examples
use regit_identifiers::Bic;
let bic = Bic::parse("DEUTDEFF500").unwrap();
assert_eq!(bic.institution(), "DEUT");
assert_eq!(bic.country_code(), "DE");
assert_eq!(bic.location_code(), "FF");
assert_eq!(bic.branch_code(), Some("500"));
assert_eq!(bic.as_str(), "DEUTDEFF500");Implementations§
Source§impl Bic
impl Bic
Sourcepub const SHORT_LENGTH: usize = 8
pub const SHORT_LENGTH: usize = 8
The length of a BIC without a branch code.
Sourcepub const MAX_LENGTH: usize = 11
pub const MAX_LENGTH: usize = 11
The length of a BIC with an explicit branch code.
Sourcepub fn parse(s: &str) -> Result<Self, ValidationError>
pub fn parse(s: &str) -> Result<Self, ValidationError>
Parses and validates a BIC.
Validation is strict and structural — a BIC carries no check digit. In order: the input must be exactly 8 or 11 characters; the institution and country segments (characters 1–6) must each be an upper-case ASCII letter; the location and branch segments must each be an ASCII digit or upper-case letter; and the country segment must be a recognised ISO 3166-1 alpha-2 code.
§Errors
ValidationError::Structurewith rule"BIC length must be 8 or 11"if the input is neither 8 nor 11 characters.ValidationError::InvalidCharacterif a character falls outside the set its position allows (this also rejects lower-case input and any non-ASCII character).ValidationError::InvalidCountryCodeif characters 5–6 are not a recognised ISO 3166-1 alpha-2 country code.
§Examples
use regit_identifiers::Bic;
use regit_identifiers::errors::ValidationError;
assert!(Bic::parse("DEUTDEFF").is_ok());
assert!(Bic::parse("DEUTDEFF500").is_ok());
// A 9-character string is neither permitted length.
assert_eq!(
Bic::parse("DEUTDEFF5"),
Err(ValidationError::Structure { rule: "BIC length must be 8 or 11" }),
);Sourcepub fn validate(s: &str) -> Result<(), ValidationError>
pub fn validate(s: &str) -> Result<(), ValidationError>
Validates a BIC without constructing one.
Equivalent to Bic::parse(s).map(|_| ()); use it when only the
verdict is needed.
§Errors
Returns the same ValidationError variants as Bic::parse.
§Examples
use regit_identifiers::Bic;
assert!(Bic::validate("CHASUS33").is_ok());
assert!(Bic::validate("CHASUS3").is_err());Sourcepub const fn from_bytes_unchecked(bytes: [u8; 11], len: u8) -> Self
pub const fn from_bytes_unchecked(bytes: [u8; 11], len: u8) -> Self
Wraps raw bytes as a Bic without any validation.
The caller asserts that the first len bytes hold the characters of a
valid BIC, that len is 8 or 11, and that every byte from len
onwards is zero. This exists for reconstructing a Bic from bytes
that were validated earlier; prefer Bic::parse for any untrusted
input.
§Examples
use regit_identifiers::Bic;
// The 3 trailing zero bytes are REQUIRED, not arbitrary padding —
// the derived `PartialEq`/`Eq`/`Hash` compare the full 11-byte buffer.
let bic = Bic::from_bytes_unchecked(*b"DEUTDEFF\0\0\0", 8);
assert_eq!(bic.as_str(), "DEUTDEFF");Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of characters in this BIC — 8 or 11.
§Examples
use regit_identifiers::Bic;
assert_eq!(Bic::parse("DEUTDEFF").unwrap().len(), 8);
assert_eq!(Bic::parse("DEUTDEFF500").unwrap().len(), 11);Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns the BIC as a string slice.
§Examples
use regit_identifiers::Bic;
assert_eq!(Bic::parse("DEUTDEFF500").unwrap().as_str(), "DEUTDEFF500");Sourcepub fn as_bytes(&self) -> &[u8]
pub fn as_bytes(&self) -> &[u8]
Returns the BIC as its raw ASCII bytes — 8 or 11 bytes, with no trailing zero padding.
§Examples
use regit_identifiers::Bic;
assert_eq!(Bic::parse("DEUTDEFF").unwrap().as_bytes(), b"DEUTDEFF");Sourcepub fn institution(&self) -> &str
pub fn institution(&self) -> &str
Returns the four-character institution code, characters 1–4.
§Examples
use regit_identifiers::Bic;
assert_eq!(Bic::parse("DEUTDEFF").unwrap().institution(), "DEUT");Sourcepub fn country_code(&self) -> &str
pub fn country_code(&self) -> &str
Returns the two-character ISO 3166-1 country code, characters 5–6.
§Examples
use regit_identifiers::Bic;
assert_eq!(Bic::parse("DEUTDEFF").unwrap().country_code(), "DE");Sourcepub fn location_code(&self) -> &str
pub fn location_code(&self) -> &str
Returns the two-character location code, characters 7–8.
§Examples
use regit_identifiers::Bic;
assert_eq!(Bic::parse("DEUTDEFF").unwrap().location_code(), "FF");Sourcepub fn branch_code(&self) -> Option<&str>
pub fn branch_code(&self) -> Option<&str>
Returns the three-character branch code, characters 9–11, or None
for an 8-character BIC with no branch suffix.
§Examples
use regit_identifiers::Bic;
assert_eq!(Bic::parse("DEUTDEFF500").unwrap().branch_code(), Some("500"));
assert_eq!(Bic::parse("DEUTDEFF").unwrap().branch_code(), None);Sourcepub fn has_branch(&self) -> bool
pub fn has_branch(&self) -> bool
Returns true if this BIC carries an explicit branch code, i.e. it is
11 characters long.
§Examples
use regit_identifiers::Bic;
assert!(Bic::parse("DEUTDEFF500").unwrap().has_branch());
assert!(!Bic::parse("DEUTDEFF").unwrap().has_branch());Sourcepub fn is_test_bic(&self) -> bool
pub fn is_test_bic(&self) -> bool
Returns true if this is a test/training BIC, i.e. the second
character of the location code (character 8) is '0'.
§Examples
use regit_identifiers::Bic;
assert!(Bic::parse("DEUTDEF0").unwrap().is_test_bic());
assert!(!Bic::parse("DEUTDEFF").unwrap().is_test_bic());Sourcepub fn is_passive(&self) -> bool
pub fn is_passive(&self) -> bool
Returns true if this BIC belongs to a passive SWIFT participant,
i.e. the second character of the location code (character 8) is '1'.
§Examples
use regit_identifiers::Bic;
assert!(Bic::parse("DEUTDEF1").unwrap().is_passive());
assert!(!Bic::parse("DEUTDEFF").unwrap().is_passive());Sourcepub fn location_status(&self) -> char
pub fn location_status(&self) -> char
Returns the second character of the location code (character 8 of the BIC) — the “status character” by SWIFT convention.
SWIFT attaches a convention to this character: '0' marks a test or
training BIC (Bic::is_test_bic) and '1' a passive participant
(Bic::is_passive). Any other value denotes a connected,
non-test, non-passive participant. Use this accessor when you want
the raw character; use the predicates when you only need a yes/no.
§Examples
use regit_identifiers::Bic;
assert_eq!(Bic::parse("DEUTDEFF").unwrap().location_status(), 'F');
assert_eq!(Bic::parse("DEUTDEF0").unwrap().location_status(), '0');
assert_eq!(Bic::parse("DEUTDEF1").unwrap().location_status(), '1');