@@ -48,6 +48,7 @@ use crate::encoding::Decoder;
4848use crate :: errors:: { Error , Result } ;
4949use crate :: escape:: { escape, partial_escape, unescape_with} ;
5050use crate :: name:: { LocalName , QName } ;
51+ use crate :: reader:: Span ;
5152use crate :: utils:: write_cow_string;
5253use attributes:: { Attribute , Attributes } ;
5354
@@ -67,15 +68,18 @@ pub struct BytesStart<'a> {
6768 pub ( crate ) buf : Cow < ' a , [ u8 ] > ,
6869 /// end of the element name, the name starts at that the start of `buf`
6970 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 ,
7073}
7174
7275impl < ' a > BytesStart < ' a > {
7376 /// Internal constructor, used by `Reader`. Supplies data in reader's encoding
7477 #[ 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 {
7679 BytesStart {
7780 buf : Cow :: Borrowed ( content) ,
7881 name_len,
82+ span,
7983 }
8084 }
8185
@@ -89,6 +93,7 @@ impl<'a> BytesStart<'a> {
8993 let buf = str_cow_to_bytes ( name) ;
9094 BytesStart {
9195 name_len : buf. len ( ) ,
96+ span : 0 ..buf. len ( ) ,
9297 buf,
9398 }
9499 }
@@ -102,8 +107,11 @@ impl<'a> BytesStart<'a> {
102107 /// to generate invalid XML if `content` or `name_len` are incorrect.
103108 #[ inline]
104109 pub fn from_content < C : Into < Cow < ' a , str > > > ( content : C , name_len : usize ) -> Self {
110+ let buf = str_cow_to_bytes ( content) ;
111+
105112 BytesStart {
106- buf : str_cow_to_bytes ( content) ,
113+ span : 0 ..buf. len ( ) ,
114+ buf,
107115 name_len,
108116 }
109117 }
@@ -113,6 +121,7 @@ impl<'a> BytesStart<'a> {
113121 BytesStart {
114122 buf : Cow :: Owned ( self . buf . into_owned ( ) ) ,
115123 name_len : self . name_len ,
124+ span : self . span ,
116125 }
117126 }
118127
@@ -121,6 +130,7 @@ impl<'a> BytesStart<'a> {
121130 BytesStart {
122131 buf : Cow :: Owned ( self . buf . to_owned ( ) . into ( ) ) ,
123132 name_len : self . name_len ,
133+ span : self . span ( ) ,
124134 }
125135 }
126136
@@ -153,6 +163,7 @@ impl<'a> BytesStart<'a> {
153163 BytesStart {
154164 buf : Cow :: Borrowed ( & self . buf ) ,
155165 name_len : self . name_len ,
166+ span : self . span ( ) ,
156167 }
157168 }
158169
@@ -176,6 +187,17 @@ impl<'a> BytesStart<'a> {
176187 self . name ( ) . into ( )
177188 }
178189
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+
179201 /// Edit the name of the BytesStart in-place
180202 ///
181203 /// # Warning
@@ -273,7 +295,7 @@ impl<'a> Debug for BytesStart<'a> {
273295 fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
274296 write ! ( f, "BytesStart {{ buf: " ) ?;
275297 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 )
277299 }
278300}
279301
0 commit comments