Skip to main content

Mic

Struct Mic 

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

A validated Market Identifier Code (ISO 10383).

A Mic can only be created by Mic::parse (or the explicitly unchecked Mic::from_bytes_unchecked), so a value of this type is a proof that the four characters are structurally a valid MIC: a leading upper-case letter followed by three upper-case alphanumeric characters. It stores the identifier inline as [u8; 4], is Copy, and allocates nothing.

Structural validity does not imply the market exists; use Mic::is_registered or Mic::parse_registered to additionally check the embedded ISO 10383 registry.

§Examples

use regit_identifiers::Mic;

let mic = Mic::parse("XNAS").unwrap();
assert_eq!(mic.as_str(), "XNAS");
assert_eq!(mic.suffix(), "NAS");

Implementations§

Source§

impl Mic

Source

pub const LENGTH: usize = 4

The number of characters in a MIC.

Source

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

Parses and validates a MIC.

Validation is strict and, in order: the input must be exactly 4 characters; the first character must be an ASCII upper-case letter; and each of the remaining three characters must be an ASCII digit or upper-case letter. A MIC has no check digit, so there is nothing further to verify.

This checks structure only — it does not consult the ISO 10383 registry. Use Mic::parse_registered to additionally require that the code names a real market.

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

assert!(Mic::parse("XLON").is_ok());

// The leading character must be a letter, not a digit.
assert_eq!(
    Mic::parse("1NAS"),
    Err(ValidationError::InvalidCharacter { position: 1, found: '1' }),
);
Source

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

Validates a MIC without constructing one.

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

§Errors

Returns the same ValidationError variants as Mic::parse.

§Examples
use regit_identifiers::Mic;

assert!(Mic::validate("XPAR").is_ok());
assert!(Mic::validate("xpar").is_err());
Source

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

Parses a MIC and requires it to be in the ISO 10383 registry.

First applies the structural validation of Mic::parse, then looks the code up in the embedded ISO 10383 snapshot. A well-formed but unregistered code such as ZZZZ is rejected here even though Mic::parse would accept it.

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

// XNYS is a real, registered market.
assert!(Mic::parse_registered("XNYS").is_ok());

// ZZZZ is well-formed but identifies no market.
assert_eq!(
    Mic::parse_registered("ZZZZ"),
    Err(ValidationError::Structure {
        rule: "MIC is not in the ISO 10383 registry",
    }),
);
Source

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

Wraps 4 raw bytes as a Mic without any validation.

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

§Examples
use regit_identifiers::Mic;

let mic = Mic::from_bytes_unchecked(*b"XNAS");
assert_eq!(mic.as_str(), "XNAS");
Source

pub fn as_str(&self) -> &str

Returns the MIC as a string slice.

§Examples
use regit_identifiers::Mic;

assert_eq!(Mic::parse("XNAS").unwrap().as_str(), "XNAS");
Source

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

Returns the MIC as its 4 raw ASCII bytes.

§Examples
use regit_identifiers::Mic;

assert_eq!(Mic::parse("XNAS").unwrap().as_bytes(), b"XNAS");
Source

pub fn prefix(&self) -> char

Returns the leading character, character 1.

§Examples
use regit_identifiers::Mic;

assert_eq!(Mic::parse("XNAS").unwrap().prefix(), 'X');
Source

pub fn suffix(&self) -> &str

Returns the three-character market suffix, characters 2–4.

§Examples
use regit_identifiers::Mic;

assert_eq!(Mic::parse("XNAS").unwrap().suffix(), "NAS");
Source

pub fn lookup(&self) -> Option<&'static MicEntry>

Looks the MIC up in the embedded ISO 10383 registry.

Returns the MicEntry describing the market — its operating MIC, name, country, city, and status — or None if the code is not in the snapshot. Delegates to crate::mic_registry::lookup.

§Examples
use regit_identifiers::Mic;

let mic = Mic::parse("XNAS").unwrap();
let entry = mic.lookup().expect("XNAS is registered");
assert_eq!(entry.mic, "XNAS");

// A well-formed but unregistered code has no entry.
assert!(Mic::parse("ZZZZ").unwrap().lookup().is_none());
Source

pub fn is_registered(&self) -> bool

Returns true if the MIC is present in the embedded ISO 10383 registry.

Equivalent to self.lookup().is_some().

§Examples
use regit_identifiers::Mic;

assert!(Mic::parse("XLON").unwrap().is_registered());
assert!(!Mic::parse("ZZZZ").unwrap().is_registered());

Trait Implementations§

Source§

impl AsRef<str> for Mic

Source§

fn as_ref(&self) -> &str

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

impl Clone for Mic

Source§

fn clone(&self) -> Mic

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 Mic

Source§

impl Debug for Mic

Source§

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

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

impl Display for Mic

Source§

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

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

impl Eq for Mic

Source§

impl FromStr for Mic

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 Mic

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 Mic

Source§

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

Auto Trait Implementations§

§

impl Freeze for Mic

§

impl RefUnwindSafe for Mic

§

impl Send for Mic

§

impl Sync for Mic

§

impl Unpin for Mic

§

impl UnsafeUnpin for Mic

§

impl UnwindSafe for Mic

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.