@@ -30,6 +30,82 @@ pub struct Attribute<'a> {
30
30
}
31
31
32
32
impl < ' 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
+
33
109
/// Returns the unescaped value.
34
110
///
35
111
/// This is normally the value you are interested in. Escape sequences such as `>` are
@@ -130,22 +206,19 @@ impl<'a> Debug for Attribute<'a> {
130
206
131
207
impl < ' a > From < ( & ' a [ u8 ] , & ' a [ u8 ] ) > for Attribute < ' a > {
132
208
/// 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 .
134
210
///
135
211
/// # Examples
136
212
///
137
213
/// ```
138
214
/// # use pretty_assertions::assert_eq;
139
215
/// use quick_xml::events::attributes::Attribute;
140
216
///
141
- /// let features = Attribute::from(("features".as_bytes(), "Bells & whistles".as_bytes()));
217
+ /// let features = Attribute::from(("features".as_bytes(), "Bells & whistles".as_bytes()));
142
218
/// assert_eq!(features.value, "Bells & whistles".as_bytes());
143
219
/// ```
144
220
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 )
149
222
}
150
223
}
151
224
@@ -163,10 +236,7 @@ impl<'a> From<(&'a str, &'a str)> for Attribute<'a> {
163
236
/// assert_eq!(features.value, "Bells & whistles".as_bytes());
164
237
/// ```
165
238
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 )
170
240
}
171
241
}
172
242
0 commit comments