Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions xmlity-quick-xml/examples/xml_root.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use xmlity::{types::utils::XmlRoot, Deserialize};

const EXAMPLE_XML_WITH_DECL: &str = r#"
<?xml version="1.0" encoding="UTF-8"?>
<root>
<value>Example</value>
</root>
"#;

#[derive(Debug, Deserialize, PartialEq)]
#[xelement(name = "root")]
struct Root {
#[xelement(name = "value")]
value: String,
}

pub fn main() {
let root = xmlity_quick_xml::from_str::<XmlRoot<Root>>(EXAMPLE_XML_WITH_DECL.trim())
.unwrap()
.into_value()
.unwrap();

assert_eq!(
root,
Root {
value: "Example".to_string(),
}
);

println!("Deserialized successfully: {:?}", root);
}
22 changes: 22 additions & 0 deletions xmlity/src/types/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,28 @@ impl<T> XmlRoot<T> {
);
self
}

/// Gets the first value element of the XML document, if any.
pub fn value(&self) -> Option<&T> {
self.elements.iter().find_map(|e| {
if let XmlRootTop::Value(v) = e {
Some(v)
} else {
None
}
})
}

/// Consumes the XML document and returns the first value element, if any.
pub fn into_value(self) -> Option<T> {
self.elements.into_iter().find_map(|e| {
if let XmlRootTop::Value(v) = e {
Some(v)
} else {
None
}
})
Comment on lines +287 to +304
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pattern matching can be simplified using matches! or a closure shorthand. The else { None } branch is redundant with find_map since non-matching items naturally return None. Consider using: self.elements.iter().find_map(|e| match e { XmlRootTop::Value(v) => Some(v), _ => None }) or even simpler: self.elements.iter().find_map(|e| if let XmlRootTop::Value(v) = e { Some(v) } else { None }).

Suggested change
self.elements.iter().find_map(|e| {
if let XmlRootTop::Value(v) = e {
Some(v)
} else {
None
}
})
}
/// Consumes the XML document and returns the first value element, if any.
pub fn into_value(self) -> Option<T> {
self.elements.into_iter().find_map(|e| {
if let XmlRootTop::Value(v) = e {
Some(v)
} else {
None
}
})
self.elements
.iter()
.find_map(|e| match e {
XmlRootTop::Value(v) => Some(v),
_ => None,
})
}
/// Consumes the XML document and returns the first value element, if any.
pub fn into_value(self) -> Option<T> {
self.elements
.into_iter()
.find_map(|e| match e {
XmlRootTop::Value(v) => Some(v),
_ => None,
})

Copilot uses AI. Check for mistakes.
Comment on lines +287 to +304
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the value() method, this pattern matching can be simplified. The explicit else { None } branch is redundant with find_map semantics.

Suggested change
self.elements.iter().find_map(|e| {
if let XmlRootTop::Value(v) = e {
Some(v)
} else {
None
}
})
}
/// Consumes the XML document and returns the first value element, if any.
pub fn into_value(self) -> Option<T> {
self.elements.into_iter().find_map(|e| {
if let XmlRootTop::Value(v) = e {
Some(v)
} else {
None
}
})
for e in &self.elements {
if let XmlRootTop::Value(v) = e {
return Some(v);
}
}
None
}
/// Consumes the XML document and returns the first value element, if any.
pub fn into_value(self) -> Option<T> {
for e in self.elements {
if let XmlRootTop::Value(v) = e {
return Some(v);
}
}
None

Copilot uses AI. Check for mistakes.
}
}

impl<T> Default for XmlRoot<T> {
Expand Down