Skip to content

Commit

Permalink
Use Cow<str> for EscapedDeserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
dralley committed Jul 16, 2022
1 parent ba25941 commit 9307489
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
17 changes: 8 additions & 9 deletions src/de/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ use std::borrow::Cow;
#[derive(Clone, Debug)]
pub struct EscapedDeserializer<'a> {
/// Possible escaped value of text/CDATA or attribute value
escaped_value: Cow<'a, [u8]>,
escaped_value: Cow<'a, str>,
/// If `true`, value requires unescaping before using
escaped: bool,
}

impl<'a> EscapedDeserializer<'a> {
pub fn new(escaped_value: Cow<'a, [u8]>, escaped: bool) -> Self {
pub fn new(escaped_value: Cow<'a, str>, escaped: bool) -> Self {
EscapedDeserializer {
escaped_value,
escaped,
Expand All @@ -37,7 +37,7 @@ macro_rules! deserialize_num {
where
V: Visitor<'de>,
{
let value = String::from_utf8(self.escaped_value.as_ref().to_vec())?.parse()?; // TODO(dalley): this is temporary
let value = self.escaped_value.parse()?;

visitor.$visit(value)
}
Expand All @@ -59,15 +59,14 @@ impl<'de, 'a> serde::Deserializer<'de> for EscapedDeserializer<'a> {
V: Visitor<'de>,
{
if self.escaped {
match unescape(std::str::from_utf8(self.escaped_value.as_ref())?)? {
// TODO(dalley): remove temporary from_utf8
match unescape(self.escaped_value.as_ref())? {
Cow::Borrowed(s) => visitor.visit_str(s),
Cow::Owned(s) => visitor.visit_string(s),
}
} else {
match self.escaped_value {
Cow::Borrowed(s) => visitor.visit_str(std::str::from_utf8(s)?),
Cow::Owned(s) => visitor.visit_string(String::from_utf8(s)?),
Cow::Borrowed(s) => visitor.visit_str(s),
Cow::Owned(s) => visitor.visit_string(s),
}
}
}
Expand Down Expand Up @@ -101,7 +100,7 @@ impl<'de, 'a> serde::Deserializer<'de> for EscapedDeserializer<'a> {
where
V: Visitor<'de>,
{
deserialize_bool(self.escaped_value.as_ref(), visitor)
deserialize_bool(&self.escaped_value.as_bytes(), visitor)
}

fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
Expand All @@ -122,7 +121,7 @@ impl<'de, 'a> serde::Deserializer<'de> for EscapedDeserializer<'a> {
where
V: Visitor<'de>,
{
if self.escaped_value.as_ref().is_empty() {
if self.escaped_value.is_empty() {
visitor.visit_none()
} else {
visitor.visit_some(self)
Expand Down
11 changes: 7 additions & 4 deletions src/de/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,11 @@ where
// try getting map from attributes (key= "value")
let (key, value) = a.into();
self.source = ValueSource::Attribute(value.unwrap_or_default());
seed.deserialize(EscapedDeserializer::new(Cow::Borrowed(&slice[key]), false))
.map(Some)
seed.deserialize(EscapedDeserializer::new(
Cow::Borrowed(std::str::from_utf8(&slice[key])?),
false,
)) // TODO(dalley): this is temporary
.map(Some)
} else {
// try getting from events (<key>value</key>)
match self.de.peek()? {
Expand Down Expand Up @@ -288,8 +291,8 @@ where
// }
seed.deserialize(self.unflatten_fields.remove(p).into_deserializer())
} else {
let name = Cow::Borrowed(e.local_name().into_inner());
seed.deserialize(EscapedDeserializer::new(name, false))
let name = std::str::from_utf8(e.local_name().into_inner())?; // TODO(dalley): this is temporary
seed.deserialize(EscapedDeserializer::new(Cow::Borrowed(name), false))
};
key.map(Some)
}
Expand Down
13 changes: 10 additions & 3 deletions src/de/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ where
V: DeserializeSeed<'de>,
{
let de = match self.de.peek()? {
DeEvent::Text(t) => EscapedDeserializer::new(Cow::Borrowed(t), true),
DeEvent::Text(t) => {
EscapedDeserializer::new(Cow::Borrowed(std::str::from_utf8(t)?), true)
} // TODO(dalley): temporary
// Escape sequences does not processed inside CDATA section
DeEvent::CData(t) => EscapedDeserializer::new(Cow::Borrowed(t), false),
DeEvent::CData(t) => {
EscapedDeserializer::new(Cow::Borrowed(std::str::from_utf8(t)?), false)
} // TODO(dalley): temporary
DeEvent::Start(e) => {
EscapedDeserializer::new(Cow::Borrowed(e.name().into_inner()), false)
EscapedDeserializer::new(
Cow::Borrowed(std::str::from_utf8(e.name().into_inner())?),
false,
) // TODO(dalley): temporary
}
_ => {
return Err(DeError::Unsupported(
Expand Down

0 comments on commit 9307489

Please sign in to comment.