Skip to content

Commit

Permalink
Merge pull request #1948 from Marwes/from_str
Browse files Browse the repository at this point in the history
refactor: Merge multiple FromStr visitors to a single FromStrVisitor
  • Loading branch information
dtolnay authored Jan 12, 2021
2 parents 4c89cf8 + 391d3ab commit 78a9dbc
Showing 1 changed file with 40 additions and 72 deletions.
112 changes: 40 additions & 72 deletions serde/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,24 +1260,7 @@ macro_rules! parse_ip_impl {
D: Deserializer<'de>,
{
if deserializer.is_human_readable() {
struct IpAddrVisitor;

impl<'de> Visitor<'de> for IpAddrVisitor {
type Value = $ty;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str($expecting)
}

fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: Error,
{
s.parse().map_err(Error::custom)
}
}

deserializer.deserialize_str(IpAddrVisitor)
deserializer.deserialize_str(FromStrVisitor::new($expecting))
} else {
<[u8; $size]>::deserialize(deserializer).map(<$ty>::from)
}
Expand Down Expand Up @@ -1405,24 +1388,7 @@ impl<'de> Deserialize<'de> for net::IpAddr {
D: Deserializer<'de>,
{
if deserializer.is_human_readable() {
struct IpAddrVisitor;

impl<'de> Visitor<'de> for IpAddrVisitor {
type Value = net::IpAddr;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("IP address")
}

fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: Error,
{
s.parse().map_err(Error::custom)
}
}

deserializer.deserialize_str(IpAddrVisitor)
deserializer.deserialize_str(FromStrVisitor::new("IP address"))
} else {
use lib::net::IpAddr;
deserialize_enum! {
Expand All @@ -1449,24 +1415,7 @@ macro_rules! parse_socket_impl {
D: Deserializer<'de>,
{
if deserializer.is_human_readable() {
struct SocketAddrVisitor;

impl<'de> Visitor<'de> for SocketAddrVisitor {
type Value = $ty;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str($expecting)
}

fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: Error,
{
s.parse().map_err(Error::custom)
}
}

deserializer.deserialize_str(SocketAddrVisitor)
deserializer.deserialize_str(FromStrVisitor::new($expecting))
} else {
<(_, u16)>::deserialize(deserializer).map(|(ip, port)| $new(ip, port))
}
Expand All @@ -1482,24 +1431,7 @@ impl<'de> Deserialize<'de> for net::SocketAddr {
D: Deserializer<'de>,
{
if deserializer.is_human_readable() {
struct SocketAddrVisitor;

impl<'de> Visitor<'de> for SocketAddrVisitor {
type Value = net::SocketAddr;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("socket address")
}

fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: Error,
{
s.parse().map_err(Error::custom)
}
}

deserializer.deserialize_str(SocketAddrVisitor)
deserializer.deserialize_str(FromStrVisitor::new("socket address"))
} else {
use lib::net::SocketAddr;
deserialize_enum! {
Expand Down Expand Up @@ -2603,3 +2535,39 @@ atomic_impl! {
atomic_impl! {
AtomicI64 AtomicU64
}

#[cfg(feature = "std")]
struct FromStrVisitor<T> {
expecting: &'static str,
ty: PhantomData<T>,
}

#[cfg(feature = "std")]
impl<T> FromStrVisitor<T> {
fn new(expecting: &'static str) -> Self {
FromStrVisitor {
expecting: expecting,
ty: PhantomData,
}
}
}

#[cfg(feature = "std")]
impl<'de, T> Visitor<'de> for FromStrVisitor<T>
where
T: str::FromStr,
T::Err: fmt::Display,
{
type Value = T;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(self.expecting)
}

fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: Error,
{
s.parse().map_err(Error::custom)
}
}

0 comments on commit 78a9dbc

Please # to comment.