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
impl Mic
Sourcepub fn parse(s: &str) -> Result<Self, ValidationError>
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
ValidationError::WrongLengthif the input is not 4 characters.ValidationError::InvalidCharacterif a character falls outside the set its position allows (this also rejects lower-case input and any non-ASCII character).
§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' }),
);Sourcepub fn validate(s: &str) -> Result<(), ValidationError>
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());Sourcepub fn parse_registered(s: &str) -> Result<Self, ValidationError>
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
- The same
ValidationErrorvariants asMic::parsefor a structurally invalid input. ValidationError::Structurewithrule: "MIC is not in the ISO 10383 registry"if the code is well-formed but absent from the embedded registry.
§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",
}),
);Sourcepub const fn from_bytes_unchecked(bytes: [u8; 4]) -> Self
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");Sourcepub fn as_str(&self) -> &str
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");Sourcepub fn as_bytes(&self) -> &[u8]
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");Sourcepub fn prefix(&self) -> char
pub fn prefix(&self) -> char
Returns the leading character, character 1.
§Examples
use regit_identifiers::Mic;
assert_eq!(Mic::parse("XNAS").unwrap().prefix(), 'X');Sourcepub fn suffix(&self) -> &str
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");Sourcepub fn lookup(&self) -> Option<&'static MicEntry>
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());Sourcepub fn is_registered(&self) -> bool
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());