diff --git a/src/events/mod.rs b/src/events/mod.rs index c0b9a92..609ada0 100644 --- a/src/events/mod.rs +++ b/src/events/mod.rs @@ -41,6 +41,7 @@ use encoding_rs::Encoding; use std::{borrow::Cow, collections::HashMap, io::BufRead, ops::Deref, str::from_utf8}; use crate::escape::{do_unescape, escape}; +use crate::utils::write_cow_string; use crate::{errors::Error, errors::Result, reader::Reader}; use attributes::{Attribute, Attributes}; @@ -365,8 +366,6 @@ impl<'a> BytesStart<'a> { impl<'a> std::fmt::Debug for BytesStart<'a> { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - use crate::utils::write_cow_string; - write!(f, "BytesStart {{ buf: ")?; write_cow_string(f, &self.buf)?; write!(f, ", name_len: {} }}", self.name_len) @@ -548,15 +547,14 @@ impl<'a> BytesEnd<'a> { impl<'a> std::fmt::Debug for BytesEnd<'a> { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - use crate::utils::write_cow_string; - write!(f, "BytesEnd {{ name: ")?; write_cow_string(f, &self.name)?; write!(f, " }}") } } -/// Data from various events (most notably, `Event::Text`). +/// Data from various events (most notably, `Event::Text`) that stored in XML +/// in escaped form. Internally data is stored in escaped form #[derive(Clone, Eq, PartialEq)] pub struct BytesText<'a> { // Invariant: The content is always escaped. @@ -566,8 +564,8 @@ pub struct BytesText<'a> { impl<'a> BytesText<'a> { /// Creates a new `BytesText` from an escaped byte sequence. #[inline] - pub fn from_escaped>>(content: C) -> BytesText<'a> { - BytesText { + pub fn from_escaped>>(content: C) -> Self { + Self { content: content.into(), } } @@ -575,15 +573,15 @@ impl<'a> BytesText<'a> { /// Creates a new `BytesText` from a byte sequence. The byte sequence is /// expected not to be escaped. #[inline] - pub fn from_plain(content: &'a [u8]) -> BytesText<'a> { - BytesText { + pub fn from_plain(content: &'a [u8]) -> Self { + Self { content: escape(content), } } /// Creates a new `BytesText` from an escaped string. #[inline] - pub fn from_escaped_str>>(content: C) -> BytesText<'a> { + pub fn from_escaped_str>>(content: C) -> Self { Self::from_escaped(match content.into() { Cow::Owned(o) => Cow::Owned(o.into_bytes()), Cow::Borrowed(b) => Cow::Borrowed(b.as_bytes()), @@ -593,7 +591,7 @@ impl<'a> BytesText<'a> { /// Creates a new `BytesText` from a string. The string is expected not to /// be escaped. #[inline] - pub fn from_plain_str(content: &'a str) -> BytesText<'a> { + pub fn from_plain_str(content: &'a str) -> Self { Self::from_plain(content.as_bytes()) } @@ -856,8 +854,6 @@ impl<'a> BytesText<'a> { impl<'a> std::fmt::Debug for BytesText<'a> { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - use crate::utils::write_cow_string; - write!(f, "BytesText {{ content: ")?; write_cow_string(f, &self.content)?; write!(f, " }}")