Skip to content

Commit

Permalink
Merge pull request #17 from wspeirs/master
Browse files Browse the repository at this point in the history
Add serde feature to IpCidr
  • Loading branch information
magiclen committed Sep 8, 2023
2 parents c1a2b76 + ac75942 commit e985a2b
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/cidr/ip_cidr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,47 @@ impl TryFrom<&str> for IpCidr {
IpCidr::from_str(s)
}

Check warning on line 206 in src/cidr/ip_cidr.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/cidr-utils/cidr-utils/src/cidr/ip_cidr.rs
}


#[cfg(feature = "serde")]
use serde::de::{Deserialize, Deserializer, Error as DeError, Visitor};
#[cfg(feature = "serde")]
use serde::ser::{Serialize, Serializer};

#[cfg(feature = "serde")]
impl Serialize for IpCidr {

Check warning on line 216 in src/cidr/ip_cidr.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/cidr-utils/cidr-utils/src/cidr/ip_cidr.rs
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer, {
serializer.serialize_str(self.to_string().as_str())
}
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for IpCidr {

Check warning on line 226 in src/cidr/ip_cidr.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/cidr-utils/cidr-utils/src/cidr/ip_cidr.rs
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>, {
struct IpVisitor;

impl<'de> Visitor<'de> for IpVisitor {
type Value = IpCidr;

#[inline]
fn expecting(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
f.write_str("a CIDR string")
}

Check warning on line 240 in src/cidr/ip_cidr.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/cidr-utils/cidr-utils/src/cidr/ip_cidr.rs
#[inline]
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: DeError, {
IpCidr::from_str(v).map_err(DeError::custom)
}
}

deserializer.deserialize_str(IpVisitor)
}
}

0 comments on commit e985a2b

Please sign in to comment.