Skip to content

Commit

Permalink
Make attribute creation more uniform
Browse files Browse the repository at this point in the history
closes #410
  • Loading branch information
dralley committed Jul 9, 2022
1 parent 0febc2b commit 2b765ab
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@
Use methods on `Attribute` instead

- [#403]: Remove deprecated `quick_xml::de::from_bytes` and `Deserializer::from_borrowing_reader`
- [#413]: Removed the `Attribute::from((&[u8], &[u8]))` implementation, use
`Attribute::new(&[u8], &[u8])` instead.

### New Tests

Expand All @@ -117,6 +119,7 @@
[#395]: https://github.com/tafia/quick-xml/pull/395
[#403]: https://github.com/tafia/quick-xml/pull/403
[#407]: https://github.com/tafia/quick-xml/pull/407
[#413]: https://github.com/tafia/quick-xml/pull/413

## 0.23.0 -- 2022-05-08

Expand Down
40 changes: 19 additions & 21 deletions src/events/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ pub struct Attribute<'a> {
}

impl<'a> Attribute<'a> {
/// Creates new attribute from raw bytes.
/// Does not apply any transformation to either key or value.
///
/// # Examples
///
/// ```
/// # use pretty_assertions::assert_eq;
/// use quick_xml::events::attributes::Attribute;
///
/// let features = Attribute::new("features".as_bytes(), "Bells &amp; whistles".as_bytes());
/// assert_eq!(features.value, "Bells &amp; whistles".as_bytes());
/// ```
pub fn new(name: &'a [u8], val: &'a [u8]) -> Attribute<'a> {
Attribute {
key: QName(name),
value: Cow::from(val),
}
}

/// Returns the unescaped value.
///
/// This is normally the value you are interested in. Escape sequences such as `&gt;` are
Expand Down Expand Up @@ -130,27 +149,6 @@ impl<'a> Debug for Attribute<'a> {
}
}

impl<'a> From<(&'a [u8], &'a [u8])> for Attribute<'a> {
/// Creates new attribute from raw bytes.
/// Does not apply any transformation to both key and value.
///
/// # Examples
///
/// ```
/// # use pretty_assertions::assert_eq;
/// use quick_xml::events::attributes::Attribute;
///
/// let features = Attribute::from(("features".as_bytes(), "Bells &amp; whistles".as_bytes()));
/// assert_eq!(features.value, "Bells &amp; whistles".as_bytes());
/// ```
fn from(val: (&'a [u8], &'a [u8])) -> Attribute<'a> {
Attribute {
key: QName(val.0),
value: Cow::from(val.1),
}
}
}

impl<'a> From<(&'a str, &'a str)> for Attribute<'a> {
/// Creates new attribute from text representation.
/// Key is stored as-is, but the value will be escaped.
Expand Down
2 changes: 1 addition & 1 deletion src/se/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ where
self.children.append(&mut self.buffer);
} else {
self.attrs
.push_attribute((key.as_bytes(), self.buffer.as_ref()));
.push_attribute(Attribute::new(key.as_bytes(), self.buffer.as_ref()));
self.buffer.clear();
}
}
Expand Down

0 comments on commit 2b765ab

Please sign in to comment.