@@ -32,6 +32,82 @@ pub struct Attribute<'a> {
32
32
}
33
33
34
34
impl < ' 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
+
35
111
/// Returns the unescaped value.
36
112
///
37
113
/// This is normally the value you are interested in. Escape sequences such as `>` are
@@ -132,22 +208,19 @@ impl<'a> Debug for Attribute<'a> {
132
208
133
209
impl < ' a > From < ( & ' a [ u8 ] , & ' a [ u8 ] ) > for Attribute < ' a > {
134
210
/// 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 .
136
212
///
137
213
/// # Examples
138
214
///
139
215
/// ```
140
216
/// # use pretty_assertions::assert_eq;
141
217
/// use quick_xml::events::attributes::Attribute;
142
218
///
143
- /// let features = Attribute::from(("features".as_bytes(), "Bells & whistles".as_bytes()));
219
+ /// let features = Attribute::from(("features".as_bytes(), "Bells & whistles".as_bytes()));
144
220
/// assert_eq!(features.value, "Bells & whistles".as_bytes());
145
221
/// ```
146
222
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 )
151
224
}
152
225
}
153
226
@@ -165,10 +238,7 @@ impl<'a> From<(&'a str, &'a str)> for Attribute<'a> {
165
238
/// assert_eq!(features.value, "Bells & whistles".as_bytes());
166
239
/// ```
167
240
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 )
172
242
}
173
243
}
174
244
0 commit comments