11use azure_core:: error:: { Error , ErrorKind , ResultExt } ;
2- use bytes:: Bytes ;
32
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) )
79 . context ( ErrorKind :: DataConversion , "failed to deserialize xml" )
810}
911
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 ..]
1616 } else {
17- bytes. clone ( )
17+ bytes
1818 }
1919}
2020
@@ -24,10 +24,10 @@ mod test {
2424
2525 #[ test]
2626 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) ) ;
2929
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) ) ;
3232 }
3333}
0 commit comments