iso10383_types/
lib.rs

1//! ISO 10383 Types
2
3#![doc = include_str!("../README.md")]
4#![no_std]
5
6#[cfg(feature = "alloc")]
7extern crate alloc;
8
9#[cfg(feature = "alloc")]
10mod alloc_;
11
12#[cfg(feature = "serde")]
13mod serde_;
14
15use core::{
16    borrow::Borrow,
17    fmt::{Display, Error as FmtError, Formatter, Result as FmtResult},
18    ops::Deref,
19    str::FromStr,
20};
21use ref_cast::{RefCastCustom, ref_cast_custom};
22use thiserror::Error as ThisError;
23
24#[cfg(feature = "serde")]
25use ::serde::{Deserialize, Serialize};
26
27const MIC_SIZE: usize = 4;
28
29const fn check_mic(bytes: &[u8]) -> Result<(), Error> {
30    if bytes.len() != MIC_SIZE {
31        return Err(Error::InvalidLength(bytes.len(), MIC_SIZE));
32    }
33
34    let mut i = 0;
35    while i < MIC_SIZE {
36        if !bytes[i].is_ascii_digit() && !bytes[i].is_ascii_uppercase() {
37            return Err(Error::InvalidCharacter(i));
38        }
39
40        i += 1;
41    }
42
43    Ok(())
44}
45
46/// An enumeration of errors when validating a MIC
47#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, ThisError)]
48#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
49pub enum Error {
50    /// Invalid length.
51    #[error("Invalid length.")]
52    InvalidLength(usize, usize),
53    /// Invalid character at position {0}.
54    #[error("Invalid character at position {0}.")]
55    InvalidCharacter(usize),
56}
57
58/// A MIC reference
59#[derive(Debug, Eq, Hash, Ord, PartialEq, PartialOrd, RefCastCustom)]
60#[allow(non_camel_case_types)]
61#[repr(transparent)]
62pub struct mic([u8]);
63
64impl mic {
65    #[ref_cast_custom]
66    pub(crate) const fn from_bytes_unchecked(src: &[u8]) -> &Self;
67
68    /// Create a new MIC from the given bytes
69    pub const fn from_bytes(src: &[u8]) -> Result<&Self, Error> {
70        if let Err(e) = check_mic(src) {
71            Err(e)
72        } else {
73            Ok(Self::from_bytes_unchecked(src))
74        }
75    }
76
77    /// Create a new MIC from the given string
78    pub const fn from_str(src: &str) -> Result<&Self, Error> {
79        let bytes = src.as_bytes();
80
81        if let Err(e) = check_mic(bytes) {
82            Err(e)
83        } else {
84            Ok(Self::from_bytes_unchecked(bytes))
85        }
86    }
87
88    /// Borrow this MIC as a byte slice.
89    pub const fn as_bytes(&self) -> &[u8] {
90        &self.0
91    }
92
93    /// Borrow this MIC as a string slice.
94    #[allow(unsafe_code)]
95    pub const fn as_str(&self) -> &str {
96        // SAFETY: a mic slice is validated before construction
97        unsafe { str::from_utf8_unchecked(&self.0) }
98    }
99
100    /// Get an owned copy of this MIC.
101    pub const fn to_mic(&self) -> Mic {
102        Mic::from_bytes_unchecked(&self.0)
103    }
104}
105
106impl AsRef<[u8]> for mic {
107    fn as_ref(&self) -> &[u8] {
108        self.as_bytes()
109    }
110}
111
112impl AsRef<str> for mic {
113    fn as_ref(&self) -> &str {
114        self.as_str()
115    }
116}
117
118impl Display for mic {
119    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
120        write!(f, "{}", str::from_utf8(&self.0).map_err(|_e| FmtError)?)
121    }
122}
123
124/// An owned MIC value
125#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, RefCastCustom)]
126#[repr(transparent)]
127pub struct Mic([u8; MIC_SIZE]);
128
129impl Mic {
130    pub(crate) const fn from_byte_array_unchecked(bytes: [u8; MIC_SIZE]) -> Self {
131        Self(bytes)
132    }
133
134    pub(crate) const fn from_bytes_unchecked(src: &[u8]) -> Self {
135        let mut bytes = [0u8; MIC_SIZE];
136        let (value, _reject) = src.split_at(MIC_SIZE);
137        bytes.copy_from_slice(value);
138        Self::from_byte_array_unchecked(bytes)
139    }
140
141    /// Create a new MIC by taking ownership of a byte array.
142    pub const fn from_byte_array(bytes: [u8; MIC_SIZE]) -> Result<Self, Error> {
143        if let Err(e) = check_mic(&bytes) {
144            Err(e)
145        } else {
146            Ok(Self::from_byte_array_unchecked(bytes))
147        }
148    }
149
150    /// Create a new owned MIC from the given byte slice.
151    pub const fn from_bytes(src: &[u8]) -> Result<Self, Error> {
152        if let Err(e) = check_mic(src) {
153            Err(e)
154        } else {
155            Ok(Self::from_bytes_unchecked(src))
156        }
157    }
158
159    /// Create a new owned MIC from the given string slice.
160    pub const fn from_str_slice(s: &str) -> Result<Self, Error> {
161        Self::from_bytes(s.as_bytes())
162    }
163
164    /// Borrow this MIC as a byte slice.
165    pub const fn as_bytes(&self) -> &[u8] {
166        &self.0
167    }
168
169    /// Borrow this MIC as a string slice.
170    #[allow(unsafe_code)]
171    pub const fn as_str(&self) -> &str {
172        // SAFETY: We validate the internal byte array contains only ASCII digits and uppercase
173        // characters on construction.
174        unsafe { str::from_utf8_unchecked(&self.0) }
175    }
176
177    /// Borrow this MIC as a MIC slice.
178    pub const fn as_mic(&self) -> &mic {
179        mic::from_bytes_unchecked(&self.0)
180    }
181}
182
183impl AsRef<[u8]> for Mic {
184    fn as_ref(&self) -> &[u8] {
185        self.as_bytes()
186    }
187}
188
189impl AsRef<str> for Mic {
190    fn as_ref(&self) -> &str {
191        self.as_str()
192    }
193}
194
195impl Deref for Mic {
196    type Target = mic;
197
198    fn deref(&self) -> &Self::Target {
199        mic::from_bytes_unchecked(&self.0)
200    }
201}
202
203impl Borrow<mic> for Mic {
204    fn borrow(&self) -> &mic {
205        mic::from_bytes_unchecked(&self.0)
206    }
207}
208
209impl Display for Mic {
210    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
211        write!(f, "{}", str::from_utf8(&self.0).map_err(|_e| FmtError)?)
212    }
213}
214
215impl FromStr for Mic {
216    type Err = Error;
217
218    fn from_str(s: &str) -> Result<Self, Self::Err> {
219        Self::from_str_slice(s)
220    }
221}
222
223/// The type of MIC
224#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
225#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
226pub enum Kind {
227    /// A top-level owner/operator organization
228    #[cfg_attr(feature = "serde", serde(alias = "OPRT"))]
229    Operating,
230    /// A market segment MIC subsidiary of an owner/operator MIC
231    #[cfg_attr(feature = "serde", serde(alias = "SGMT"))]
232    Segment,
233}
234
235/// The market category of a MIC
236#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
237#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
238pub enum Category {
239    /// ATSS - Alternative Trading System
240    #[cfg_attr(feature = "serde", serde(alias = "ATSS"))]
241    AlternativeTradingSystem,
242
243    /// APPA - Approved Publication Arrangement
244    #[cfg_attr(feature = "serde", serde(alias = "APPA"))]
245    ApprovedPublicationArrangement,
246
247    /// ARMS - Approved Reporting Mechanism
248    #[cfg_attr(feature = "serde", serde(alias = "ARMS"))]
249    ApprovedReportingMechanism,
250
251    /// CTPS - Consolidated Tape Provider
252    #[cfg_attr(feature = "serde", serde(alias = "CTPS"))]
253    ConsolidatedTapeProvider,
254
255    /// CASP - Crypto Asset Services Provider
256    #[cfg_attr(feature = "serde", serde(alias = "CASP"))]
257    CryptoAssetServicesProvider,
258
259    /// DCMS - Designated Contract Market
260    #[cfg_attr(feature = "serde", serde(alias = "DCMS"))]
261    DesignatedContractMarket,
262
263    /// IDQS - Inter Dealer Quotation System
264    #[cfg_attr(feature = "serde", serde(alias = "IDQS"))]
265    InterDealerQuotationSystem,
266
267    /// MLTF - Multilateral Trading Facility
268    #[cfg_attr(feature = "serde", serde(alias = "MLTF"))]
269    MultilateralTradingFacility,
270
271    /// NSPD - Not Specified
272    #[cfg_attr(feature = "serde", serde(alias = "NSPD"))]
273    NotSpecified,
274
275    /// OTFS - Organised Trading Facility
276    #[cfg_attr(feature = "serde", serde(alias = "OTFS"))]
277    OrganisedTradingFacility,
278
279    /// OTHR - ,
280    #[cfg_attr(feature = "serde", serde(alias = "OTHR"))]
281    Other,
282
283    /// RMOS - Recognised Market Operator
284    #[cfg_attr(feature = "serde", serde(alias = "RMOS"))]
285    RecognisedMarketOperator,
286
287    /// RMKT - Regulated Market
288    #[cfg_attr(feature = "serde", serde(alias = "RMKT"))]
289    RegulatedMarket,
290
291    /// SEFS - Swap Execution Facility
292    #[cfg_attr(feature = "serde", serde(alias = "SEFS"))]
293    SwapExecutionFacility,
294
295    /// SINT - Systematic Internaliser
296    #[cfg_attr(feature = "serde", serde(alias = "SINT"))]
297    SystematicInternaliser,
298
299    /// TRFS - Trade Reporting Facility
300    #[cfg_attr(feature = "serde", serde(alias = "TRFS"))]
301    TradeReportingFacility,
302}
303
304/// The status of a MIC
305#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
306#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
307pub enum Status {
308    /// The MIC is active
309    #[cfg_attr(feature = "serde", serde(alias = "ACTIVE"))]
310    Active,
311
312    /// The MIC has been updated
313    #[cfg_attr(feature = "serde", serde(alias = "UPDATED"))]
314    Updated,
315
316    /// The MIC has expired
317    #[cfg_attr(feature = "serde", serde(alias = "EXPIRED"))]
318    Expired,
319}