1#![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#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, ThisError)]
48#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
49pub enum Error {
50 #[error("Invalid length.")]
52 InvalidLength(usize, usize),
53 #[error("Invalid character at position {0}.")]
55 InvalidCharacter(usize),
56}
57
58#[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 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 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 pub const fn as_bytes(&self) -> &[u8] {
90 &self.0
91 }
92
93 #[allow(unsafe_code)]
95 pub const fn as_str(&self) -> &str {
96 unsafe { str::from_utf8_unchecked(&self.0) }
98 }
99
100 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#[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 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 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 pub const fn from_str_slice(s: &str) -> Result<Self, Error> {
161 Self::from_bytes(s.as_bytes())
162 }
163
164 pub const fn as_bytes(&self) -> &[u8] {
166 &self.0
167 }
168
169 #[allow(unsafe_code)]
171 pub const fn as_str(&self) -> &str {
172 unsafe { str::from_utf8_unchecked(&self.0) }
175 }
176
177 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#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
225#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
226pub enum Kind {
227 #[cfg_attr(feature = "serde", serde(alias = "OPRT"))]
229 Operating,
230 #[cfg_attr(feature = "serde", serde(alias = "SGMT"))]
232 Segment,
233}
234
235#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
237#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
238pub enum Category {
239 #[cfg_attr(feature = "serde", serde(alias = "ATSS"))]
241 AlternativeTradingSystem,
242
243 #[cfg_attr(feature = "serde", serde(alias = "APPA"))]
245 ApprovedPublicationArrangement,
246
247 #[cfg_attr(feature = "serde", serde(alias = "ARMS"))]
249 ApprovedReportingMechanism,
250
251 #[cfg_attr(feature = "serde", serde(alias = "CTPS"))]
253 ConsolidatedTapeProvider,
254
255 #[cfg_attr(feature = "serde", serde(alias = "CASP"))]
257 CryptoAssetServicesProvider,
258
259 #[cfg_attr(feature = "serde", serde(alias = "DCMS"))]
261 DesignatedContractMarket,
262
263 #[cfg_attr(feature = "serde", serde(alias = "IDQS"))]
265 InterDealerQuotationSystem,
266
267 #[cfg_attr(feature = "serde", serde(alias = "MLTF"))]
269 MultilateralTradingFacility,
270
271 #[cfg_attr(feature = "serde", serde(alias = "NSPD"))]
273 NotSpecified,
274
275 #[cfg_attr(feature = "serde", serde(alias = "OTFS"))]
277 OrganisedTradingFacility,
278
279 #[cfg_attr(feature = "serde", serde(alias = "OTHR"))]
281 Other,
282
283 #[cfg_attr(feature = "serde", serde(alias = "RMOS"))]
285 RecognisedMarketOperator,
286
287 #[cfg_attr(feature = "serde", serde(alias = "RMKT"))]
289 RegulatedMarket,
290
291 #[cfg_attr(feature = "serde", serde(alias = "SEFS"))]
293 SwapExecutionFacility,
294
295 #[cfg_attr(feature = "serde", serde(alias = "SINT"))]
297 SystematicInternaliser,
298
299 #[cfg_attr(feature = "serde", serde(alias = "TRFS"))]
301 TradeReportingFacility,
302}
303
304#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
306#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
307pub enum Status {
308 #[cfg_attr(feature = "serde", serde(alias = "ACTIVE"))]
310 Active,
311
312 #[cfg_attr(feature = "serde", serde(alias = "UPDATED"))]
314 Updated,
315
316 #[cfg_attr(feature = "serde", serde(alias = "EXPIRED"))]
318 Expired,
319}