Added custom deserialization and serialization for ipnetwork::IpNetwork

This commit is contained in:
Emīls Piņķis
2019-02-01 11:57:14 +00:00
parent 16c4af9823
commit d8ce2e4dbc
2 changed files with 37 additions and 9 deletions

View File

@ -13,10 +13,10 @@ categories = ["network-programming", "os"]
[dependencies] [dependencies]
clippy = {version = "0.0.302", optional = true} clippy = {version = "0.0.302", optional = true}
serde = ">=0.8.0, <2.0" serde = ">=0.8.0, <2.0"
serde_derive = ">=0.8.0, <2.0"
[dev-dependencies] [dev-dependencies]
serde_json = "1.0" serde_json = "1.0"
serde_derive = ">=0.8.0, <2.0"
[badges] [badges]
travis-ci = { repository = "achanda/ipnetwork" } travis-ci = { repository = "achanda/ipnetwork" }

View File

@ -7,8 +7,6 @@
#![doc(html_root_url = "https://docs.rs/ipnetwork/0.13.1")] #![doc(html_root_url = "https://docs.rs/ipnetwork/0.13.1")]
extern crate serde; extern crate serde;
#[macro_use]
extern crate serde_derive;
use std::fmt; use std::fmt;
use std::net::IpAddr; use std::net::IpAddr;
@ -19,19 +17,38 @@ mod ipv6;
use std::str::FromStr; use std::str::FromStr;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
pub use common::IpNetworkError; pub use common::IpNetworkError;
pub use ipv4::{Ipv4Network, ipv4_mask_to_prefix}; pub use ipv4::{ipv4_mask_to_prefix, Ipv4Network};
pub use ipv6::{Ipv6Network, ipv6_mask_to_prefix}; pub use ipv6::{ipv6_mask_to_prefix, Ipv6Network};
/// Represents a generic network range. This type can have two variants: /// Represents a generic network range. This type can have two variants:
/// the v4 and the v6 case. /// the v4 and the v6 case.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[serde(untagged)]
pub enum IpNetwork { pub enum IpNetwork {
V4(Ipv4Network), V4(Ipv4Network),
V6(Ipv6Network), V6(Ipv6Network),
} }
impl<'de> Deserialize<'de> for IpNetwork {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = <String>::deserialize(deserializer)?;
IpNetwork::from_str(&s).map_err(de::Error::custom)
}
}
impl Serialize for IpNetwork {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
/// Represents a generic network size. For IPv4, the max size is a u32 and for IPv6, it is a u128 /// Represents a generic network size. For IPv4, the max size is a u32 and for IPv6, it is a u128
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
@ -79,7 +96,7 @@ impl IpNetwork {
} }
} }
/// Returns the address of the network denoted by this `IpNetwork`. /// Returns the address of the network denoted by this `IpNetwork`.
/// This means the lowest possible IP address inside of the network. /// This means the lowest possible IP address inside of the network.
/// ///
/// # Examples /// # Examples
@ -207,7 +224,6 @@ impl IpNetwork {
} }
} }
/// Returns the number of possible host addresses in this `IpAddr` /// Returns the number of possible host addresses in this `IpAddr`
/// ///
/// # Examples /// # Examples
@ -292,3 +308,15 @@ pub fn ip_mask_to_prefix(mask: IpAddr) -> Result<u8, IpNetworkError> {
IpAddr::V6(mask) => ipv6_mask_to_prefix(mask), IpAddr::V6(mask) => ipv6_mask_to_prefix(mask),
} }
} }
#[cfg(test)]
mod test {
#[test]
fn deserialize_from_serde_json_value() {
use super::*;
let network = IpNetwork::from_str("0.0.0.0/0").unwrap();
let val: serde_json::value::Value = serde_json::from_str(&serde_json::to_string(&network).unwrap()).unwrap();
let _deser: IpNetwork =
serde_json::from_value(val).expect("Fails to deserialize from json_value::value::Value");
}
}