pub struct Lei { /* private fields */ }Expand description
A validated Legal Entity Identifier (ISO 17442).
A Lei can only be created by Lei::parse (or the explicitly unchecked
Lei::from_bytes_unchecked), so a value of this type is a proof that the
20 characters form a structurally valid LEI with correct check digits. It
stores the identifier inline as [u8; 20], is Copy, and allocates
nothing.
§Examples
use regit_identifiers::Lei;
let lei = Lei::parse("5493001KJTIIGC8Y1R12").unwrap();
assert_eq!(lei.lou_prefix(), "5493");
assert_eq!(lei.entity_id(), "1KJTIIGC8Y1R");
assert_eq!(lei.check_digits(), "12");
assert_eq!(lei.as_str(), "5493001KJTIIGC8Y1R12");Implementations§
Source§impl Lei
impl Lei
Sourcepub fn parse(s: &str) -> Result<Self, ValidationError>
pub fn parse(s: &str) -> Result<Self, ValidationError>
Parses and fully validates an LEI.
Validation is strict and, in order: the input must be exactly 20
characters; characters 1–18 must each be an ASCII digit or upper-case
letter and characters 19–20 ASCII digits; characters 5–6 must be the
reserved literal 00; and the two check digits must equal the values
recomputed from the 18-character body.
§Errors
ValidationError::WrongLengthif the input is not 20 characters.ValidationError::InvalidCharacterif a character falls outside the set its position allows (this also rejects lower-case input and any non-ASCII character).ValidationError::Structureif characters 5–6 are not00.ValidationError::BadCheckDigitif a supplied check digit does not match the recomputed one; the first differing digit is reported.
§Examples
use regit_identifiers::Lei;
use regit_identifiers::errors::ValidationError;
assert!(Lei::parse("5493001KJTIIGC8Y1R12").is_ok());
// A single wrong check digit is caught, not silently accepted.
assert_eq!(
Lei::parse("5493001KJTIIGC8Y1R13"),
Err(ValidationError::BadCheckDigit { expected: '2', found: '3' }),
);Sourcepub fn validate(s: &str) -> Result<(), ValidationError>
pub fn validate(s: &str) -> Result<(), ValidationError>
Validates an LEI without constructing one.
Equivalent to Lei::parse(s).map(|_| ()); use it when only the verdict
is needed.
§Errors
Returns the same ValidationError variants as Lei::parse.
§Examples
use regit_identifiers::Lei;
assert!(Lei::validate("5493001KJTIIGC8Y1R12").is_ok());
assert!(Lei::validate("5493001KJTIIGC8Y1R13").is_err());Sourcepub const fn from_bytes_unchecked(bytes: [u8; 20]) -> Self
pub const fn from_bytes_unchecked(bytes: [u8; 20]) -> Self
Wraps 20 raw bytes as a Lei without any validation.
The caller asserts that bytes holds the 20 ASCII characters of a
valid LEI. This exists for reconstructing a Lei from bytes that were
validated earlier; prefer Lei::parse for any untrusted input.
§Examples
use regit_identifiers::Lei;
let lei = Lei::from_bytes_unchecked(*b"5493001KJTIIGC8Y1R12");
assert_eq!(lei.as_str(), "5493001KJTIIGC8Y1R12");Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns the LEI as a string slice.
§Examples
use regit_identifiers::Lei;
assert_eq!(
Lei::parse("5493001KJTIIGC8Y1R12").unwrap().as_str(),
"5493001KJTIIGC8Y1R12",
);Sourcepub fn as_bytes(&self) -> &[u8]
pub fn as_bytes(&self) -> &[u8]
Returns the LEI as its 20 raw ASCII bytes.
§Examples
use regit_identifiers::Lei;
assert_eq!(
Lei::parse("5493001KJTIIGC8Y1R12").unwrap().as_bytes(),
b"5493001KJTIIGC8Y1R12",
);Sourcepub fn lou_prefix(&self) -> &str
pub fn lou_prefix(&self) -> &str
Returns the four-character LOU prefix, characters 1–4.
§Examples
use regit_identifiers::Lei;
assert_eq!(
Lei::parse("5493001KJTIIGC8Y1R12").unwrap().lou_prefix(),
"5493",
);Sourcepub fn entity_id(&self) -> &str
pub fn entity_id(&self) -> &str
Returns the twelve-character entity ID, characters 7–18 (the segment ISO 17442 calls the entity-specific part).
§Examples
use regit_identifiers::Lei;
assert_eq!(
Lei::parse("5493001KJTIIGC8Y1R12").unwrap().entity_id(),
"1KJTIIGC8Y1R",
);Sourcepub fn check_digits(&self) -> &str
pub fn check_digits(&self) -> &str
Returns the two check digits, characters 19–20.
§Examples
use regit_identifiers::Lei;
assert_eq!(
Lei::parse("5493001KJTIIGC8Y1R12").unwrap().check_digits(),
"12",
);