Skip to main content

Bic

Struct Bic 

Source
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

Source

pub const SHORT_LENGTH: usize = 8

The length of a BIC without a branch code.

Source

pub const MAX_LENGTH: usize = 11

The length of a BIC with an explicit branch code.

Source

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
§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" }),
);
Source

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());
Source

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");
Source

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);
Source

pub fn is_empty(&self) -> bool

Always false — a valid BIC is never empty (it is 8 or 11 characters). Provided for API consistency with Bic::len.

§Examples
use regit_identifiers::Bic;

assert!(!Bic::parse("DEUTDEFF").unwrap().is_empty());
Source

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");
Source

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");
Source

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");
Source

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");
Source

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");
Source

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);
Source

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());
Source

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());
Source

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());
Source

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');

Trait Implementations§

Source§

impl AsRef<str> for Bic

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Bic

Source§

fn clone(&self) -> Bic

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Bic

Source§

impl Debug for Bic

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Bic

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Bic

Source§

impl FromStr for Bic

Source§

type Err = ValidationError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Bic

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Bic

Source§

fn eq(&self, other: &Bic) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Bic

Auto Trait Implementations§

§

impl Freeze for Bic

§

impl RefUnwindSafe for Bic

§

impl Send for Bic

§

impl Sync for Bic

§

impl Unpin for Bic

§

impl UnsafeUnpin for Bic

§

impl UnwindSafe for Bic

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.