Skip to content

Using attribute-from-tuple conversion can be error-prone #410

Open
@dralley

Description

@dralley

Creating an attribute from a (&[u8], &[u8]) performs no transformation (escaping) on the provided value, but creating an attribute from (&str, &str) does.

This feels a bit too implicit. It should probably be treated like BytesText instead, where different methods are provided depending on whether the value has been pre-escaped. https://docs.rs/quick-xml/latest/quick_xml/events/struct.BytesText.html

use quick_xml::events::attributes::Attribute;

fn main() {
    // correct
    println!("{:#?}", Attribute::from(("foo", "Bells & Whistles")));
    println!("{:#?}", Attribute::from(("foo".as_bytes(), "Bells & Whistles".as_bytes())));

    // incorrect
    println!("{:#?}", Attribute::from(("foo", "Bells & Whistles")));
    println!("{:#?}", Attribute::from(("foo".as_bytes(), "Bells & Whistles".as_bytes())));
}
Attribute { key: "foo", value: Owned("Bells & Whistles") }
Attribute { key: "foo", value: Borrowed("Bells & Whistles") }
Attribute { key: "foo", value: Owned("Bells & Whistles") }
Attribute { key: "foo", value: Borrowed("Bells & Whistles") }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions