Skip to main content

ftracker_identifiers/cfi/
schema.rs

1//! [`JsonSchema`] implementation for [`Cfi`].
2//!
3//! Enabled by the `schemars` feature. The schema is a structural, pattern-constrained string: it
4//! captures the six-uppercase-letter shape, but a regex cannot express which category/group/
5//! attribute *combinations* ISO 10962 actually defines. Deserialization (via the `serde` feature)
6//! still enforces the full taxonomy.
7
8#[cfg(not(feature = "std"))]
9extern crate alloc;
10
11#[cfg(not(feature = "std"))]
12use alloc::borrow::Cow;
13#[cfg(feature = "std")]
14use std::borrow::Cow;
15
16use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
17
18use super::Cfi;
19
20impl JsonSchema for Cfi {
21    fn schema_name() -> Cow<'static, str> {
22        Cow::Borrowed("Cfi")
23    }
24
25    fn json_schema(_: &mut SchemaGenerator) -> Schema {
26        json_schema!({
27            "type": "string",
28            "format": "cfi",
29            "minLength": 6,
30            "maxLength": 6,
31            "pattern": "^[A-Z]{6}$",
32            "description": "CFI (Classification of Financial Instruments, ISO 10962). \
33            The pattern is structural; taxonomic validity is enforced on deserialization."
34        })
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::super::Cfi;
41    use schemars::schema_for;
42
43    #[test]
44    fn schema_is_a_pattern_constrained_string() {
45        let schema = schema_for!(Cfi);
46        let json = serde_json::to_value(&schema).unwrap();
47
48        assert_eq!(json["type"], "string");
49        assert_eq!(json["format"], "cfi");
50        assert_eq!(json["minLength"], 6);
51        assert_eq!(json["maxLength"], 6);
52        assert_eq!(json["pattern"], "^[A-Z]{6}$");
53    }
54}