use u8 slice for read_xml instead of Bytes
use azure_core::error::{Error, ErrorKind, ResultExt};
-use bytes::Bytes;
-/// Reads the XML from the Bytes.
-pub fn read_xml<'de, T: serde::de::Deserialize<'de>>(body: &Bytes) -> Result<T, Error> {
- serde_xml_rs::from_reader(slice_bom(body).as_ref())
+/// The UTF8 [byte order marker](https://en.wikipedia.org/wiki/Byte_order_mark)
+const UTF8_BOM: [u8; 3] = [0xEF, 0xBB, 0xBF];
+
+/// Reads the XML from bytes.
+pub fn read_xml<'de, T: serde::de::Deserialize<'de>>(body: &[u8]) -> Result<T, Error> {
+ serde_xml_rs::from_reader(slice_bom(body))
.context(ErrorKind::DataConversion, "failed to deserialize xml")
}
-const UTF8_BOM: [u8; 3] = [0xEF, 0xBB, 0xBF];
-
-/// Returns Bytes without the UTF-8 BOM.
-fn slice_bom(bytes: &Bytes) -> Bytes {
- if bytes.len() > 3 && bytes.slice(0..3).as_ref() == UTF8_BOM {
- bytes.slice(3..)
+/// Returns bytes without the UTF-8 BOM.
+fn slice_bom(bytes: &[u8]) -> &[u8] {
+ if bytes.len() > 3 && bytes[0..3] == UTF8_BOM {
+ &bytes[3..]
} else {
- bytes.clone()
+ bytes
}
}
@@ -24,10 +24,10 @@ mod test {
#[test]
fn test_slice_bom() {
- let bytes = Bytes::from_static(&[0xEF, 0xBB, 0xBF, 7]);
- assert_eq!(Bytes::from_static(&[7]), slice_bom(&bytes));
+ let bytes = &[0xEF, 0xBB, 0xBF, 7];
+ assert_eq!(&[7], slice_bom(bytes));
- let bytes = Bytes::from_static(&[8]);
- assert_eq!(Bytes::from_static(&[8]), slice_bom(&bytes));
+ let bytes = &[8];
+ assert_eq!(&[8], slice_bom(bytes));
}
}
