Skip to main content

Cusip

Struct Cusip 

Source
pub struct Cusip { /* private fields */ }
Expand description

A validated CUSIP (or CINS) number (ANSI X9.6).

A Cusip can only be created by Cusip::parse (or the explicitly unchecked Cusip::from_bytes_unchecked), so a value of this type is a proof that the 9 characters form a structurally valid CUSIP with a correct check digit. It stores the identifier inline as [u8; 9], is Copy, and allocates nothing.

§Examples

use regit_identifiers::Cusip;

let cusip = Cusip::parse("037833100").unwrap();
assert_eq!(cusip.issuer(), "037833");
assert_eq!(cusip.issue(), "10");
assert_eq!(cusip.check_digit(), '0');
assert_eq!(cusip.as_str(), "037833100");

Implementations§

Source§

impl Cusip

Source

pub const LENGTH: usize = 9

The number of characters in a CUSIP.

Source

pub fn parse(s: &str) -> Result<Self, ValidationError>

Parses and fully validates a CUSIP.

Validation is strict and, in order: the input must be exactly 9 characters; characters 1–8 must each be drawn from the body alphabet [A-Z0-9*@#] and character 9 must be an ASCII digit; and the check digit must equal the value recomputed from the eight-character body.

§Errors
§Examples
use regit_identifiers::Cusip;
use regit_identifiers::errors::ValidationError;

assert!(Cusip::parse("037833100").is_ok());

// A single wrong digit is caught, not silently accepted.
assert_eq!(
    Cusip::parse("037833101"),
    Err(ValidationError::BadCheckDigit { expected: '0', found: '1' }),
);
Source

pub fn validate(s: &str) -> Result<(), ValidationError>

Validates a CUSIP without constructing one.

Equivalent to Cusip::parse(s).map(|_| ()); use it when only the verdict is needed.

§Errors

Returns the same ValidationError variants as Cusip::parse.

§Examples
use regit_identifiers::Cusip;

assert!(Cusip::validate("037833100").is_ok());
assert!(Cusip::validate("037833101").is_err());
Source

pub const fn from_bytes_unchecked(bytes: [u8; 9]) -> Self

Wraps 9 raw bytes as a Cusip without any validation.

The caller asserts that bytes holds the 9 ASCII characters of a valid CUSIP. This exists for reconstructing a Cusip from bytes that were validated earlier; prefer Cusip::parse for any untrusted input.

§Examples
use regit_identifiers::Cusip;

let cusip = Cusip::from_bytes_unchecked(*b"037833100");
assert_eq!(cusip.as_str(), "037833100");
Source

pub fn as_str(&self) -> &str

Returns the CUSIP as a string slice.

§Examples
use regit_identifiers::Cusip;

assert_eq!(Cusip::parse("037833100").unwrap().as_str(), "037833100");
Source

pub fn as_bytes(&self) -> &[u8]

Returns the CUSIP as its 9 raw ASCII bytes.

§Examples
use regit_identifiers::Cusip;

assert_eq!(Cusip::parse("037833100").unwrap().as_bytes(), b"037833100");
Source

pub fn issuer(&self) -> &str

Returns the six-character issuer segment, characters 1–6.

§Examples
use regit_identifiers::Cusip;

assert_eq!(Cusip::parse("037833100").unwrap().issuer(), "037833");
Source

pub fn issue(&self) -> &str

Returns the two-character issue segment, characters 7–8.

§Examples
use regit_identifiers::Cusip;

assert_eq!(Cusip::parse("037833100").unwrap().issue(), "10");
Source

pub fn check_digit(&self) -> char

Returns the check digit, character 9.

§Examples
use regit_identifiers::Cusip;

assert_eq!(Cusip::parse("037833100").unwrap().check_digit(), '0');
Source

pub fn is_cins(&self) -> bool

Returns true if this identifier is a CINS number.

A CINS (CUSIP International Numbering System) number is structurally a CUSIP — same length, same alphabet, same check-digit algorithm — and is distinguished solely by its first character being a letter, where a domestic CUSIP always starts with a digit.

§Examples
use regit_identifiers::Cusip;

// A domestic CUSIP starts with a digit.
assert!(!Cusip::parse("037833100").unwrap().is_cins());
Source

pub fn is_domestic(&self) -> bool

Returns true if this is a domestic (US/Canada) CUSIP — the complement of Cusip::is_cins.

The discrimination rule is the leading character: a domestic CUSIP starts with a digit, a CINS with a letter.

§Examples
use regit_identifiers::Cusip;

assert!(Cusip::parse("037833100").unwrap().is_domestic());
Source

pub fn cins_region(&self) -> Option<&'static str>

Returns the CINS issuing region of this identifier, if it is a CINS.

The leading letter of a CINS number designates its issuing region per the CINS table; for a domestic CUSIP (which starts with a digit) this returns None. A leading letter outside the assigned table likewise returns None.

§Examples
use regit_identifiers::Cusip;

// A domestic CUSIP has no CINS region.
assert_eq!(Cusip::parse("037833100").unwrap().cins_region(), None);

Trait Implementations§

Source§

impl AsRef<str> for Cusip

Source§

fn as_ref(&self) -> &str

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

impl Clone for Cusip

Source§

fn clone(&self) -> Cusip

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 Cusip

Source§

impl Debug for Cusip

Source§

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

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

impl Display for Cusip

Source§

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

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

impl Eq for Cusip

Source§

impl FromStr for Cusip

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 Cusip

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 Cusip

Source§

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

Auto Trait Implementations§

§

impl Freeze for Cusip

§

impl RefUnwindSafe for Cusip

§

impl Send for Cusip

§

impl Sync for Cusip

§

impl Unpin for Cusip

§

impl UnsafeUnpin for Cusip

§

impl UnwindSafe for Cusip

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.