regit-identifiers 1.0.1

Securities identifier validation in pure Rust — ISIN, CUSIP, SEDOL, LEI, BIC, MIC, FIGI, CFI and national numbers. Check-digit algorithms, parsing, and conversion. Zero dependencies, no_std, no alloc.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright 2026 Regit.io — Nicolas Koenig
// SPDX-License-Identifier: Apache-2.0

//! CFI — Classification of Financial Instruments (ISO 10962).
//!
//! A CFI code classifies a financial instrument — what kind of thing it is,
//! rather than which specific issue. It is exactly 6 characters, all
//! upper-case letters, in three parts:
//!
//! ```text
//!   E S V U F R
//!   │ │ └──┬──┘
//!   │ │    └──── attributes  [2..6]  four characters, instrument-specific
//!   │ └───────── group       [1]     a category-dependent sub-class
//!   └─────────── category    [0]     one of 14 ISO 10962 category letters
//! ```
//!
//! - The **category** is the top-level class. It must be one of the 14
//!   ISO 10962 category letters `E C D R O F S H I J K L T M`.
//! - The **group** narrows the category into a sub-class; its meaning depends
//!   on the category.
//! - The **four attributes** further describe the instrument; an `X` in any
//!   attribute position means "not applicable / not known".
//!
//! A CFI carries **no check digit**, so any 6 upper-case letters whose first
//! character is a valid category letter form a structurally valid CFI.
//!
//! # Scope of validation
//!
//! [`Cfi::parse`] validates **structure and category only**: the exact
//! length, the all-`[A-Z]` character set, and that character 1 is a
//! recognised category letter. It deliberately does **not** check the group
//! character or the four attribute characters against the per-category
//! ISO 10962 tables. Those tables are large, category-specific, and revised
//! with each edition of the standard; validating against an embedded snapshot
//! would silently reject instruments classified under a newer revision. The
//! group and attributes are therefore exposed verbatim through accessors but
//! left semantically unvalidated.
//!
//! # References
//!
//! - ISO 10962, *Securities and related financial instruments —
//!   Classification of financial instruments (CFI) code*.

use crate::errors::ValidationError;

/// A validated Classification of Financial Instruments code (ISO 10962).
///
/// A `Cfi` can only be created by [`Cfi::parse`] (or the explicitly unchecked
/// [`Cfi::from_bytes_unchecked`]), so a value of this type is a proof that
/// the 6 characters are all upper-case letters and that the first is a
/// recognised ISO 10962 category letter. It stores the code inline as
/// `[u8; 6]`, is `Copy`, and allocates nothing.
///
/// # Examples
///
/// ```
/// use regit_identifiers::Cfi;
///
/// let cfi = Cfi::parse("ESVUFR").unwrap();
/// assert_eq!(cfi.category(), 'E');
/// assert_eq!(cfi.category_name(), "Equities");
/// assert_eq!(cfi.group(), 'S');
/// assert_eq!(cfi.attributes(), "VUFR");
/// assert_eq!(cfi.as_str(), "ESVUFR");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Cfi {
    /// The 6 validated ASCII bytes of the code.
    bytes: [u8; Self::LENGTH],
}

impl Cfi {
    /// The number of characters in a CFI code.
    pub const LENGTH: usize = 6;

    /// Parses and validates a CFI code.
    ///
    /// Validation is strict and, in order: the input must be exactly 6
    /// characters; every character must be an ASCII upper-case letter; and
    /// the first character must be one of the 14 ISO 10962 category letters
    /// `E C D R O F S H I J K L T M`. A CFI has no check digit, so any
    /// 6-letter string satisfying those rules parses.
    ///
    /// The group character and the four attribute characters are **not**
    /// validated against the per-category ISO 10962 tables — see the module
    /// documentation for why.
    ///
    /// # Errors
    ///
    /// - [`ValidationError::WrongLength`] if the input is not 6 characters.
    /// - [`ValidationError::InvalidCharacter`] if a character is not an ASCII
    ///   upper-case letter (this also rejects digits, lower-case input, and
    ///   any non-ASCII character).
    /// - [`ValidationError::Structure`] if the first character is not a
    ///   recognised ISO 10962 category letter.
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_identifiers::Cfi;
    /// use regit_identifiers::errors::ValidationError;
    ///
    /// assert!(Cfi::parse("ESVUFR").is_ok());
    ///
    /// // `Q` is not one of the 14 ISO 10962 category letters.
    /// assert_eq!(
    ///     Cfi::parse("QSVUFR"),
    ///     Err(ValidationError::Structure {
    ///         rule: "CFI category must be one of E C D R O F S H I J K L T M",
    ///     }),
    /// );
    /// ```
    pub fn parse(s: &str) -> Result<Self, ValidationError> {
        // A CFI is exactly 6 characters.
        let found = s.chars().count();
        if found != Self::LENGTH {
            return Err(ValidationError::WrongLength {
                expected: Self::LENGTH,
                found,
            });
        }
        // Every character must be an ASCII upper-case letter. A non-ASCII
        // character fails the predicate and is rejected here.
        for (i, ch) in s.chars().enumerate() {
            if !ch.is_ascii_uppercase() {
                return Err(ValidationError::InvalidCharacter {
                    position: i + 1,
                    found: ch,
                });
            }
        }
        // Every character is ASCII, so the string is exactly 6 ASCII bytes.
        let mut bytes = [0u8; Self::LENGTH];
        bytes.copy_from_slice(s.as_bytes());

        // The first character must be a recognised ISO 10962 category letter.
        if category_name_of(bytes[0]).is_none() {
            return Err(ValidationError::Structure {
                rule: "CFI category must be one of E C D R O F S H I J K L T M",
            });
        }
        Ok(Self { bytes })
    }

    /// Validates a CFI code without constructing one.
    ///
    /// Equivalent to `Cfi::parse(s).map(|_| ())`; use it when only the
    /// verdict is needed.
    ///
    /// # Errors
    ///
    /// Returns the same [`ValidationError`] variants as [`Cfi::parse`].
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_identifiers::Cfi;
    ///
    /// assert!(Cfi::validate("ESVUFR").is_ok());
    /// assert!(Cfi::validate("QSVUFR").is_err());
    /// ```
    pub fn validate(s: &str) -> Result<(), ValidationError> {
        Self::parse(s).map(|_| ())
    }

    /// Wraps 6 raw bytes as a `Cfi` without any validation.
    ///
    /// The caller asserts that `bytes` holds the 6 ASCII characters of a
    /// valid CFI. This exists for reconstructing a `Cfi` from bytes that were
    /// validated earlier; prefer [`Cfi::parse`] for any untrusted input.
    ///
    /// Bypassing validation has a consequence for [`Cfi::category_name`]: if
    /// `bytes[0]` is not one of the 14 ISO 10962 category letters, the
    /// accessor returns the empty string `""` (the safe fallback).
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_identifiers::Cfi;
    ///
    /// let cfi = Cfi::from_bytes_unchecked(*b"ESVUFR");
    /// assert_eq!(cfi.as_str(), "ESVUFR");
    /// ```
    #[must_use]
    pub const fn from_bytes_unchecked(bytes: [u8; Self::LENGTH]) -> Self {
        Self { bytes }
    }

    /// Returns the CFI code as a string slice.
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_identifiers::Cfi;
    ///
    /// assert_eq!(Cfi::parse("ESVUFR").unwrap().as_str(), "ESVUFR");
    /// ```
    #[must_use]
    #[inline]
    pub fn as_str(&self) -> &str {
        core::str::from_utf8(&self.bytes).unwrap_or("")
    }

    /// Returns the CFI code as its 6 raw ASCII bytes.
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_identifiers::Cfi;
    ///
    /// assert_eq!(Cfi::parse("ESVUFR").unwrap().as_bytes(), b"ESVUFR");
    /// ```
    #[must_use]
    #[inline]
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes
    }

    /// Returns the category letter, character 1.
    ///
    /// This is always one of the 14 ISO 10962 category letters.
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_identifiers::Cfi;
    ///
    /// assert_eq!(Cfi::parse("ESVUFR").unwrap().category(), 'E');
    /// ```
    #[must_use]
    #[inline]
    pub fn category(&self) -> char {
        char::from(self.bytes[0])
    }

    /// Returns the ISO 10962 name of the category, character 1.
    ///
    /// The returned name is one of the 14 fixed category descriptions; it is
    /// never empty for a value produced by [`Cfi::parse`].
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_identifiers::Cfi;
    ///
    /// assert_eq!(Cfi::parse("ESVUFR").unwrap().category_name(), "Equities");
    /// assert_eq!(Cfi::parse("DBFUGR").unwrap().category_name(), "Debt instruments");
    /// ```
    #[must_use]
    #[inline]
    pub fn category_name(&self) -> &'static str {
        category_name_of(self.bytes[0]).unwrap_or("")
    }

    /// Returns the group letter, character 2.
    ///
    /// The group narrows the category into a sub-class. Its meaning is
    /// category-dependent and is **not** validated by [`Cfi::parse`].
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_identifiers::Cfi;
    ///
    /// assert_eq!(Cfi::parse("ESVUFR").unwrap().group(), 'S');
    /// ```
    #[must_use]
    #[inline]
    pub fn group(&self) -> char {
        char::from(self.bytes[1])
    }

    /// Returns the four attribute characters, characters 3–6.
    ///
    /// The attributes further describe the instrument; an `X` in a position
    /// means "not applicable". Their meaning is category-dependent and is
    /// **not** validated by [`Cfi::parse`].
    ///
    /// # Examples
    ///
    /// ```
    /// use regit_identifiers::Cfi;
    ///
    /// assert_eq!(Cfi::parse("ESVUFR").unwrap().attributes(), "VUFR");
    /// ```
    #[must_use]
    #[inline]
    pub fn attributes(&self) -> &str {
        core::str::from_utf8(&self.bytes[2..6]).unwrap_or("")
    }
}

/// Maps an ISO 10962 category letter to its English name.
///
/// Returns `None` if `b` is not one of the 14 recognised category letters,
/// which is exactly how [`Cfi::parse`] decides whether character 1 is valid.
fn category_name_of(b: u8) -> Option<&'static str> {
    match b {
        b'E' => Some("Equities"),
        b'C' => Some("Collective investment vehicles"),
        b'D' => Some("Debt instruments"),
        b'R' => Some("Entitlements (rights)"),
        b'O' => Some("Listed options"),
        b'F' => Some("Futures"),
        b'S' => Some("Swaps"),
        b'H' => Some("Non-listed and complex listed options"),
        b'I' => Some("Spot"),
        b'J' => Some("Forwards"),
        b'K' => Some("Strategies"),
        b'L' => Some("Financing"),
        b'T' => Some("Referential instruments"),
        b'M' => Some("Others (miscellaneous)"),
        _ => None,
    }
}

impl core::fmt::Display for Cfi {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl core::str::FromStr for Cfi {
    type Err = ValidationError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s)
    }
}

impl AsRef<str> for Cfi {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_support::display;
    use core::str::FromStr;

    /// Well-formed CFI codes used as regression anchors.
    const GOLDEN: &[&str] = &[
        "ESVUFR", // Equities
        "DBFUGR", // Debt instruments
        "OCASPS", // Listed options
        "CIOIES", // Collective investment vehicles
    ];

    /// One representative CFI for every ISO 10962 category letter, paired
    /// with the expected category name.
    const CATEGORY_VECTORS: &[(&str, char, &str)] = &[
        ("EXXXXX", 'E', "Equities"),
        ("CXXXXX", 'C', "Collective investment vehicles"),
        ("DXXXXX", 'D', "Debt instruments"),
        ("RXXXXX", 'R', "Entitlements (rights)"),
        ("OXXXXX", 'O', "Listed options"),
        ("FXXXXX", 'F', "Futures"),
        ("SXXXXX", 'S', "Swaps"),
        ("HXXXXX", 'H', "Non-listed and complex listed options"),
        ("IXXXXX", 'I', "Spot"),
        ("JXXXXX", 'J', "Forwards"),
        ("KXXXXX", 'K', "Strategies"),
        ("LXXXXX", 'L', "Financing"),
        ("TXXXXX", 'T', "Referential instruments"),
        ("MXXXXX", 'M', "Others (miscellaneous)"),
    ];

    #[test]
    fn parses_golden_cfis() {
        for &s in GOLDEN {
            let cfi = Cfi::parse(s).unwrap_or_else(|e| panic!("{s} should parse: {e}"));
            assert_eq!(cfi.as_str(), s);
        }
    }

    #[test]
    fn segment_accessors() {
        let cfi = Cfi::parse("ESVUFR").unwrap();
        assert_eq!(cfi.category(), 'E');
        assert_eq!(cfi.category_name(), "Equities");
        assert_eq!(cfi.group(), 'S');
        assert_eq!(cfi.attributes(), "VUFR");
        assert_eq!(cfi.as_bytes(), b"ESVUFR");
        assert_eq!(Cfi::LENGTH, 6);
    }

    #[test]
    fn accepts_all_category_letters() {
        for &(s, letter, name) in CATEGORY_VECTORS {
            let cfi = Cfi::parse(s).unwrap_or_else(|e| panic!("{s} should parse: {e}"));
            assert_eq!(cfi.category(), letter);
            assert_eq!(cfi.category_name(), name);
        }
    }

    #[test]
    fn accepts_any_group_and_attributes() {
        // A CFI has no check digit and group/attributes are unvalidated, so
        // any 6-letter code with a valid category letter parses.
        let cfi = Cfi::parse("EZZZZZ").unwrap();
        assert_eq!(cfi.group(), 'Z');
        assert_eq!(cfi.attributes(), "ZZZZ");
    }

    #[test]
    fn rejects_wrong_length() {
        assert_eq!(
            Cfi::parse("ESVUF"),
            Err(ValidationError::WrongLength {
                expected: 6,
                found: 5,
            })
        );
        assert_eq!(
            Cfi::parse("ESVUFRR"),
            Err(ValidationError::WrongLength {
                expected: 6,
                found: 7,
            })
        );
        assert_eq!(
            Cfi::parse(""),
            Err(ValidationError::WrongLength {
                expected: 6,
                found: 0,
            })
        );
    }

    #[test]
    fn rejects_digit() {
        assert!(matches!(
            Cfi::parse("ESVUF1"),
            Err(ValidationError::InvalidCharacter { position: 6, .. })
        ));
    }

    #[test]
    fn rejects_lower_case() {
        assert!(matches!(
            Cfi::parse("esvufr"),
            Err(ValidationError::InvalidCharacter { position: 1, .. })
        ));
    }

    #[test]
    fn rejects_unknown_category() {
        assert_eq!(
            Cfi::parse("QSVUFR"),
            Err(ValidationError::Structure {
                rule: "CFI category must be one of E C D R O F S H I J K L T M",
            })
        );
    }

    #[test]
    fn rejects_non_ascii_without_panic() {
        // A multi-byte character must be rejected cleanly.
        assert!(Cfi::parse("ESVUFÉ").is_err());
        assert!(Cfi::parse("ÉSVUFR").is_err());
    }

    #[test]
    fn round_trips_through_str() {
        for &s in GOLDEN {
            assert_eq!(Cfi::parse(s).unwrap().as_str(), s);
        }
    }

    #[test]
    fn from_str_matches_parse() {
        assert_eq!(Cfi::from_str("ESVUFR"), Cfi::parse("ESVUFR"));
        assert!(Cfi::from_str("nonsense").is_err());
    }

    #[test]
    fn display_renders_identifier() {
        let cfi = Cfi::parse("ESVUFR").unwrap();
        assert_eq!(display(cfi).as_str(), "ESVUFR");
    }

    #[test]
    fn as_ref_str() {
        let cfi = Cfi::parse("ESVUFR").unwrap();
        let s: &str = cfi.as_ref();
        assert_eq!(s, "ESVUFR");
    }

    #[test]
    fn from_bytes_unchecked_round_trip() {
        let cfi = Cfi::from_bytes_unchecked(*b"ESVUFR");
        assert_eq!(cfi, Cfi::parse("ESVUFR").unwrap());
    }

    #[test]
    fn validate_matches_parse() {
        assert!(Cfi::validate("ESVUFR").is_ok());
        assert!(Cfi::validate("QSVUFR").is_err());
        assert!(Cfi::validate("ESVUF").is_err());
    }

    #[test]
    fn is_copy_and_eq_and_hashable() {
        let a = Cfi::parse("ESVUFR").unwrap();
        let b = a; // Copy
        assert_eq!(a, b);
        assert_ne!(a, Cfi::parse("DBFUGR").unwrap());
        // Usable as a map key (Eq + Hash) — checked by constructing a slice.
        let keys = [a, b];
        assert_eq!(keys[0], keys[1]);
    }
}