Skip to main content

mx20022_codegen/xsd/
types.rs

1//! XSD Abstract Syntax Tree types.
2//!
3//! These types represent the subset of XSD constructs found in ISO 20022 schemas.
4//! The design is intentionally narrow: only the patterns that actually appear in
5//! the `head`, `pacs`, `pain`, and `camt` schema families are modelled.
6
7/// A parsed XSD schema file.
8#[derive(Debug, Clone, PartialEq)]
9pub struct Schema {
10    /// The `targetNamespace` attribute of the `<xs:schema>` element.
11    pub target_namespace: String,
12    /// Top-level `<xs:element>` declarations.
13    pub elements: Vec<Element>,
14    /// Top-level `<xs:simpleType>` definitions.
15    pub simple_types: Vec<SimpleType>,
16    /// Top-level `<xs:complexType>` definitions.
17    pub complex_types: Vec<ComplexType>,
18}
19
20/// A top-level element declaration: `<xs:element name="…" type="…"/>`.
21#[derive(Debug, Clone, PartialEq)]
22pub struct Element {
23    /// The element name (e.g. `"Document"`, `"AppHdr"`).
24    pub name: String,
25    /// The declared type name (e.g. `"Document"`, `"BusinessApplicationHeaderV04"`).
26    pub type_name: String,
27}
28
29/// A `<xs:simpleType>` definition, always restriction-based in ISO 20022 schemas.
30#[derive(Debug, Clone, PartialEq)]
31pub struct SimpleType {
32    /// The type name.
33    pub name: String,
34    /// The restriction that defines the type's value space.
35    pub restriction: Restriction,
36}
37
38/// The `<xs:restriction>` element inside a `<xs:simpleType>`.
39#[derive(Debug, Clone, PartialEq)]
40pub struct Restriction {
41    /// The `base` attribute (e.g. `"xs:string"`, `"xs:decimal"`, `"xs:boolean"`).
42    pub base: String,
43    /// Zero or more constraining facets.
44    pub facets: Vec<Facet>,
45}
46
47/// A single XSD constraining facet.
48#[derive(Debug, Clone, PartialEq)]
49pub enum Facet {
50    /// `<xs:enumeration value="…"/>`
51    Enumeration(String),
52    /// `<xs:pattern value="…"/>`
53    Pattern(String),
54    /// `<xs:minLength value="…"/>`
55    MinLength(u64),
56    /// `<xs:maxLength value="…"/>`
57    MaxLength(u64),
58    /// `<xs:minInclusive value="…"/>`
59    MinInclusive(String),
60    /// `<xs:maxInclusive value="…"/>`
61    MaxInclusive(String),
62    /// `<xs:totalDigits value="…"/>`
63    TotalDigits(u32),
64    /// `<xs:fractionDigits value="…"/>`
65    FractionDigits(u32),
66}
67
68/// A `<xs:complexType>` definition.
69#[derive(Debug, Clone, PartialEq)]
70pub struct ComplexType {
71    /// The type name.
72    pub name: String,
73    /// The content model of the complex type.
74    pub content: ComplexContent,
75}
76
77/// The content model of a complex type.
78#[derive(Debug, Clone, PartialEq)]
79pub enum ComplexContent {
80    /// `<xs:sequence>` — maps to a struct with ordered fields.
81    Sequence(Vec<SequenceElement>),
82    /// `<xs:choice>` — maps to a Rust enum.
83    Choice(Vec<ChoiceVariant>),
84    /// `<xs:simpleContent><xs:extension base="…">` — a value type with attributes.
85    SimpleContent {
86        /// The base type referenced in `<xs:extension base="…">`.
87        base: String,
88        /// Attributes declared inside the extension.
89        attributes: Vec<Attribute>,
90    },
91    /// `<xs:any>` wildcard or empty body — used for opaque wrapper types.
92    Any {
93        /// The `namespace` attribute of `<xs:any>`, if present.
94        namespace: Option<String>,
95    },
96}
97
98/// An element inside a `<xs:sequence>`.
99#[derive(Debug, Clone, PartialEq)]
100pub struct SequenceElement {
101    /// The element name.
102    pub name: String,
103    /// The element type name.
104    pub type_name: String,
105    /// Minimum occurrences (default `1`).
106    pub min_occurs: u32,
107    /// Maximum occurrences (default `Bounded(1)`).
108    pub max_occurs: MaxOccurs,
109}
110
111/// The `maxOccurs` attribute on a sequence element.
112#[derive(Debug, Clone, PartialEq)]
113pub enum MaxOccurs {
114    /// A finite upper bound.
115    Bounded(u32),
116    /// `maxOccurs="unbounded"`.
117    Unbounded,
118}
119
120/// A variant inside a `<xs:choice>`.
121#[derive(Debug, Clone, PartialEq)]
122pub struct ChoiceVariant {
123    /// The element name used as the variant label.
124    pub name: String,
125    /// The element type name.
126    pub type_name: String,
127}
128
129/// An `<xs:attribute>` declaration.
130#[derive(Debug, Clone, PartialEq)]
131pub struct Attribute {
132    /// The attribute name.
133    pub name: String,
134    /// The attribute type name.
135    pub type_name: String,
136    /// Whether `use="required"` was specified.
137    pub required: bool,
138}