pub struct Isin { /* private fields */ }Expand description
A validated International Securities Identification Number (ISO 6166).
An Isin can only be created by Isin::parse (or the explicitly
unchecked Isin::from_bytes_unchecked), so a value of this type is a
proof that the 12 characters form a structurally valid ISIN with a
correct check digit. It stores the identifier inline as [u8; 12], is
Copy, and allocates nothing.
§Examples
use regit_identifiers::Isin;
let isin = Isin::parse("US0378331005").unwrap();
assert_eq!(isin.country_code(), "US");
assert_eq!(isin.nsin(), "037833100");
assert_eq!(isin.check_digit(), '5');
assert_eq!(isin.as_str(), "US0378331005");Implementations§
Source§impl Isin
impl Isin
Sourcepub fn parse(s: &str) -> Result<Self, ValidationError>
pub fn parse(s: &str) -> Result<Self, ValidationError>
Parses and fully validates an ISIN.
Validation is strict and, in order: the input must be exactly 12 characters; characters 1–11 must each be an ASCII digit or upper-case letter and character 12 an ASCII digit; the first two characters must be a recognised ISIN country prefix; and the check digit must equal the value recomputed from the 11-character body.
§Errors
ValidationError::WrongLengthif the input is not 12 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 the first two characters are not a recognised ISIN country prefix.ValidationError::BadCheckDigitif the supplied check digit does not match the recomputed one.
§Examples
use regit_identifiers::Isin;
use regit_identifiers::errors::ValidationError;
assert!(Isin::parse("US0378331005").is_ok());
// A single wrong digit is caught, not silently accepted.
assert_eq!(
Isin::parse("US0378331004"),
Err(ValidationError::BadCheckDigit { expected: '5', found: '4' }),
);Sourcepub fn validate(s: &str) -> Result<(), ValidationError>
pub fn validate(s: &str) -> Result<(), ValidationError>
Validates an ISIN without constructing one.
Equivalent to Isin::parse(s).map(|_| ()); use it when only the
verdict is needed.
§Errors
Returns the same ValidationError variants as Isin::parse.
§Examples
use regit_identifiers::Isin;
assert!(Isin::validate("US0378331005").is_ok());
assert!(Isin::validate("US0378331004").is_err());Sourcepub const fn from_bytes_unchecked(bytes: [u8; 12]) -> Self
pub const fn from_bytes_unchecked(bytes: [u8; 12]) -> Self
Wraps 12 raw bytes as an Isin without any validation.
The caller asserts that bytes holds the 12 ASCII characters of a
valid ISIN. This exists for reconstructing an Isin from bytes that
were validated earlier; prefer Isin::parse for any untrusted
input.
§Examples
use regit_identifiers::Isin;
let isin = Isin::from_bytes_unchecked(*b"US0378331005");
assert_eq!(isin.as_str(), "US0378331005");Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns the ISIN as a string slice.
§Examples
use regit_identifiers::Isin;
assert_eq!(Isin::parse("US0378331005").unwrap().as_str(), "US0378331005");Sourcepub fn as_bytes(&self) -> &[u8]
pub fn as_bytes(&self) -> &[u8]
Returns the ISIN as its 12 raw ASCII bytes.
§Examples
use regit_identifiers::Isin;
assert_eq!(Isin::parse("US0378331005").unwrap().as_bytes(), b"US0378331005");Sourcepub fn country_code(&self) -> &str
pub fn country_code(&self) -> &str
Returns the two-character country prefix, characters 1–2.
§Examples
use regit_identifiers::Isin;
assert_eq!(Isin::parse("US0378331005").unwrap().country_code(), "US");Sourcepub fn nsin(&self) -> &str
pub fn nsin(&self) -> &str
Returns the nine-character NSIN, characters 3–11.
§Examples
use regit_identifiers::Isin;
assert_eq!(Isin::parse("US0378331005").unwrap().nsin(), "037833100");Sourcepub fn check_digit(&self) -> char
pub fn check_digit(&self) -> char
Returns the check digit, character 12.
§Examples
use regit_identifiers::Isin;
assert_eq!(Isin::parse("US0378331005").unwrap().check_digit(), '5');