Conversation
…s from `XmlRoot<T>`.
There was a problem hiding this comment.
Pull request overview
This PR adds two convenience methods to the XmlRoot<T> type to make it easier to extract the first value element from an XML document.
Changes:
- Added
value()method to get a reference to the first value element - Added
into_value()method to consume theXmlRootand return the first value element - Added a working example demonstrating the usage of these new methods
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| xmlity/src/types/utils.rs | Implements value() and into_value() methods for XmlRoot<T> to extract the first value element |
| xmlity-quick-xml/examples/xml_root.rs | Adds an example demonstrating how to use the new into_value() method |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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 | ||
| } | ||
| }) |
There was a problem hiding this comment.
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 }).
| 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, | |
| }) |
| 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 | ||
| } | ||
| }) |
There was a problem hiding this comment.
Similar to the value() method, this pattern matching can be simplified. The explicit else { None } branch is redundant with find_map semantics.
| 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 |
No description provided.