Skip to main content

Isin

Struct Isin 

Source
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

Source

pub const LENGTH: usize = 12

The number of characters in an ISIN.

Source

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

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

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

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

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

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

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

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

Trait Implementations§

Source§

impl AsRef<str> for Isin

Source§

fn as_ref(&self) -> &str

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

impl Clone for Isin

Source§

fn clone(&self) -> Isin

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 Isin

Source§

impl Debug for Isin

Source§

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

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

impl Display for Isin

Source§

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

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

impl Eq for Isin

Source§

impl FromStr for Isin

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 Isin

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 Isin

Source§

fn eq(&self, other: &Isin) -> 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 Isin

Auto Trait Implementations§

§

impl Freeze for Isin

§

impl RefUnwindSafe for Isin

§

impl Send for Isin

§

impl Sync for Isin

§

impl Unpin for Isin

§

impl UnsafeUnpin for Isin

§

impl UnwindSafe for Isin

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.