@@ -30,6 +30,82 @@ pub struct Attribute<'a> {
3030}
3131
3232impl < ' a > Attribute < ' a > {
33+ /// Creates new attribute from text representation.
34+ /// Key is stored as-is, but the value will be escaped.
35+ ///
36+ /// # Examples
37+ ///
38+ /// ```
39+ /// # use pretty_assertions::assert_eq;
40+ /// use quick_xml::events::attributes::Attribute;
41+ ///
42+ /// let features = Attribute::from_str("features", "Bells & whistles");
43+ /// assert_eq!(features.value, "Bells & whistles".as_bytes());
44+ /// ```
45+ pub fn from_str ( key : & ' a str , val : & ' a str ) -> Attribute < ' a > {
46+ Attribute {
47+ key : QName ( key. as_bytes ( ) ) ,
48+ value : escape ( val. as_bytes ( ) ) ,
49+ }
50+ }
51+
52+ /// Creates new attribute from text representation.
53+ /// Does not apply any transformation to either key or value.
54+ ///
55+ /// # Examples
56+ ///
57+ /// ```
58+ /// # use pretty_assertions::assert_eq;
59+ /// use quick_xml::events::attributes::Attribute;
60+ ///
61+ /// let features = Attribute::from_escaped_str("features", "Bells & whistles");
62+ /// assert_eq!(features.value, "Bells & whistles".as_bytes());
63+ /// ```
64+ pub fn from_escaped_str ( key : & ' a str , val : & ' a str ) -> Attribute < ' a > {
65+ Attribute {
66+ key : QName ( key. as_bytes ( ) ) ,
67+ value : Cow :: from ( val. as_bytes ( ) ) ,
68+ }
69+ }
70+
71+ /// Creates new attribute from raw bytes.
72+ /// Key is stored as-is, but the value will be escaped.
73+ ///
74+ /// # Examples
75+ ///
76+ /// ```
77+ /// # use pretty_assertions::assert_eq;
78+ /// use quick_xml::events::attributes::Attribute;
79+ ///
80+ /// let features = Attribute::from_bytes("features".as_bytes(), "Bells & whistles".as_bytes());
81+ /// assert_eq!(features.value, "Bells & whistles".as_bytes());
82+ /// ```
83+ pub fn from_bytes ( key : & ' a [ u8 ] , val : & ' a [ u8 ] ) -> Attribute < ' a > {
84+ Attribute {
85+ key : QName ( key) ,
86+ value : escape ( val) ,
87+ }
88+ }
89+
90+ /// Creates new attribute from raw bytes.
91+ /// Does not apply any transformation to either key or value.
92+ ///
93+ /// # Examples
94+ ///
95+ /// ```
96+ /// # use pretty_assertions::assert_eq;
97+ /// use quick_xml::events::attributes::Attribute;
98+ ///
99+ /// let features = Attribute::from_escaped_bytes("features".as_bytes(), "Bells & whistles".as_bytes());
100+ /// assert_eq!(features.value, "Bells & whistles".as_bytes());
101+ /// ```
102+ pub fn from_escaped_bytes ( key : & ' a [ u8 ] , val : & ' a [ u8 ] ) -> Attribute < ' a > {
103+ Attribute {
104+ key : QName ( key) ,
105+ value : Cow :: from ( val) ,
106+ }
107+ }
108+
33109 /// Returns the unescaped value.
34110 ///
35111 /// This is normally the value you are interested in. Escape sequences such as `>` are
@@ -130,22 +206,19 @@ impl<'a> Debug for Attribute<'a> {
130206
131207impl < ' a > From < ( & ' a [ u8 ] , & ' a [ u8 ] ) > for Attribute < ' a > {
132208 /// Creates new attribute from raw bytes.
133- /// Does not apply any transformation to both key and value .
209+ /// Key is stored as-is, but the value will be escaped .
134210 ///
135211 /// # Examples
136212 ///
137213 /// ```
138214 /// # use pretty_assertions::assert_eq;
139215 /// use quick_xml::events::attributes::Attribute;
140216 ///
141- /// let features = Attribute::from(("features".as_bytes(), "Bells & whistles".as_bytes()));
217+ /// let features = Attribute::from(("features".as_bytes(), "Bells & whistles".as_bytes()));
142218 /// assert_eq!(features.value, "Bells & whistles".as_bytes());
143219 /// ```
144220 fn from ( val : ( & ' a [ u8 ] , & ' a [ u8 ] ) ) -> Attribute < ' a > {
145- Attribute {
146- key : QName ( val. 0 ) ,
147- value : Cow :: from ( val. 1 ) ,
148- }
221+ Attribute :: from_bytes ( val. 0 , val. 1 )
149222 }
150223}
151224
@@ -163,10 +236,7 @@ impl<'a> From<(&'a str, &'a str)> for Attribute<'a> {
163236 /// assert_eq!(features.value, "Bells & whistles".as_bytes());
164237 /// ```
165238 fn from ( val : ( & ' a str , & ' a str ) ) -> Attribute < ' a > {
166- Attribute {
167- key : QName ( val. 0 . as_bytes ( ) ) ,
168- value : escape ( val. 1 . as_bytes ( ) ) ,
169- }
239+ Attribute :: from_str ( val. 0 , val. 1 )
170240 }
171241}
172242
0 commit comments