1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
use std::collections::HashMap; use std::io::Write; use std::str; use quick_xml::errors::Error as XmlError; use quick_xml::events::{Event, BytesStart, BytesEnd, BytesText}; use quick_xml::writer::Writer; use toxml::ToXml; pub(crate) mod util; /// A map of extension namespace prefixes to local names to elements. pub type ExtensionMap = HashMap<String, HashMap<String, Vec<Extension>>>; /// A namespaced extension. #[derive(Debug, Default, Clone, PartialEq, Builder)] #[builder(setter(into), default)] pub struct Extension { /// The qualified name of the extension element. name: String, /// The content of the extension element. value: Option<String>, /// The attributes for the extension element. attrs: HashMap<String, String>, /// The children of the extension element. A map of local names to child elements. children: HashMap<String, Vec<Extension>>, } impl Extension { /// Return the qualified name of this extension. /// /// # Examples /// /// ``` /// use atom_syndication::extension::Extension; /// /// let mut extension = Extension::default(); /// extension.set_name("ext:name"); /// assert_eq!(extension.name(), "ext:name"); /// ``` pub fn name(&self) -> &str { self.name.as_str() } /// Set the qualified name of this extension. /// /// # Examples /// /// ``` /// use atom_syndication::extension::Extension; /// /// let mut extension = Extension::default(); /// extension.set_name("ext:name"); /// ``` pub fn set_name<V>(&mut self, name: V) where V: Into<String>, { self.name = name.into(); } /// Return the text content of this extension. /// /// # Examples /// /// ``` /// use atom_syndication::extension::Extension; /// /// let mut extension = Extension::default(); /// extension.set_value("John Doe".to_string()); /// assert_eq!(extension.value(), Some("John Doe")); /// ``` pub fn value(&self) -> Option<&str> { self.value.as_ref().map(|s| s.as_str()) } /// Set the text content of this extension. /// /// # Examples /// /// ``` /// use atom_syndication::extension::Extension; /// /// let mut extension = Extension::default(); /// extension.set_value("John Doe".to_string()); /// ``` pub fn set_value<V>(&mut self, value: V) where V: Into<Option<String>>, { self.value = value.into(); } /// Return the attributes for the extension element. /// /// # Examples /// /// ``` /// use std::collections::HashMap; /// use atom_syndication::extension::Extension; /// /// let mut extension = Extension::default(); /// let mut attrs = HashMap::<String, String>::new(); /// attrs.insert("email".to_string(), "johndoe@example.com".to_string()); /// extension.set_attrs(attrs.clone()); /// assert_eq!(*extension.attrs(), attrs); /// ``` pub fn attrs(&self) -> &HashMap<String, String> { &self.attrs } /// Set the attributes for the extension element. /// /// # Examples /// /// ``` /// use std::collections::HashMap; /// use atom_syndication::extension::Extension; /// /// let mut extension = Extension::default(); /// extension.set_attrs(HashMap::new()); /// ``` pub fn set_attrs<V>(&mut self, attrs: V) where V: Into<HashMap<String, String>>, { self.attrs = attrs.into(); } /// Return the children of the extension element. /// /// A map of local names to child elements. /// /// # Examples /// /// ``` /// use std::collections::HashMap; /// use atom_syndication::extension::Extension; /// /// let mut extension = Extension::default(); /// let mut children = HashMap::<String, Vec<Extension>>::new(); /// children.insert("ext:child".to_string(), Vec::new()); /// extension.set_children(children); /// assert!(extension.children().contains_key("ext:child")); /// ``` pub fn children(&self) -> &HashMap<String, Vec<Extension>> { &self.children } /// Set the children of the extension element. /// /// A map of local names to child elements. /// /// # Examples /// /// ``` /// use std::collections::HashMap; /// use atom_syndication::extension::Extension; /// /// let mut extension = Extension::default(); /// extension.set_children(HashMap::new()); /// ``` pub fn set_children<V>(&mut self, children: V) where V: Into<HashMap<String, Vec<Extension>>>, { self.children = children.into(); } } impl ToXml for Extension { fn to_xml<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), XmlError> { let name = self.name.as_bytes(); let mut element = BytesStart::borrowed(name, name.len()); element.extend_attributes(self.attrs.iter().map(|a| (a.0.as_bytes(), a.1.as_bytes()))); writer.write_event(Event::Start(element))?; if let Some(value) = self.value.as_ref() { writer .write_event(Event::Text(BytesText::borrowed(value.as_bytes())))?; } for extension in self.children.values().flat_map(|extensions| extensions) { extension.to_xml(writer)?; } writer.write_event(Event::End(BytesEnd::borrowed(name)))?; Ok(()) } }