xmlschema/
lib.rs

1use std::{
2    collections::HashMap,
3    fmt::{Display, Error, Formatter},
4};
5
6#[derive(Debug)]
7enum XmlSchemaNode {
8    /// xs:attribute
9    Attribute(Attribute),
10    /// xs:complexType
11    ComplexType(ComplexType),
12    /// xs:element
13    Element(Element),
14    /// xs:simpleType
15    SimpleType(SimpleType),
16}
17
18#[derive(Debug)]
19struct XmlSchema {
20    target_namespace: Option<String>,
21    element_form_default: Option<String>,
22    attribute_form_default: Option<String>,
23    nodes: Vec<XmlSchemaNode>,
24}
25
26#[derive(Debug)]
27struct Element {
28    /// The name of the element
29    name: String,
30    /// The datatype of the element
31    datatype: Datatype,
32    /// The maximum number of times the element can occur
33    max_occurs: u32,
34    /// The minimum number of times the element can occur
35    min_occurs: u32,
36}
37
38#[derive(Debug)]
39enum Datatype {
40    /// A reference to a simple type
41    SimpleType(String),
42    /// A reference to a complex type
43    ComplexType(String),
44}
45
46#[derive(Debug)]
47struct Attribute {
48    name: String,
49    datatype: Datatype,
50    default_value: Option<String>,
51    fixed_value: Option<String>,
52    use_option: UseOption,
53}
54
55#[derive(Debug)]
56enum UseOption {
57    Required,
58    Optional,
59}
60
61#[derive(Debug)]
62struct SimpleType {
63    name: String,
64    datatype: SimpleDatatype,
65}
66
67#[derive(Debug)]
68enum SimpleDatatype {
69    Boolean,
70    Decimal,
71    Double,
72    Float,
73    Integer,
74    String,
75}
76
77#[derive(Debug)]
78struct ComplexType {
79    name: String,
80    base_type: Option<String>,
81    attributes: HashMap<String, Attribute>,
82    content: ComplexContent,
83    mixed_content: Option<String>,
84}
85
86#[derive(Debug)]
87enum ComplexContent {
88    All,
89    Choice,
90    Empty,
91    Element,
92    Group,
93    Sequence,
94    SimpleContent,
95    Union,
96    MixedContent,
97    ComplexContentExtension,
98}
99
100#[derive(Debug)]
101struct SimpleContent {
102    datatype: SimpleDatatype,
103}
104
105#[derive(Debug)]
106struct ComplexContentExtension {
107    base_type: String,
108    attributes: HashMap<String, Attribute>,
109}
110
111#[derive(Debug)]
112struct ComplexContentRestriction {
113    base_type: String,
114    attributes: HashMap<String, Attribute>,
115}
116
117struct XmlSchemaParser<'a> {
118    input: &'a str,
119    position: usize,
120}
121
122impl<'a> XmlSchemaParser<'a> {
123    fn new(input: &'a str) -> Self {
124        Self { input, position: 0 }
125    }
126
127    /// Parses an XML schema from the input stream.
128    ///
129    /// This function reads the input stream character by character,
130    /// parsing different types of XML elements and adding them to an
131    /// `XmlSchema` object. The function returns the `XmlSchema` object
132    /// wrapped in an `Ok` variant of the `Result` type, or an error
133    /// message in the form of a `String` wrapped in an `Err` variant of
134    /// the `Result` type.
135    ///
136    /// # Arguments
137    ///
138    /// * `self` - A mutable reference to an object that implements the
139    /// `parse()` function.
140    ///
141    /// # Examples
142    ///
143    /// ```
144    /// let mut parser = XmlSchemaParser::new(input);
145    /// let result = parser.parse();
146    /// match result {
147    ///     Ok(schema) => {
148    ///         // Do something with the parsed schema
149    ///     }
150    ///     Err(message) => {
151    ///         eprintln!("Error parsing schema: {}", message);
152    ///     }
153    /// }
154    /// ```
155    ///
156    fn parse(&mut self) -> Result<XmlSchema, String> {
157        let mut schema = XmlSchema {
158            target_namespace: None,
159            element_form_default: None,
160            attribute_form_default: None,
161            nodes: Vec::new(),
162        };
163
164        while let Some(ch) = self.next_char() {
165            match ch {
166                '<' => {
167                    let tag_name = self.parse_tag_name()?;
168                    match tag_name.as_str() {
169                        "xs:schema" => {
170                            schema.target_namespace =
171                                self.parse_optional_attr_value("targetNamespace");
172                            schema.element_form_default =
173                                self.parse_optional_attr_value("elementFormDefault");
174                            schema.attribute_form_default =
175                                self.parse_optional_attr_value("attributeFormDefault");
176                        }
177                        "xs:element" => {
178                            let element = self.parse_element()?;
179                            schema.nodes.push(XmlSchemaNode::Element(element));
180                        }
181                        "xs:attribute" => {
182                            let attribute = self.parse_attribute()?;
183                            schema.nodes.push(XmlSchemaNode::Attribute(attribute));
184                        }
185                        "xs:simpleType" => {
186                            let simple_type = self.parse_simple_type()?;
187                            schema.nodes.push(XmlSchemaNode::SimpleType(simple_type));
188                        }
189                        "xs:complexType" => {
190                            let complex_type = self.parse_complex_type()?;
191                            schema.nodes.push(XmlSchemaNode::ComplexType(complex_type));
192                        }
193                        _ => return Err(format!("Unexpected tag: {}", tag_name)),
194                    }
195                }
196                _ => {
197                    // Ignore non-tag characters
198                }
199            }
200        }
201        Ok(schema)
202    }
203
204    /// Returns the next character from the input stream, or `None` if
205    /// there are no more characters.
206    ///
207    /// This function reads the input stream character by character,
208    /// returning each character one at a time until the end of the
209    /// stream is reached. The function returns an `Option` object that
210    /// either contains the next character as a `char`, or `None` if
211    /// there are no more characters to read.
212    ///
213    /// # Arguments
214    ///
215    /// * `self` - A mutable reference to an object that implements the
216    /// `next_char()` function.
217    ///
218    /// # Examples
219    ///
220    /// ```
221    /// let mut parser = XmlSchemaParser::new(input);
222    /// while let Some(ch) = parser.next_char() {
223    ///     match ch {
224    ///         '<' => {
225    ///             // Do something with the opening tag
226    ///         }
227    ///         '>' => {
228    ///             // Do something with the closing tag
229    ///         }
230    ///         _ => {
231    ///             // Do something with non-tag characters
232    ///         }
233    ///     }
234    /// }
235    /// ```
236    ///
237    fn next_char(&mut self) -> Option<char> {
238        let ch = self.input.chars().nth(self.position);
239        self.position += 1;
240        ch
241    }
242
243    /// Parses the name of an XML tag from the input stream.
244    ///
245    /// This function reads the input stream character by character, parsing the name of an XML tag
246    /// until it encounters a whitespace character or a closing angle bracket '>'. The function returns
247    /// the parsed tag name as a `String` wrapped in an `Ok` variant of the `Result` type, or an error
248    /// message in the form of a `String` wrapped in an `Err` variant of the `Result` type.
249    ///
250    /// # Arguments
251    ///
252    /// * `self` - A mutable reference to an object that implements the `parse_tag_name()` function.
253    ///
254    /// # Examples
255    ///
256    /// ```
257    /// let mut parser = XmlSchemaParser::new(input);
258    /// let result = parser.parse_tag_name();
259    /// match result {
260    ///     Ok(tag_name) => {
261    ///         // Do something with the parsed tag name
262    ///     }
263    ///     Err(message) => {
264    ///         eprintln!("Error parsing tag name: {}", message);
265    ///     }
266    /// }
267    /// ```
268    ///
269    fn parse_tag_name(&mut self) -> Result<String, String> {
270        let mut tag_name = String::new();
271        while let Some(ch) = self.next_char() {
272            match ch {
273                '>' => break,
274                ' ' => break,
275                _ => tag_name.push(ch),
276            }
277        }
278        Ok(tag_name)
279    }
280
281    /// Parses the value of an XML attribute from the input stream.
282    ///
283    /// This function reads the input stream character by character, searching for the value of an
284    /// XML attribute with the given `attr_name` until it encounters a whitespace character, a closing
285    /// angle bracket '>', or a double-quote character '"'. If the attribute is found and has a value,
286    /// the function returns the value as a `String` wrapped in a `Some` variant of the `Option` type.
287    /// If the attribute is not found or has no value, the function returns `None`.
288    ///
289    /// # Arguments
290    ///
291    /// * `self` - A mutable reference to an object that implements the `parse_optional_attr_value()`
292    /// function.
293    /// * `attr_name` - A string slice representing the name of the XML attribute to search for.
294    ///
295    /// # Examples
296    ///
297    /// ```
298    /// let mut parser = XmlSchemaParser::new(input);
299    /// let result = parser.parse_optional_attr_value("targetNamespace");
300    /// match result {
301    ///     Some(attr_value) => {
302    ///         // Do something with the parsed attribute value
303    ///     }
304    ///     None => {
305    ///         // The attribute was not found or has no value
306    ///     }
307    /// }
308    /// ```
309    ///
310    fn parse_optional_attr_value(&mut self, attr_name: &str) -> Option<String> {
311        let mut attr_value = String::new();
312        while let Some(ch) = self.next_char() {
313            match ch {
314                '>' => break,
315                ' ' => break,
316                '"' => {
317                    while let Some(ch) = self.next_char() {
318                        match ch {
319                            '"' => break,
320                            _ => attr_value.push(ch),
321                        }
322                    }
323                    break;
324                }
325                _ => {}
326            }
327        }
328        if attr_value.is_empty() {
329            None
330        } else {
331            Some(attr_value)
332        }
333    }
334
335    /// Parses an `xs:element` XML element from the input stream.
336    ///
337    /// This function reads the input stream character by character, parsing the name and attributes
338    /// of an `xs:element` XML element. The function returns an `Element` object containing the
339    /// parsed data, wrapped in an `Ok` variant of the `Result` type, or an error message in the form
340    /// of a `String` wrapped in an `Err` variant of the `Result` type.
341    ///
342    /// # Arguments
343    ///
344    /// * `self` - A mutable reference to an object that implements the `parse_element()` function.
345    ///
346    /// # Examples
347    ///
348    /// ```
349    /// let mut parser = XmlSchemaParser::new(input);
350    /// let result = parser.parse_element();
351    /// match result {
352    ///     Ok(element) => {
353    ///         // Do something with the parsed element
354    ///     }
355    ///     Err(message) => {
356    ///         eprintln!("Error parsing element: {}", message);
357    ///     }
358    /// }
359    /// ```
360    ///
361    fn parse_element(&mut self) -> Result<Element, String> {
362        let mut element = Element {
363            name: String::new(),
364            datatype: Datatype::SimpleType(String::new()),
365            max_occurs: 1,
366            min_occurs: 1,
367        };
368        while let Some(ch) = self.next_char() {
369            match ch {
370                '>' => break,
371                ' ' => break,
372                _ => element.name.push(ch),
373            }
374        }
375        while let Some(ch) = self.next_char() {
376            match ch {
377                '>' => break,
378                ' ' => break,
379                _ => {}
380            }
381        }
382        Ok(element)
383    }
384    /// Parses an `xs:attribute` XML element from the input stream.
385    ///
386    /// This function reads the input stream character by character, parsing the name and attributes
387    /// of an `xs:attribute` XML element. The function returns an `Attribute` object containing the
388    /// parsed data, wrapped in an `Ok` variant of the `Result` type, or an error message in the form
389    /// of a `String` wrapped in an `Err` variant of the `Result` type.
390    ///
391    /// # Arguments
392    ///
393    /// * `self` - A mutable reference to an object that implements the `parse_attribute()` function.
394    ///
395    /// # Examples
396    ///
397    /// ```
398    /// let mut parser = XmlSchemaParser::new(input);
399    /// let result = parser.parse_attribute();
400    /// match result {
401    ///     Ok(attribute) => {
402    ///         // Do something with the parsed attribute
403    ///     }
404    ///     Err(message) => {
405    ///         eprintln!("Error parsing attribute: {}", message);
406    ///     }
407    /// }
408    /// ```
409    ///
410    fn parse_attribute(&mut self) -> Result<Attribute, String> {
411        let mut attribute = Attribute {
412            name: String::new(),
413            datatype: Datatype::SimpleType(String::new()),
414            default_value: None,
415            fixed_value: None,
416            use_option: UseOption::Optional,
417        };
418        while let Some(ch) = self.next_char() {
419            match ch {
420                '>' => break,
421                ' ' => break,
422                _ => attribute.name.push(ch),
423            }
424        }
425        while let Some(ch) = self.next_char() {
426            match ch {
427                '>' => break,
428                ' ' => break,
429                _ => {}
430            }
431        }
432        Ok(attribute)
433    }
434    /// Parses an `xs:simpleType` XML element from the input stream.
435    ///
436    /// This function reads the input stream character by character, parsing the name and attributes
437    /// of an `xs:simpleType` XML element, as well as any child elements that define the data type of
438    /// the simple type. The function returns a `SimpleType` object containing the parsed data, wrapped
439    /// in an `Ok` variant of the `Result` type, or an error message in the form of a `String` wrapped
440    /// in an `Err` variant of the `Result` type.
441    ///
442    /// # Arguments
443    ///
444    /// * `self` - A mutable reference to an object that implements the `parse_simple_type()` function.
445    ///
446    /// # Examples
447    ///
448    /// ```
449    /// let mut parser = XmlSchemaParser::new(input);
450    /// let result = parser.parse_simple_type();
451    /// match result {
452    ///     Ok(simple_type) => {
453    ///         // Do something with the parsed simple type
454    ///     }
455    ///     Err(message) => {
456    ///         eprintln!("Error parsing simple type: {}", message);
457    ///     }
458    /// }
459    /// ```
460    ///
461    fn parse_simple_type(&mut self) -> Result<SimpleType, String> {
462        let mut simple_type = SimpleType {
463            name: String::new(),
464            datatype: SimpleDatatype::String,
465        };
466        while let Some(ch) = self.next_char() {
467            match ch {
468                '>' => break,
469                ' ' => break,
470                _ => simple_type.name.push(ch),
471            }
472        }
473        while let Some(ch) = self.next_char() {
474            match ch {
475                '<' => break,
476                _ => {}
477            }
478        }
479        let mut datatype = SimpleDatatype::String;
480        loop {
481            match self.next_char() {
482                Some(ch) => match ch {
483                    '>' => break,
484                    ' ' => {}
485                    's' => {
486                        let start = self.position - 1;
487                        let end = start + 4;
488                        if &self.input[start..end] == "type" {
489                            let _ = self.parse_optional_attr_value("type");
490                            match self.parse_datatype_name()?.as_str() {
491                                "boolean" => datatype = SimpleDatatype::Boolean,
492                                "decimal" => datatype = SimpleDatatype::Decimal,
493                                "double" => datatype = SimpleDatatype::Double,
494                                "float" => datatype = SimpleDatatype::Float,
495                                "integer" => datatype = SimpleDatatype::Integer,
496                                "string" => datatype = SimpleDatatype::String,
497                                _ => return Err(format!("Unsupported datatype")),
498                            }
499                            break;
500                        }
501                    }
502                    _ => {}
503                },
504                None => return Err(format!("Unexpected end of input")),
505            }
506        }
507        simple_type.datatype = datatype;
508        Ok(simple_type)
509    }
510    /// Parses the name of an XML datatype from the input stream.
511    ///
512    /// This function reads the input stream character by character, parsing the name of an XML datatype
513    /// until it encounters a whitespace character, a closing angle bracket '>', or a colon ':'. The
514    /// function returns the parsed datatype name as a `String` wrapped in an `Ok` variant of the
515    /// `Result` type, or an error message in the form of a `String` wrapped in an `Err` variant of the
516    /// `Result` type.
517    ///
518    /// # Arguments
519    ///
520    /// * `self` - A mutable reference to an object that implements the `parse_datatype_name()` function.
521    ///
522    /// # Examples
523    ///
524    /// ```
525    /// let mut parser = XmlSchemaParser::new(input);
526    /// let result = parser.parse_datatype_name();
527    /// match result {
528    ///     Ok(datatype_name) => {
529    ///         // Do something with the parsed datatype name
530    ///     }
531    ///     Err(message) => {
532    ///         eprintln!("Error parsing datatype name: {}", message);
533    ///     }
534    /// }
535    /// ```
536    ///
537    fn parse_datatype_name(&mut self) -> Result<String, String> {
538        let mut datatype_name = String::new();
539        while let Some(ch) = self.next_char() {
540            match ch {
541                ':' => continue,
542                '>' => break,
543                ' ' => break,
544                _ => datatype_name.push(ch),
545            }
546        }
547        Ok(datatype_name)
548    }
549    /// Parses an `xs:complexType` XML element from the input stream.
550    ///
551    /// This function reads the input stream character by character, parsing the name and attributes
552    /// of an `xs:complexType` XML element, as well as any child elements that define the structure
553    /// and content of the complex type. The function returns a `ComplexType` object containing the
554    /// parsed data, wrapped in an `Ok` variant of the `Result` type, or an error message in the form
555    /// of a `String` wrapped in an `Err` variant of the `Result` type.
556    ///
557    /// # Arguments
558    ///
559    /// * `self` - A mutable reference to an object that implements the `parse_complex_type()` function.
560    ///
561    /// # Examples
562    ///
563    /// ```
564    /// let mut parser = XmlSchemaParser::new(input);
565    /// let result = parser.parse_complex_type();
566    /// match result {
567    ///     Ok(complex_type) => {
568    ///         // Do something with the parsed complex type
569    ///     }
570    ///     Err(message) => {
571    ///         eprintln!("Error parsing complex type: {}", message);
572    ///     }
573    /// }
574    /// ```
575    ///
576    fn parse_complex_type(&mut self) -> Result<ComplexType, String> {
577        let mut complex_type = ComplexType {
578            name: String::new(),
579            base_type: None,
580            attributes: HashMap::new(),
581            content: ComplexContent::Empty,
582            mixed_content: None,
583        };
584
585        while let Some(ch) = self.next_char() {
586            match ch {
587                '>' => break,
588                ' ' => break,
589                _ => complex_type.name.push(ch),
590            }
591        }
592
593        while let Some(ch) = self.next_char() {
594            match ch {
595                '<' => {
596                    let tag_name = self.parse_tag_name()?;
597                    match tag_name.as_str() {
598                        "xs:simpleContent" => {
599                            complex_type.content = ComplexContent::SimpleContent;
600                        }
601                        "xs:complexContent" => {
602                            complex_type.content = ComplexContent::ComplexContentExtension;
603                        }
604                        "xs:mixedContent" => {
605                            complex_type.content = ComplexContent::MixedContent;
606                            while let Some(ch) = self.next_char() {
607                                if ch == '>' {
608                                    break;
609                                }
610                            }
611                            break;
612                        }
613                        "xs:sequence" => {
614                            complex_type.content = ComplexContent::Sequence;
615                        }
616                        "xs:choice" => {
617                            complex_type.content = ComplexContent::Choice;
618                        }
619                        "xs:all" => {
620                            complex_type.content = ComplexContent::All;
621                        }
622                        _ => {
623                            if tag_name == "xs:complexType" {
624                                let mixed_attr = self.parse_optional_attr_value("mixed");
625                                if mixed_attr == Some("true".to_string()) {
626                                    complex_type.content = ComplexContent::MixedContent;
627                                    while let Some(ch) = self.next_char() {
628                                        if ch == '>' {
629                                            break;
630                                        }
631                                    }
632                                    break;
633                                }
634                            }
635                            return Err(format!("Unexpected tag: {}", tag_name));
636                        }
637                    }
638                }
639                ' ' => continue,
640                '>' => break,
641                _ => return Err(format!("Unexpected character: {}", ch)),
642            }
643        }
644
645        Ok(complex_type)
646    }
647
648    fn parse_sequence(&mut self) -> Result<Vec<XmlSchemaNode>, String> {
649        let mut nodes = Vec::new();
650        while let Some(ch) = self.next_char() {
651            match ch {
652                '<' => {
653                    let tag_name = self.parse_tag_name()?;
654                    match tag_name.as_str() {
655                        "xs:element" => {
656                            nodes.push(XmlSchemaNode::Element(self.parse_element()?));
657                        }
658                        "xs:complexType" => {
659                            nodes.push(XmlSchemaNode::ComplexType(self.parse_complex_type()?));
660                        }
661                        "xs:simpleType" => {
662                            nodes.push(XmlSchemaNode::SimpleType(self.parse_simple_type()?));
663                        }
664                        _ => {
665                            return Err(format!("Unexpected tag: {}", tag_name));
666                        }
667                    }
668                }
669                ' ' => continue,
670                '>' => break,
671                _ => return Err(format!("Unexpected character: {}", ch)),
672            }
673        }
674        Ok(nodes)
675    }
676    /// Parses an `xs:choice` XML element from the input stream.
677    ///
678    /// This function reads the input stream character by character, parsing the child elements of an
679    /// `xs:choice` XML element. The function returns a vector of `XmlSchemaNode` objects containing
680    /// the parsed data, wrapped in an `Ok` variant of the `Result` type, or an error message in the
681    /// form of a `String` wrapped in an `Err` variant of the `Result` type.
682    ///
683    /// # Arguments
684    ///
685    /// * `self` - A mutable reference to an object that implements the `parse_choice()` function.
686    ///
687    /// # Examples
688    ///
689    /// ```
690    /// let mut parser = XmlSchemaParser::new(input);
691    /// let result = parser.parse_choice();
692    /// match result {
693    ///     Ok(nodes) => {
694    ///         // Do something with the parsed choice elements
695    ///     }
696    ///     Err(message) => {
697    ///         eprintln!("Error parsing choice elements: {}", message);
698    ///     }
699    /// }
700    /// ```
701    ///
702    fn parse_choice(&mut self) -> Result<Vec<XmlSchemaNode>, String> {
703        let mut nodes = Vec::new();
704        while let Some(ch) = self.next_char() {
705            match ch {
706                '<' => {
707                    let tag_name = self.parse_tag_name()?;
708                    match tag_name.as_str() {
709                        "xs:element" => {
710                            nodes.push(XmlSchemaNode::Element(self.parse_element()?));
711                        }
712                        "xs:complexType" => {
713                            nodes.push(XmlSchemaNode::ComplexType(self.parse_complex_type()?));
714                        }
715                        "xs:simpleType" => {
716                            nodes.push(XmlSchemaNode::SimpleType(self.parse_simple_type()?));
717                        }
718                        _ => {
719                            return Err(format!("Unexpected tag: {}", tag_name));
720                        }
721                    }
722                }
723                ' ' => continue,
724                '>' => break,
725                _ => return Err(format!("Unexpected character: {}", ch)),
726            }
727        }
728        Ok(nodes)
729    }
730    /// Parses an `xs:all` XML element from the input stream.
731    ///
732    /// This function reads the input stream character by character, parsing the child elements of an
733    /// `xs:all` XML element. The function returns a vector of `XmlSchemaNode` objects containing the
734    /// parsed data, wrapped in an `Ok` variant of the `Result` type, or an error message in the form
735    /// of a `String` wrapped in an `Err` variant of the `Result` type.
736    ///
737    /// # Arguments
738    ///
739    /// * `self` - A mutable reference to an object that implements the `parse_all()` function.
740    ///
741    /// # Examples
742    ///
743    /// ```
744    /// let mut parser = XmlSchemaParser::new(input);
745    /// let result = parser.parse_all();
746    /// match result {
747    ///     Ok(nodes) => {
748    ///         // Do something with the parsed all elements
749    ///     }
750    ///     Err(message) => {
751    ///         eprintln!("Error parsing all elements: {}", message);
752    ///     }
753    /// }
754    /// ```
755    ///
756    fn parse_all(&mut self) -> Result<Vec<XmlSchemaNode>, String> {
757        let mut nodes = Vec::new();
758        while let Some(ch) = self.next_char() {
759            match ch {
760                '<' => {
761                    let tag_name = self.parse_tag_name()?;
762                    match tag_name.as_str() {
763                        "xs:element" => {
764                            nodes.push(XmlSchemaNode::Element(self.parse_element()?));
765                        }
766                        "xs:complexType" => {
767                            nodes.push(XmlSchemaNode::ComplexType(self.parse_complex_type()?));
768                        }
769                        "xs:simpleType" => {
770                            nodes.push(XmlSchemaNode::SimpleType(self.parse_simple_type()?));
771                        }
772                        _ => {
773                            return Err(format!("Unexpected tag: {}", tag_name));
774                        }
775                    }
776                }
777                ' ' => continue,
778                '>' => break,
779                _ => return Err(format!("Unexpected character: {}", ch)),
780            }
781        }
782        Ok(nodes)
783    }
784    /// Parses an `xs:simpleContent` XML element from the input stream.
785    ///
786    /// This function reads the input stream character by character, parsing the child elements of an
787    /// `xs:simpleContent` XML element. The function returns a `SimpleContent` object containing the
788    /// parsed data, wrapped in an `Ok` variant of the `Result` type, or an error message in the form
789    /// of a `String` wrapped in an `Err` variant of the `Result` type.
790    ///
791    /// # Arguments
792    ///
793    /// * `self` - A mutable reference to an object that implements the `parse_simple_content()` function.
794    ///
795    /// # Examples
796    ///
797    /// ```
798    /// let mut parser = XmlSchemaParser::new(input);
799    /// let result = parser.parse_simple_content();
800    /// match result {
801    ///     Ok(simple_content) => {
802    ///         // Do something with the parsed simple content
803    ///     }
804    ///     Err(message) => {
805    ///         eprintln!("Error parsing simple content: {}", message);
806    ///     }
807    /// }
808    /// ```
809    ///
810    fn parse_simple_content(&mut self) -> Result<SimpleContent, String> {
811        let mut simple_content = SimpleContent {
812            datatype: SimpleDatatype::String,
813        };
814
815        while let Some(ch) = self.next_char() {
816            match ch {
817                '>' => break,
818                ' ' => break,
819                _ => {}
820            }
821        }
822
823        let mut datatype = SimpleDatatype::String;
824        loop {
825            match self.next_char() {
826                Some(ch) => match ch {
827                    '>' => break,
828                    ' ' => {}
829                    's' => {
830                        let start = self.position - 1;
831                        let end = start + 4;
832                        if &self.input[start..end] == "type" {
833                            let _ = self.parse_optional_attr_value("type");
834                            match self.parse_datatype_name()?.as_str() {
835                                "boolean" => datatype = SimpleDatatype::Boolean,
836                                "decimal" => datatype = SimpleDatatype::Decimal,
837                                "double" => datatype = SimpleDatatype::Double,
838                                "float" => datatype = SimpleDatatype::Float,
839                                "integer" => datatype = SimpleDatatype::Integer,
840                                "string" => datatype = SimpleDatatype::String,
841                                _ => return Err(format!("Unsupported datatype")),
842                            }
843                            break;
844                        }
845                    }
846                    _ => {}
847                },
848                None => return Err(format!("Unexpected end of input")),
849            }
850        }
851        simple_content.datatype = datatype;
852
853        while let Some(ch) = self.next_char() {
854            match ch {
855                '<' => break,
856                _ => {}
857            }
858        }
859
860        Ok(simple_content)
861    }
862    /// Parses an `xs:complexContent` extension XML element from the input stream.
863    ///
864    /// This function reads the input stream character by character, parsing the child elements of an
865    /// `xs:complexContent` extension XML element. The function returns a `ComplexContentExtension`
866    /// object containing the parsed data, wrapped in an `Ok` variant of the `Result` type, or an error
867    /// message in the form of a `String` wrapped in an `Err` variant of the `Result` type.
868    ///
869    /// # Arguments
870    ///
871    /// * `self` - A mutable reference to an object that implements the `parse_complex_content_extension()`
872    ///            function.
873    ///
874    /// # Examples
875    ///
876    /// ```
877    /// let mut parser = XmlSchemaParser::new(input);
878    /// let result = parser.parse_complex_content_extension();
879    /// match result {
880    ///     Ok(complex_content_extension) => {
881    ///         // Do something with the parsed complex content extension
882    ///     }
883    ///     Err(message) => {
884    ///         eprintln!("Error parsing complex content extension: {}", message);
885    ///     }
886    /// }
887    /// ```
888    ///
889    fn parse_complex_content_extension(&mut self) -> Result<ComplexContentExtension, String> {
890        let mut complex_content_extension = ComplexContentExtension {
891            base_type: String::new(),
892            attributes: HashMap::new(),
893        };
894
895        while let Some(ch) = self.next_char() {
896            match ch {
897                '>' => break,
898                ' ' => break,
899                _ => {}
900            }
901        }
902
903        while let Some(ch) = self.next_char() {
904            match ch {
905                ' ' => continue,
906                'b' => {
907                    let start = self.position - 1;
908                    let end = start + 8;
909                    if &self.input[start..end] == "base=\"xs" {
910                        let base_type = self.parse_datatype_name()?;
911                        complex_content_extension.base_type = base_type;
912                        break;
913                    } else {
914                        return Err(format!("Unexpected character: {}", ch));
915                    }
916                }
917                _ => return Err(format!("Unexpected character: {}", ch)),
918            }
919        }
920        Ok(complex_content_extension)
921    }
922}
923
924impl Display for SimpleDatatype {
925    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
926        match self {
927            SimpleDatatype::Boolean => write!(f, "boolean"),
928            SimpleDatatype::Decimal => write!(f, "decimal"),
929            SimpleDatatype::Double => write!(f, "double"),
930            SimpleDatatype::Float => write!(f, "float"),
931            SimpleDatatype::Integer => write!(f, "integer"),
932            SimpleDatatype::String => write!(f, "string"),
933        }
934    }
935}