Skip to content

Commit

Permalink
Optimise default string deserialization for Url
Browse files Browse the repository at this point in the history
  • Loading branch information
nox committed Jul 19, 2019
1 parent 171e65e commit c2ff51b
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/lib.rs
Expand Up @@ -2281,11 +2281,28 @@ impl<'de> serde::Deserialize<'de> for Url {
where
D: serde::Deserializer<'de>,
{
use serde::de::{Error, Unexpected};
let string_representation: String = serde::Deserialize::deserialize(deserializer)?;
Url::parse(&string_representation).map_err(|err| {
Error::invalid_value(Unexpected::Str(&string_representation), &err.description())
})
use serde::de::{Error, Unexpected, Visitor};

struct UrlVisitor;

impl<'de> Visitor<'de> for UrlVisitor {
type Value = Url;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a string representing an URL")
}

fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: Error,
{
Url::parse(s).map_err(|err| {
Error::invalid_value(Unexpected::Str(s), &err.description())
})
}
}

deserializer.deserialize_str(UrlVisitor)
}
}

Expand Down

0 comments on commit c2ff51b

Please sign in to comment.