@@ -48,6 +48,7 @@ use crate::encoding::Decoder;
48
48
use crate :: errors:: { Error , Result } ;
49
49
use crate :: escape:: { escape, partial_escape, unescape_with} ;
50
50
use crate :: name:: { LocalName , QName } ;
51
+ use crate :: reader:: Span ;
51
52
use crate :: utils:: write_cow_string;
52
53
use attributes:: { Attribute , Attributes } ;
53
54
@@ -67,15 +68,18 @@ pub struct BytesStart<'a> {
67
68
pub ( crate ) buf : Cow < ' a , [ u8 ] > ,
68
69
/// end of the element name, the name starts at that the start of `buf`
69
70
pub ( crate ) name_len : usize ,
71
+ /// the position of the element in the input, this does not reflect updates to the struct
72
+ pub ( crate ) span : Span ,
70
73
}
71
74
72
75
impl < ' a > BytesStart < ' a > {
73
76
/// Internal constructor, used by `Reader`. Supplies data in reader's encoding
74
77
#[ inline]
75
- pub ( crate ) fn wrap ( content : & ' a [ u8 ] , name_len : usize ) -> Self {
78
+ pub ( crate ) fn wrap ( content : & ' a [ u8 ] , name_len : usize , span : Span ) -> Self {
76
79
BytesStart {
77
80
buf : Cow :: Borrowed ( content) ,
78
81
name_len,
82
+ span,
79
83
}
80
84
}
81
85
@@ -89,6 +93,7 @@ impl<'a> BytesStart<'a> {
89
93
let buf = str_cow_to_bytes ( name) ;
90
94
BytesStart {
91
95
name_len : buf. len ( ) ,
96
+ span : 0 ..buf. len ( ) ,
92
97
buf,
93
98
}
94
99
}
@@ -102,8 +107,11 @@ impl<'a> BytesStart<'a> {
102
107
/// to generate invalid XML if `content` or `name_len` are incorrect.
103
108
#[ inline]
104
109
pub fn from_content < C : Into < Cow < ' a , str > > > ( content : C , name_len : usize ) -> Self {
110
+ let buf = str_cow_to_bytes ( content) ;
111
+
105
112
BytesStart {
106
- buf : str_cow_to_bytes ( content) ,
113
+ span : 0 ..buf. len ( ) ,
114
+ buf,
107
115
name_len,
108
116
}
109
117
}
@@ -113,6 +121,7 @@ impl<'a> BytesStart<'a> {
113
121
BytesStart {
114
122
buf : Cow :: Owned ( self . buf . into_owned ( ) ) ,
115
123
name_len : self . name_len ,
124
+ span : self . span ,
116
125
}
117
126
}
118
127
@@ -121,6 +130,7 @@ impl<'a> BytesStart<'a> {
121
130
BytesStart {
122
131
buf : Cow :: Owned ( self . buf . to_owned ( ) . into ( ) ) ,
123
132
name_len : self . name_len ,
133
+ span : self . span ( ) ,
124
134
}
125
135
}
126
136
@@ -153,6 +163,7 @@ impl<'a> BytesStart<'a> {
153
163
BytesStart {
154
164
buf : Cow :: Borrowed ( & self . buf ) ,
155
165
name_len : self . name_len ,
166
+ span : self . span ( ) ,
156
167
}
157
168
}
158
169
@@ -176,6 +187,17 @@ impl<'a> BytesStart<'a> {
176
187
self . name ( ) . into ( )
177
188
}
178
189
190
+ /// Gets the range of bytes this element spans in the input stream.
191
+ ///
192
+ /// This does not reflect updates to the struct. I.e. if [`set_name`] is called, this will not
193
+ /// update the span, as the underlying source is not altered by this operation.
194
+ ///
195
+ /// [`set_name`]: Self::set_name
196
+ #[ inline]
197
+ pub fn span ( & self ) -> Span {
198
+ self . span . clone ( )
199
+ }
200
+
179
201
/// Edit the name of the BytesStart in-place
180
202
///
181
203
/// # Warning
@@ -273,7 +295,7 @@ impl<'a> Debug for BytesStart<'a> {
273
295
fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
274
296
write ! ( f, "BytesStart {{ buf: " ) ?;
275
297
write_cow_string ( f, & self . buf ) ?;
276
- write ! ( f, ", name_len: {} }} " , self . name_len)
298
+ write ! ( f, ", name_len: {}, span: {:?} }} " , self . name_len, self . span )
277
299
}
278
300
}
279
301
0 commit comments