mirror of
https://github.com/achanda/ipnetwork.git
synced 2025-06-17 17:28:51 +00:00
No need to implement serialize/deserialize for enum, use serde(untagged)
This commit is contained in:
26
src/lib.rs
26
src/lib.rs
@ -14,9 +14,6 @@ extern crate serde;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
|
|
||||||
#[cfg(feature = "with-serde")]
|
|
||||||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
|
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
|
|
||||||
@ -32,33 +29,14 @@ 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.
|
||||||
|
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
|
||||||
|
#[cfg_attr(feature = "with-serde", serde(untagged))]
|
||||||
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub enum IpNetwork {
|
pub enum IpNetwork {
|
||||||
V4(Ipv4Network),
|
V4(Ipv4Network),
|
||||||
V6(Ipv6Network),
|
V6(Ipv6Network),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "with-serde")]
|
|
||||||
impl<'de> Deserialize<'de> for IpNetwork {
|
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
||||||
where
|
|
||||||
D: Deserializer<'de>,
|
|
||||||
{
|
|
||||||
let s = <&str>::deserialize(deserializer)?;
|
|
||||||
IpNetwork::from_str(s).map_err(de::Error::custom)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "with-serde")]
|
|
||||||
impl Serialize for IpNetwork {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: Serializer,
|
|
||||||
{
|
|
||||||
serializer.serialize_str(&self.to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IpNetwork {
|
impl IpNetwork {
|
||||||
/// Constructs a new `IpNetwork` from a given `IpAddr` and a prefix denoting the
|
/// Constructs a new `IpNetwork` from a given `IpAddr` and a prefix denoting the
|
||||||
/// network size. If the prefix is larger than 32 (for IPv4) or 128 (for IPv6), this
|
/// network size. If the prefix is larger than 32 (for IPv4) or 128 (for IPv6), this
|
||||||
|
Reference in New Issue
Block a user