Skip to content

Commit

Permalink
Don't require both attribute key and value to be of same type
Browse files Browse the repository at this point in the history
The key value isn't escaped, so we can use AsRef to allow either value.

Therefore it is unnecessary to convert both parts of the tuple to the
same type in order to produce an Attribute
  • Loading branch information
dralley committed Mar 24, 2021
1 parent 4c11c60 commit 79e1ac1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
34 changes: 20 additions & 14 deletions src/events/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,42 +291,48 @@ impl<'a> std::fmt::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.
impl<'a, A> From<(&'a A, &'a str)> for Attribute<'a>
where
A: AsRef<[u8]> + ?Sized,
{
/// Creates new attribute from text representation.
/// Key is stored as-is, but the value will be escaped.
///
/// # Examples
///
/// ```
/// use quick_xml::events::attributes::Attribute;
///
/// let features = Attribute::from(("features".as_bytes(), "Bells &amp; whistles".as_bytes()));
/// let features = Attribute::from(("features", "Bells & whistles"));
/// assert_eq!(features.value, "Bells &amp; whistles".as_bytes());
/// ```
fn from(val: (&'a [u8], &'a [u8])) -> Attribute<'a> {
fn from(val: (&'a A, &'a str)) -> Attribute<'a> {
Attribute {
key: val.0,
value: Cow::from(val.1),
key: val.0.as_ref(),
value: escape(val.1.as_bytes()),
}
}
}

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.
impl<'a, A> From<(&'a A, &'a [u8])> for Attribute<'a>
where
A: AsRef<[u8]> + ?Sized,
{
/// Creates new attribute from raw bytes.
/// Does not apply any transformation to both key and value.
///
/// # Examples
///
/// ```
/// use quick_xml::events::attributes::Attribute;
///
/// let features = Attribute::from(("features", "Bells & whistles"));
/// 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 str, &'a str)) -> Attribute<'a> {
fn from(val: (&'a A, &'a [u8])) -> Attribute<'a> {
Attribute {
key: val.0.as_bytes(),
value: escape(val.1.as_bytes()),
key: val.0.as_ref(),
value: Cow::from(val.1),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/writer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A module to handle `Writer`
use crate::errors::{Error, Result};
use crate::events::{attributes::Attribute, BytesEnd, BytesStart, BytesText, Event};
use crate::events::{attributes::Attribute, BytesStart, BytesText, Event};
use std::io::Write;

/// XML writer.
Expand Down

0 comments on commit 79e1ac1

Please sign in to comment.