Skip to content

Commit

Permalink
Merge pull request #31 from smklein/schemars
Browse files Browse the repository at this point in the history
Add 'schemars' feature for deriving JSON schema
  • Loading branch information
krisprice authored Mar 6, 2022
2 parents 88699c1 + e34c914 commit dacd04a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ categories = ["network-programming"]
readme = "README.md"
documentation = "https://docs.rs/ipnet"

[features]
default = []
# Implements "schemars::JsonSchema". Also implies "serde".
json = ["serde", "schemars"]

[dependencies]
serde = { version = "1", features = ["derive"], optional = true }
serde = { package = "serde", version = "1", features = ["derive"], optional = true }
schemars = { version = "0.8", optional = true }

[dev-dependencies]
serde_test = "1"
Expand Down
3 changes: 3 additions & 0 deletions src/ipnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use ipext::{IpAdd, IpSub, IpStep, IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};
/// assert_eq!(Ok(net.network()), "fd00::".parse());
/// ```
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "json", derive(schemars::JsonSchema))]
pub enum IpNet {
V4(Ipv4Net),
V6(Ipv6Net),
Expand Down Expand Up @@ -70,6 +71,7 @@ pub enum IpNet {
/// assert_eq!(Ok(net.network()), "10.1.1.0".parse());
/// ```
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "json", derive(schemars::JsonSchema))]
pub struct Ipv4Net {
addr: Ipv4Addr,
prefix_len: u8,
Expand Down Expand Up @@ -100,6 +102,7 @@ pub struct Ipv4Net {
/// assert_eq!(Ok(net.network()), "fd00::".parse());
/// ```
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "json", derive(schemars::JsonSchema))]
pub struct Ipv6Net {
addr: Ipv6Addr,
prefix_len: u8,
Expand Down
12 changes: 6 additions & 6 deletions src/ipnet_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use {IpNet, Ipv4Net, Ipv6Net};
use std::fmt;
use std::net::{Ipv4Addr, Ipv6Addr};
use serde::{self, Serialize, Deserialize, Serializer, Deserializer};
use serde::ser::{SerializeTuple};
use serde::ser::SerializeTuple;
use serde::de::{EnumAccess, Error, VariantAccess, Visitor};

impl Serialize for IpNet {
Expand Down Expand Up @@ -53,7 +53,7 @@ impl<'de> Deserialize<'de> for IpNet {
V4,
V6,
}

impl<'de> Visitor<'de> for EnumVisitor {
type Value = IpNet;

Expand All @@ -64,7 +64,7 @@ impl<'de> Deserialize<'de> for IpNet {
fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
where A: EnumAccess<'de>
{
match try!(data.variant()) {
match data.variant()? {
(IpNetKind::V4, v) => v.newtype_variant().map(IpNet::V4),
(IpNetKind::V6, v) => v.newtype_variant().map(IpNet::V6),
}
Expand Down Expand Up @@ -196,7 +196,7 @@ mod tests {
Token::TupleEnd,
]);
}

#[test]
fn test_serialize_ipnet_v6() {
let net_str = "fd00::/32";
Expand Down Expand Up @@ -225,7 +225,7 @@ mod tests {
Token::U8(0),
Token::U8(32),
Token::TupleEnd,
]);
]);
}

#[test]
Expand Down Expand Up @@ -273,4 +273,4 @@ mod tests {
Token::TupleEnd,
]);
}
}
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
//! [feature]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section
#[cfg(feature = "serde")]
#[macro_use]
extern crate serde;

pub use self::ipext::{IpAdd, IpSub, IpBitAnd, IpBitOr, IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};
Expand Down

0 comments on commit dacd04a

Please sign in to comment.