1
1
use azure_core:: error:: { Error , ErrorKind , ResultExt } ;
2
- use bytes:: Bytes ;
3
2
4
- /// Reads the XML from the Bytes.
5
- pub fn read_xml < ' de , T : serde:: de:: Deserialize < ' de > > ( body : & Bytes ) -> Result < T , Error > {
6
- serde_xml_rs:: from_reader ( slice_bom ( body) . as_ref ( ) )
3
+ /// The UTF8 [byte order marker](https://en.wikipedia.org/wiki/Byte_order_mark)
4
+ const UTF8_BOM : [ u8 ; 3 ] = [ 0xEF , 0xBB , 0xBF ] ;
5
+
6
+ /// Reads the XML from bytes.
7
+ pub fn read_xml < ' de , T : serde:: de:: Deserialize < ' de > > ( body : & [ u8 ] ) -> Result < T , Error > {
8
+ serde_xml_rs:: from_reader ( slice_bom ( body) )
7
9
. context ( ErrorKind :: DataConversion , "failed to deserialize xml" )
8
10
}
9
11
10
- const UTF8_BOM : [ u8 ; 3 ] = [ 0xEF , 0xBB , 0xBF ] ;
11
-
12
- /// Returns Bytes without the UTF-8 BOM.
13
- fn slice_bom ( bytes : & Bytes ) -> Bytes {
14
- if bytes. len ( ) > 3 && bytes. slice ( 0 ..3 ) . as_ref ( ) == UTF8_BOM {
15
- bytes. slice ( 3 ..)
12
+ /// Returns bytes without the UTF-8 BOM.
13
+ fn slice_bom ( bytes : & [ u8 ] ) -> & [ u8 ] {
14
+ if bytes. len ( ) > 3 && bytes[ 0 ..3 ] == UTF8_BOM {
15
+ & bytes[ 3 ..]
16
16
} else {
17
- bytes. clone ( )
17
+ bytes
18
18
}
19
19
}
20
20
@@ -24,10 +24,10 @@ mod test {
24
24
25
25
#[ test]
26
26
fn test_slice_bom ( ) {
27
- let bytes = Bytes :: from_static ( & [ 0xEF , 0xBB , 0xBF , 7 ] ) ;
28
- assert_eq ! ( Bytes :: from_static ( & [ 7 ] ) , slice_bom( & bytes) ) ;
27
+ let bytes = & [ 0xEF , 0xBB , 0xBF , 7 ] ;
28
+ assert_eq ! ( & [ 7 ] , slice_bom( bytes) ) ;
29
29
30
- let bytes = Bytes :: from_static ( & [ 8 ] ) ;
31
- assert_eq ! ( Bytes :: from_static ( & [ 8 ] ) , slice_bom( & bytes) ) ;
30
+ let bytes = & [ 8 ] ;
31
+ assert_eq ! ( & [ 8 ] , slice_bom( bytes) ) ;
32
32
}
33
33
}
0 commit comments