pub struct Root<T> {
pub name: Option<String>,
pub namespaces: Vec<(String, String)>,
pub value: T,
}Expand description
A document: the value of the root element with its name and the namespaces declared on it.
When deserialized the name of the root element and its namespace
declarations are captured, when serialized the root element is written
with them. The name is kept as the deserializer passes on names: as
written, with the configured prefix of its namespace or as {uri}local
if namespaces are resolved (see DeserializerConfig). The prefixes
of the namespaces are the ones the names use (the configured prefixes
for namespaces that have one).
use std::collections::BTreeMap;
use deser_xml::Root;
let doc: Root<BTreeMap<String, String>> =
deser_xml::from_str(r#"<rss xmlns:dc="urn:dc"><dc:creator>Jane</dc:creator></rss>"#)
.unwrap();
assert_eq!(doc.name.as_deref(), Some("rss"));
assert_eq!(doc.namespaces, [("dc".to_string(), "urn:dc".to_string())]);
assert_eq!(doc.value["dc:creator"], "Jane");
// values without a name (like maps) are named by the root
let doc = Root::new("r", BTreeMap::from([("@a", 1), ("b", 2)]));
assert_eq!(deser_xml::to_string(&doc).unwrap(), r#"<r a="1"><b>2</b></r>"#);The name of the root is used over the name of the type of the value
and the configured name, its namespaces
are declared before the configured
ones (which are left out if their
prefix is taken). Only documents have a root: in other places, and when
serializing to other formats, the name and the namespaces are ignored,
when deserializing from other formats the name is None and there are
no namespaces.
Fields§
§name: Option<String>The name of the root element.
namespaces: Vec<(String, String)>The namespaces declared on the root element as prefix and URI (the empty prefix is the default namespace).
value: TThe value of the root element.
Implementations§
Source§impl<T> Root<T>
impl<T> Root<T>
Sourcepub fn with_namespace<P: Into<String>, U: Into<String>>(
self,
prefix: P,
uri: U,
) -> Root<T>
pub fn with_namespace<P: Into<String>, U: Into<String>>( self, prefix: P, uri: U, ) -> Root<T>
Declares a namespace on the root element.
use deser_xml::Root;
let doc = Root::new("{urn:a}doc", "x").with_namespace("a", "urn:a");
assert_eq!(
deser_xml::to_string(&doc).unwrap(),
r#"<a:doc xmlns:a="urn:a">x</a:doc>"#
);Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Returns the value of the root element.