@@ -32,6 +32,82 @@ pub struct Attribute<'a> {
3232}
3333
3434impl < ' a > Attribute < ' a > {
35+ /// Creates new attribute from text representation.
36+ /// Key is stored as-is, but the value will be escaped.
37+ ///
38+ /// # Examples
39+ ///
40+ /// ```
41+ /// # use pretty_assertions::assert_eq;
42+ /// use quick_xml::events::attributes::Attribute;
43+ ///
44+ /// let features = Attribute::from_str("features", "Bells & whistles");
45+ /// assert_eq!(features.value, "Bells & whistles".as_bytes());
46+ /// ```
47+ pub fn from_str ( key : & ' a str , val : & ' a str ) -> Attribute < ' a > {
48+ Attribute {
49+ key : QName ( key. as_bytes ( ) ) ,
50+ value : escape ( val. as_bytes ( ) ) ,
51+ }
52+ }
53+
54+ /// Creates new attribute from text representation.
55+ /// Does not apply any transformation to either key or value.
56+ ///
57+ /// # Examples
58+ ///
59+ /// ```
60+ /// # use pretty_assertions::assert_eq;
61+ /// use quick_xml::events::attributes::Attribute;
62+ ///
63+ /// let features = Attribute::from_escaped_str("features", "Bells & whistles");
64+ /// assert_eq!(features.value, "Bells & whistles".as_bytes());
65+ /// ```
66+ pub fn from_escaped_str ( key : & ' a str , val : & ' a str ) -> Attribute < ' a > {
67+ Attribute {
68+ key : QName ( key. as_bytes ( ) ) ,
69+ value : Cow :: from ( val. as_bytes ( ) ) ,
70+ }
71+ }
72+
73+ /// Creates new attribute from raw bytes.
74+ /// Key is stored as-is, but the value will be escaped.
75+ ///
76+ /// # Examples
77+ ///
78+ /// ```
79+ /// # use pretty_assertions::assert_eq;
80+ /// use quick_xml::events::attributes::Attribute;
81+ ///
82+ /// let features = Attribute::from_bytes("features".as_bytes(), "Bells & whistles".as_bytes());
83+ /// assert_eq!(features.value, "Bells & whistles".as_bytes());
84+ /// ```
85+ pub fn from_bytes ( key : & ' a [ u8 ] , val : & ' a [ u8 ] ) -> Attribute < ' a > {
86+ Attribute {
87+ key : QName ( key) ,
88+ value : escape ( val) ,
89+ }
90+ }
91+
92+ /// Creates new attribute from raw bytes.
93+ /// Does not apply any transformation to either key or value.
94+ ///
95+ /// # Examples
96+ ///
97+ /// ```
98+ /// # use pretty_assertions::assert_eq;
99+ /// use quick_xml::events::attributes::Attribute;
100+ ///
101+ /// let features = Attribute::from_escaped_bytes("features".as_bytes(), "Bells & whistles".as_bytes());
102+ /// assert_eq!(features.value, "Bells & whistles".as_bytes());
103+ /// ```
104+ pub fn from_escaped_bytes ( key : & ' a [ u8 ] , val : & ' a [ u8 ] ) -> Attribute < ' a > {
105+ Attribute {
106+ key : QName ( key) ,
107+ value : Cow :: from ( val) ,
108+ }
109+ }
110+
35111 /// Returns the unescaped value.
36112 ///
37113 /// This is normally the value you are interested in. Escape sequences such as `>` are
@@ -132,22 +208,19 @@ impl<'a> Debug for Attribute<'a> {
132208
133209impl < ' a > From < ( & ' a [ u8 ] , & ' a [ u8 ] ) > for Attribute < ' a > {
134210 /// Creates new attribute from raw bytes.
135- /// Does not apply any transformation to both key and value .
211+ /// Key is stored as-is, but the value will be escaped .
136212 ///
137213 /// # Examples
138214 ///
139215 /// ```
140216 /// # use pretty_assertions::assert_eq;
141217 /// use quick_xml::events::attributes::Attribute;
142218 ///
143- /// let features = Attribute::from(("features".as_bytes(), "Bells & whistles".as_bytes()));
219+ /// let features = Attribute::from(("features".as_bytes(), "Bells & whistles".as_bytes()));
144220 /// assert_eq!(features.value, "Bells & whistles".as_bytes());
145221 /// ```
146222 fn from ( val : ( & ' a [ u8 ] , & ' a [ u8 ] ) ) -> Attribute < ' a > {
147- Attribute {
148- key : QName ( val. 0 ) ,
149- value : Cow :: from ( val. 1 ) ,
150- }
223+ Attribute :: from_bytes ( val. 0 , val. 1 )
151224 }
152225}
153226
@@ -165,10 +238,7 @@ impl<'a> From<(&'a str, &'a str)> for Attribute<'a> {
165238 /// assert_eq!(features.value, "Bells & whistles".as_bytes());
166239 /// ```
167240 fn from ( val : ( & ' a str , & ' a str ) ) -> Attribute < ' a > {
168- Attribute {
169- key : QName ( val. 0 . as_bytes ( ) ) ,
170- value : escape ( val. 1 . as_bytes ( ) ) ,
171- }
241+ Attribute :: from_str ( val. 0 , val. 1 )
172242 }
173243}
174244
0 commit comments