-
Notifications
You must be signed in to change notification settings - Fork 0
feat(types): Adds method value and into_value to easily get values from XmlRoot<T>.
#153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…s from `XmlRoot<T>`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 | ||
| } | ||
| }) |
Copilot
AI
Jan 15, 2026
There was a problem hiding this comment.
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 }).
| 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 | ||
| } | ||
| }) |
Copilot
AI
Jan 15, 2026
There was a problem hiding this comment.
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.
| 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.