xsd_parser/models/schema/
xs.rs

1#![allow(
2    missing_docs,
3    unused_mut,
4    unused_variables,
5    clippy::large_enum_variant,
6    clippy::len_zero,
7    clippy::missing_errors_doc,
8    clippy::needless_pass_by_value,
9    clippy::ptr_arg,
10    clippy::redundant_else,
11    clippy::redundant_field_names,
12    clippy::semicolon_if_nothing_returned,
13    clippy::single_match,
14    clippy::too_many_lines,
15    clippy::unnecessary_wraps,
16    clippy::unused_self
17)]
18
19use std::{
20    hash::{Hash, Hasher},
21    str::from_utf8,
22};
23
24use quick_xml::{events::Event, Writer};
25use unindent::unindent;
26
27use xsd_parser_types::quick_xml::{Serializer, WithSerializer};
28
29pub type Use = AttributeUseType;
30
31include!("./xs_generated.rs");
32
33impl Copy for FormChoiceType {}
34
35impl Hash for FormChoiceType {
36    fn hash<H: Hasher>(&self, state: &mut H) {
37        match self {
38            Self::Qualified => state.write_u8(0),
39            Self::Unqualified => state.write_u8(1),
40        }
41    }
42}
43
44impl Copy for AttributeUseType {}
45
46impl Hash for AttributeUseType {
47    fn hash<H: Hasher>(&self, state: &mut H) {
48        match self {
49            Self::Prohibited => state.write_u8(0),
50            Self::Optional => state.write_u8(1),
51            Self::Required => state.write_u8(2),
52        }
53    }
54}
55
56impl Annotation {
57    /// Extract the `xs:documentation` nodes from this `xs:annotation` node.
58    ///
59    /// This will extract each `xs:documentation` node from the `xs:annotation`,
60    /// serialize the content of the `xs:documentation` node into a string and
61    /// return it as vector.
62    ///
63    /// # Errors
64    ///
65    /// Will raise an error if the serialization of the `xs:documentation` node
66    /// has failed.
67    pub fn extract_documentation(&self) -> Result<Vec<String>, Error> {
68        let mut docs = Vec::new();
69
70        self.extract_documentation_into(&mut docs)?;
71
72        Ok(docs)
73    }
74
75    /// Extract the `xs:documentation` nodes from this `xs:annotation` node and
76    /// store it in the passed `docs` vector.
77    ///
78    /// # Errors
79    ///
80    /// Will raise an error if the serialization of the `xs:documentation` node
81    /// has failed.
82    pub fn extract_documentation_into(&self, docs: &mut Vec<String>) -> Result<(), Error> {
83        for content in &self.content {
84            let AnnotationContent::Documentation(doc) = content else {
85                continue;
86            };
87
88            let mut level = 0usize;
89            let mut buffer = Vec::new();
90            let mut writer = Writer::new(&mut buffer);
91
92            for event in doc
93                .serializer(None, true)
94                .into_iter()
95                .flat_map(Serializer::into_iter)
96            {
97                let event = event?;
98                let write = match &event {
99                    Event::Start(_) | Event::Empty(_) => {
100                        let write = level > 0;
101
102                        level += 1;
103
104                        write
105                    }
106                    Event::End(_) => {
107                        level = level.saturating_sub(1);
108
109                        level > 0
110                    }
111                    _ => level > 0,
112                };
113
114                if write {
115                    writer.write_event(event)?;
116                }
117            }
118
119            if !buffer.is_empty() {
120                let docu = from_utf8(&buffer)?;
121                let docu = unindent(docu).trim_end().to_owned();
122
123                docs.push(docu);
124            }
125        }
126
127        Ok(())
128    }
129}