ilex_xml/
item.rs

1use std::fmt::Display;
2
3use quick_xml::events::Event;
4
5use crate::{util::GetEvents, Element, Other, ToStringSafe};
6
7/** Any XML item. */
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum Item<'a> {
10    /** Element ```<tag attr="value">...</tag>``` or ```<tag attr="value" />```. */
11    Element(Element<'a>),
12    /** Comment ```<!-- ... -->```. */
13    Comment(Other<'a>),
14    /** Escaped character data between tags. */
15    Text(Other<'a>),
16    /** Document type definition data (DTD) stored in ```<!DOCTYPE ...>```. */
17    DocType(Other<'a>),
18    /** Unescaped character data stored in ```<![CDATA[...]]>```. */
19    CData(Other<'a>),
20    /** XML declaration ```<?xml ...?>```. */
21    Decl(Other<'a>),
22    /** Processing instruction ```<?...?>```. */
23    PI(Other<'a>),
24}
25
26impl<'a> Item<'a> {
27    /** Create a new Element. */
28    pub fn new_element(name: &'a str, self_closing: bool) -> Self {
29        Item::Element(Element::new(name, self_closing))
30    }
31
32    /** Create a new comment item. */
33    pub fn new_comment(content: &'a str) -> Self {
34        Item::Comment(Other::new_comment(content))
35    }
36
37    /** Create a new text item. */
38    pub fn new_text(content: &'a str) -> Self {
39        Item::Text(Other::new_text(content))
40    }
41
42    /** Create a new doctype item. */
43    pub fn new_doctype(content: &'a str) -> Self {
44        Item::DocType(Other::new_doctype(content))
45    }
46
47    /** Create a new character data item. */
48    pub fn new_cdata(content: &'a str) -> Self {
49        Item::CData(Other::new_cdata(content))
50    }
51
52    /** Create a new declaration item. */
53    pub fn new_decl(version: &str, encoding: Option<&str>, standalone: Option<&str>) -> Self {
54        Item::Decl(Other::new_decl(version, encoding, standalone))
55    }
56
57    /** Create a new processing instruction item. */
58    pub fn new_pi(content: &'a str) -> Self {
59        Item::PI(Other::new_pi(content))
60    }
61}
62
63impl ToStringSafe for Item<'_> {
64    fn to_string_safe(&self) -> Result<String, crate::Error> {
65        match self {
66            Item::Element(element) => element.to_string_safe(),
67            Item::Comment(comment) => comment.to_string_safe(),
68            Item::Text(text) => text.to_string_safe(),
69            Item::DocType(doctype) => doctype.to_string_safe(),
70            Item::CData(cdata) => cdata.to_string_safe(),
71            Item::Decl(decl) => decl.to_string_safe(),
72            Item::PI(pi) => pi.to_string_safe(),
73        }
74    }
75}
76
77impl Display for Item<'_> {
78    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79        match self {
80            Item::Element(element) => element.fmt(f),
81            Item::Comment(comment) => comment.fmt(f),
82            Item::Text(text) => text.fmt(f),
83            Item::DocType(doctype) => doctype.fmt(f),
84            Item::CData(cdata) => cdata.fmt(f),
85            Item::Decl(decl) => decl.fmt(f),
86            Item::PI(pi) => pi.fmt(f),
87        }
88    }
89}
90
91impl GetEvents for Item<'_> {
92    fn get_all_events(&self) -> Box<dyn Iterator<Item = Event> + '_> {
93        match self {
94            Item::Element(element) => element.get_all_events(),
95            Item::Comment(comment) => comment.get_all_events(),
96            Item::Text(text) => text.get_all_events(),
97            Item::DocType(doctype) => doctype.get_all_events(),
98            Item::CData(cdata) => cdata.get_all_events(),
99            Item::Decl(decl) => decl.get_all_events(),
100            Item::PI(pi) => pi.get_all_events(),
101        }
102    }
103}