From e075fb80f87dbd25cadc11bf1b28529d334c32a2 Mon Sep 17 00:00:00 2001 From: sharks Date: Sun, 15 Apr 2018 20:58:52 -0500 Subject: [PATCH 01/12] Add IPv4 test case --- Cargo.toml | 3 +++ tests/test_json.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/test_json.rs diff --git a/Cargo.toml b/Cargo.toml index 6c01ab2..08244d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,9 @@ clippy = {version = "0.0.104", optional = true} serde = { version = ">=0.8.0, <2.0", optional = true } serde_derive = { version = ">=0.8.0, <2.0", optional = true } +[dev-dependencies] +serde_json = "*" + [badges] travis-ci = { repository = "achanda/ipnetwork" } diff --git a/tests/test_json.rs b/tests/test_json.rs new file mode 100644 index 0000000..9d1aab4 --- /dev/null +++ b/tests/test_json.rs @@ -0,0 +1,35 @@ +#[cfg(feature = "with-serde")] +extern crate serde; +#[cfg(feature = "with-serde")] +extern crate serde_json; +#[cfg(feature = "with-serde")] +#[macro_use] +extern crate serde_derive; + +extern crate ipnetwork; + + +#[cfg(test)] +mod tests { + + use ipnetwork::Ipv4Network; + use std::net::Ipv4Addr; + + #[cfg(feature = "with-serde")] + #[test] + fn test_ipv4_json() { + let json_string = "{\"ipnetwork\":{\"addr\":\"127.1.0.0\",\"prefix\":24}}"; + + #[derive(Serialize, Deserialize)] + struct MyStruct { + ipnetwork: Ipv4Network, + } + + let mystruct: MyStruct = ::serde_json::from_str(json_string).unwrap(); + + assert_eq!(mystruct.ipnetwork.ip(), Ipv4Addr::new(127, 1, 0, 0)); + assert_eq!(mystruct.ipnetwork.prefix(), 24); + + assert_eq!(::serde_json::to_string(&mystruct).unwrap(), json_string); + } +} \ No newline at end of file From 7f684cf06e623a33bd043cca7ed1b6513eb618f6 Mon Sep 17 00:00:00 2001 From: sharks Date: Sun, 15 Apr 2018 21:04:00 -0500 Subject: [PATCH 02/12] WARNING: src/lib.rs - IpNetwork::mask (line 83) Code block is not currently run as a test, but will in future versions of rustdoc. Please ensure this code block is a runnable test, or use the `ignore` directive. --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 21f99df..cde4d7b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -79,6 +79,7 @@ impl IpNetwork { /// That means the `prefix` most significant bits will be 1 and the rest 0 /// /// # Example + /// /// ``` /// use ipnetwork::IpNetwork; /// use std::net::{Ipv4Addr, Ipv6Addr}; @@ -92,7 +93,7 @@ impl IpNetwork { /// assert_eq!(v6_net.mask(), Ipv6Addr::new(0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff)); /// let v6_net: IpNetwork = "ff01::0/32".parse().unwrap(); /// assert_eq!(v6_net.mask(), Ipv6Addr::new(0xffff, 0xffff, 0, 0, 0, 0, 0, 0)); - ///``` + /// ``` pub fn mask(&self) -> IpAddr { match *self { IpNetwork::V4(ref a) => IpAddr::V4(a.mask()), From ce26663b1ffebbef7dbfe7fa63830ac6577f7e10 Mon Sep 17 00:00:00 2001 From: sharks Date: Sun, 15 Apr 2018 21:04:13 -0500 Subject: [PATCH 03/12] Add Ipv6 test case --- tests/test_json.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/tests/test_json.rs b/tests/test_json.rs index 9d1aab4..74733ab 100644 --- a/tests/test_json.rs +++ b/tests/test_json.rs @@ -12,13 +12,13 @@ extern crate ipnetwork; #[cfg(test)] mod tests { - use ipnetwork::Ipv4Network; - use std::net::Ipv4Addr; + use ipnetwork::{Ipv4Network, Ipv6Network}; + use std::net::{Ipv4Addr, Ipv6Addr}; #[cfg(feature = "with-serde")] #[test] fn test_ipv4_json() { - let json_string = "{\"ipnetwork\":{\"addr\":\"127.1.0.0\",\"prefix\":24}}"; + let json_string = r#"{"ipnetwork":{"addr":"127.1.0.0","prefix":24}}"#; #[derive(Serialize, Deserialize)] struct MyStruct { @@ -32,4 +32,22 @@ mod tests { assert_eq!(::serde_json::to_string(&mystruct).unwrap(), json_string); } + + #[cfg(feature = "with-serde")] + #[test] + fn test_ipv6_json() { + let json_string = r#"{"ipnetwork":{"addr":"::1","prefix":0}}"#; + + #[derive(Serialize, Deserialize)] + struct MyStruct { + ipnetwork: Ipv6Network, + } + + let mystruct: MyStruct = ::serde_json::from_str(json_string).unwrap(); + + assert_eq!(mystruct.ipnetwork.ip(), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); + assert_eq!(mystruct.ipnetwork.prefix(), 0); + + assert_eq!(::serde_json::to_string(&mystruct).unwrap(), json_string); + } } \ No newline at end of file From 811cab642e312473d068def8172b14a794ee42e2 Mon Sep 17 00:00:00 2001 From: sharks Date: Sun, 15 Apr 2018 21:11:01 -0500 Subject: [PATCH 04/12] vector of ip networks --- tests/test_json.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/test_json.rs b/tests/test_json.rs index 74733ab..a71bbb1 100644 --- a/tests/test_json.rs +++ b/tests/test_json.rs @@ -12,7 +12,7 @@ extern crate ipnetwork; #[cfg(test)] mod tests { - use ipnetwork::{Ipv4Network, Ipv6Network}; + use ipnetwork::{IpNetwork, Ipv4Network, Ipv6Network}; use std::net::{Ipv4Addr, Ipv6Addr}; #[cfg(feature = "with-serde")] @@ -50,4 +50,24 @@ mod tests { assert_eq!(::serde_json::to_string(&mystruct).unwrap(), json_string); } + + #[cfg(feature = "with-serde")] + #[test] + fn test_ipnetwork_json() { + let json_string = r#"{"ipnetwork":[{"V4":{"addr":"127.1.0.0","prefix":24}},{"V6":{"addr":"::1","prefix":0}}]}"#; + + #[derive(Serialize, Deserialize)] + struct MyStruct { + ipnetwork: Vec, + } + + let mystruct: MyStruct = ::serde_json::from_str(json_string).unwrap(); + + assert_eq!(mystruct.ipnetwork[0].ip(), Ipv4Addr::new(127, 1, 0, 0)); + assert_eq!(mystruct.ipnetwork[0].prefix(), 24); + assert_eq!(mystruct.ipnetwork[1].ip(), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); + assert_eq!(mystruct.ipnetwork[1].prefix(), 0); + + assert_eq!(::serde_json::to_string(&mystruct).unwrap(), json_string); + } } \ No newline at end of file From 15fa0936180f1779a460d41f9b4939724561e190 Mon Sep 17 00:00:00 2001 From: sharks Date: Sun, 15 Apr 2018 21:15:30 -0500 Subject: [PATCH 05/12] cargo fmt --- src/common.rs | 4 ++-- src/ipv4.rs | 6 +++--- src/ipv6.rs | 2 +- src/lib.rs | 6 +++--- tests/test_json.rs | 13 +++++++++---- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/common.rs b/src/common.rs index 17ac193..5007599 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,6 +1,6 @@ -use std::net::Ipv4Addr; -use std::fmt; use std::error::Error; +use std::fmt; +use std::net::Ipv4Addr; /// Represents a bunch of errors that can occur while working with a `IpNetwork` #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/src/ipv4.rs b/src/ipv4.rs index e0fd721..1ad4e1c 100644 --- a/src/ipv4.rs +++ b/src/ipv4.rs @@ -243,10 +243,10 @@ pub fn ipv4_mask_to_prefix(mask: Ipv4Addr) -> Result { #[cfg(test)] mod test { - use std::mem; - use std::collections::HashMap; - use std::net::Ipv4Addr; use super::*; + use std::collections::HashMap; + use std::mem; + use std::net::Ipv4Addr; #[test] fn create_v4() { diff --git a/src/ipv6.rs b/src/ipv6.rs index 711423e..edd53a0 100644 --- a/src/ipv6.rs +++ b/src/ipv6.rs @@ -253,8 +253,8 @@ pub fn ipv6_mask_to_prefix(mask: Ipv6Addr) -> Result { #[cfg(test)] mod test { - use std::net::Ipv6Addr; use super::*; + use std::net::Ipv6Addr; #[test] fn create_v6() { diff --git a/src/lib.rs b/src/lib.rs index cde4d7b..3722773 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,15 +17,15 @@ extern crate serde_derive; use std::fmt; use std::net::IpAddr; +mod common; mod ipv4; mod ipv6; -mod common; use std::str::FromStr; -pub use ipv4::{Ipv4Network, ipv4_mask_to_prefix}; -pub use ipv6::{Ipv6Network, ipv6_mask_to_prefix}; pub use common::IpNetworkError; +pub use ipv4::{ipv4_mask_to_prefix, Ipv4Network}; +pub use ipv6::{ipv6_mask_to_prefix, Ipv6Network}; /// Represents a generic network range. This type can have two variants: /// the v4 and the v6 case. diff --git a/tests/test_json.rs b/tests/test_json.rs index a71bbb1..5876cad 100644 --- a/tests/test_json.rs +++ b/tests/test_json.rs @@ -8,7 +8,6 @@ extern crate serde_derive; extern crate ipnetwork; - #[cfg(test)] mod tests { @@ -45,7 +44,10 @@ mod tests { let mystruct: MyStruct = ::serde_json::from_str(json_string).unwrap(); - assert_eq!(mystruct.ipnetwork.ip(), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); + assert_eq!( + mystruct.ipnetwork.ip(), + Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1) + ); assert_eq!(mystruct.ipnetwork.prefix(), 0); assert_eq!(::serde_json::to_string(&mystruct).unwrap(), json_string); @@ -65,9 +67,12 @@ mod tests { assert_eq!(mystruct.ipnetwork[0].ip(), Ipv4Addr::new(127, 1, 0, 0)); assert_eq!(mystruct.ipnetwork[0].prefix(), 24); - assert_eq!(mystruct.ipnetwork[1].ip(), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); + assert_eq!( + mystruct.ipnetwork[1].ip(), + Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1) + ); assert_eq!(mystruct.ipnetwork[1].prefix(), 0); assert_eq!(::serde_json::to_string(&mystruct).unwrap(), json_string); } -} \ No newline at end of file +} From 6b4dc9762bf54e39453b4b97e8dc4dcd03625e83 Mon Sep 17 00:00:00 2001 From: sharks Date: Mon, 16 Apr 2018 19:22:33 -0500 Subject: [PATCH 06/12] pin version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 08244d1..339e00e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ serde = { version = ">=0.8.0, <2.0", optional = true } serde_derive = { version = ">=0.8.0, <2.0", optional = true } [dev-dependencies] -serde_json = "*" +serde_json = "1.0" [badges] travis-ci = { repository = "achanda/ipnetwork" } From ac3b11b1d7f924a76dcdec0f56a5c4486b658b8a Mon Sep 17 00:00:00 2001 From: sharks Date: Mon, 16 Apr 2018 19:22:54 -0500 Subject: [PATCH 07/12] ignore .idea/ folder --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7a635e0..60465a5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .gitignore Cargo.lock target -.vscode \ No newline at end of file +.vscode +.idea/ \ No newline at end of file From 4414ae4f0f65a0f1b932f75cdb1370c758c0ecee Mon Sep 17 00:00:00 2001 From: sharks Date: Mon, 16 Apr 2018 19:25:22 -0500 Subject: [PATCH 08/12] direct serialize/deserialize from string/to string --- src/ipv4.rs | 25 ++++++++++++++++++++++++- src/ipv6.rs | 25 ++++++++++++++++++++++++- src/lib.rs | 25 ++++++++++++++++++++++++- tests/test_json.rs | 6 +++--- 4 files changed, 75 insertions(+), 6 deletions(-) diff --git a/src/ipv4.rs b/src/ipv4.rs index 1ad4e1c..b15f7aa 100644 --- a/src/ipv4.rs +++ b/src/ipv4.rs @@ -2,18 +2,41 @@ use std::fmt; use std::net::Ipv4Addr; use std::str::FromStr; +#[cfg(feature = "with-serde")] +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + use common::{cidr_parts, parse_addr, parse_prefix, IpNetworkError}; const IPV4_BITS: u8 = 32; /// Represents a network range where the IP addresses are of v4 -#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct Ipv4Network { addr: Ipv4Addr, prefix: u8, } +#[cfg(feature = "with-serde")] +impl<'de> Deserialize<'de> for Ipv4Network { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let s = <&str>::deserialize(deserializer)?; + Ipv4Network::from_str(s).map_err(de::Error::custom) + } +} + +#[cfg(feature = "with-serde")] +impl Serialize for Ipv4Network { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(&self.to_string()) + } +} + impl Ipv4Network { /// Constructs a new `Ipv4Network` from any `Ipv4Addr` and a prefix denoting the network size. /// If the prefix is larger than 32 this will return an `IpNetworkError::InvalidPrefix`. diff --git a/src/ipv6.rs b/src/ipv6.rs index edd53a0..fa57116 100644 --- a/src/ipv6.rs +++ b/src/ipv6.rs @@ -3,19 +3,42 @@ use std::fmt; use std::net::Ipv6Addr; use std::str::FromStr; +#[cfg(feature = "with-serde")] +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + use common::{cidr_parts, parse_prefix, IpNetworkError}; const IPV6_BITS: u8 = 128; const IPV6_SEGMENT_BITS: u8 = 16; /// Represents a network range where the IP addresses are of v6 -#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct Ipv6Network { addr: Ipv6Addr, prefix: u8, } +#[cfg(feature = "with-serde")] +impl<'de> Deserialize<'de> for Ipv6Network { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let s = <&str>::deserialize(deserializer)?; + Ipv6Network::from_str(s).map_err(de::Error::custom) + } +} + +#[cfg(feature = "with-serde")] +impl Serialize for Ipv6Network { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(&self.to_string()) + } +} + impl Ipv6Network { /// Constructs a new `Ipv6Network` from any `Ipv6Addr` and a prefix denoting the network size. /// If the prefix is larger than 128 this will return an `IpNetworkError::InvalidPrefix`. diff --git a/src/lib.rs b/src/lib.rs index 3722773..5fa5f27 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,9 @@ extern crate serde; #[macro_use] extern crate serde_derive; +#[cfg(feature = "with-serde")] +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + use std::fmt; use std::net::IpAddr; @@ -29,13 +32,33 @@ pub use ipv6::{ipv6_mask_to_prefix, Ipv6Network}; /// Represents a generic network range. This type can have two variants: /// the v4 and the v6 case. -#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] pub enum IpNetwork { V4(Ipv4Network), V6(Ipv6Network), } +#[cfg(feature = "with-serde")] +impl<'de> Deserialize<'de> for IpNetwork { + fn deserialize(deserializer: D) -> Result + 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(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(&self.to_string()) + } +} + impl IpNetwork { /// 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 diff --git a/tests/test_json.rs b/tests/test_json.rs index 5876cad..383fc28 100644 --- a/tests/test_json.rs +++ b/tests/test_json.rs @@ -17,7 +17,7 @@ mod tests { #[cfg(feature = "with-serde")] #[test] fn test_ipv4_json() { - let json_string = r#"{"ipnetwork":{"addr":"127.1.0.0","prefix":24}}"#; + let json_string = r#"{"ipnetwork":"127.1.0.0/24"}"#; #[derive(Serialize, Deserialize)] struct MyStruct { @@ -35,7 +35,7 @@ mod tests { #[cfg(feature = "with-serde")] #[test] fn test_ipv6_json() { - let json_string = r#"{"ipnetwork":{"addr":"::1","prefix":0}}"#; + let json_string = r#"{"ipnetwork":"::1/0"}"#; #[derive(Serialize, Deserialize)] struct MyStruct { @@ -56,7 +56,7 @@ mod tests { #[cfg(feature = "with-serde")] #[test] fn test_ipnetwork_json() { - let json_string = r#"{"ipnetwork":[{"V4":{"addr":"127.1.0.0","prefix":24}},{"V6":{"addr":"::1","prefix":0}}]}"#; + let json_string = r#"{"ipnetwork":["127.1.0.0/24","::1/0"]}"#; #[derive(Serialize, Deserialize)] struct MyStruct { From 4e9714660566077a47b462273cf7c10cf060a68f Mon Sep 17 00:00:00 2001 From: sharks Date: Tue, 17 Apr 2018 06:30:44 -0500 Subject: [PATCH 09/12] No need to implement serialize/deserialize for enum, use serde(untagged) --- src/lib.rs | 26 ++------------------------ 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5fa5f27..49dfd84 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,9 +14,6 @@ extern crate serde; #[macro_use] extern crate serde_derive; -#[cfg(feature = "with-serde")] -use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; - use std::fmt; 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: /// 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)] pub enum IpNetwork { V4(Ipv4Network), V6(Ipv6Network), } -#[cfg(feature = "with-serde")] -impl<'de> Deserialize<'de> for IpNetwork { - fn deserialize(deserializer: D) -> Result - 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(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str(&self.to_string()) - } -} - impl IpNetwork { /// 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 From de707e35ae906fb66d5a72a8b7ac74b500958fc0 Mon Sep 17 00:00:00 2001 From: sharks Date: Tue, 17 Apr 2018 17:03:36 -0500 Subject: [PATCH 10/12] Remove with-serde in favor of just 'serde' https://rust-lang-nursery.github.io/api-guidelines/naming.html#feature-names-are-free-of-placeholder-words-c-feature --- .travis.yml | 2 +- Cargo.toml | 1 - src/ipv4.rs | 6 +++--- src/ipv6.rs | 6 +++--- src/lib.rs | 8 ++++---- tests/test_json.rs | 12 ++++++------ 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/.travis.yml b/.travis.yml index 868d2c9..ce876ac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ env: matrix: include: - rust: nightly - env: FEATURES=ipv6-iterator,ipv6-methods,with-serde + env: FEATURES=ipv6-iterator,ipv6-methods,serde,serde-derive script: - cargo build --features $FEATURES --verbose diff --git a/Cargo.toml b/Cargo.toml index 339e00e..8421dc7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,4 +26,3 @@ default = [] dev = ["clippy"] ipv6-iterator = [] ipv6-methods = [] -with-serde = ["serde", "serde_derive"] diff --git a/src/ipv4.rs b/src/ipv4.rs index b15f7aa..3202a1c 100644 --- a/src/ipv4.rs +++ b/src/ipv4.rs @@ -2,7 +2,7 @@ use std::fmt; use std::net::Ipv4Addr; use std::str::FromStr; -#[cfg(feature = "with-serde")] +#[cfg(feature = "serde")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use common::{cidr_parts, parse_addr, parse_prefix, IpNetworkError}; @@ -16,7 +16,7 @@ pub struct Ipv4Network { prefix: u8, } -#[cfg(feature = "with-serde")] +#[cfg(feature = "serde")] impl<'de> Deserialize<'de> for Ipv4Network { fn deserialize(deserializer: D) -> Result where @@ -27,7 +27,7 @@ impl<'de> Deserialize<'de> for Ipv4Network { } } -#[cfg(feature = "with-serde")] +#[cfg(feature = "serde")] impl Serialize for Ipv4Network { fn serialize(&self, serializer: S) -> Result where diff --git a/src/ipv6.rs b/src/ipv6.rs index fa57116..6e3c1ac 100644 --- a/src/ipv6.rs +++ b/src/ipv6.rs @@ -3,7 +3,7 @@ use std::fmt; use std::net::Ipv6Addr; use std::str::FromStr; -#[cfg(feature = "with-serde")] +#[cfg(feature = "serde")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use common::{cidr_parts, parse_prefix, IpNetworkError}; @@ -18,7 +18,7 @@ pub struct Ipv6Network { prefix: u8, } -#[cfg(feature = "with-serde")] +#[cfg(feature = "serde")] impl<'de> Deserialize<'de> for Ipv6Network { fn deserialize(deserializer: D) -> Result where @@ -29,7 +29,7 @@ impl<'de> Deserialize<'de> for Ipv6Network { } } -#[cfg(feature = "with-serde")] +#[cfg(feature = "serde")] impl Serialize for Ipv6Network { fn serialize(&self, serializer: S) -> Result where diff --git a/src/lib.rs b/src/lib.rs index 49dfd84..f5fffaa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,9 +8,9 @@ #![crate_type = "lib"] #![doc(html_root_url = "https://docs.rs/ipnetwork/0.12.8")] -#[cfg(feature = "with-serde")] +#[cfg(feature = "serde")] extern crate serde; -#[cfg(feature = "with-serde")] +#[cfg(feature = "serde")] #[macro_use] extern crate serde_derive; @@ -29,8 +29,8 @@ pub use ipv6::{ipv6_mask_to_prefix, Ipv6Network}; /// Represents a generic network range. This type can have two variants: /// the v4 and the v6 case. -#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "with-serde", serde(untagged))] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", serde(untagged))] #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] pub enum IpNetwork { V4(Ipv4Network), diff --git a/tests/test_json.rs b/tests/test_json.rs index 383fc28..373290c 100644 --- a/tests/test_json.rs +++ b/tests/test_json.rs @@ -1,8 +1,8 @@ -#[cfg(feature = "with-serde")] +#[cfg(feature = "serde")] extern crate serde; -#[cfg(feature = "with-serde")] +#[cfg(feature = "serde")] extern crate serde_json; -#[cfg(feature = "with-serde")] +#[cfg(feature = "serde")] #[macro_use] extern crate serde_derive; @@ -14,7 +14,7 @@ mod tests { use ipnetwork::{IpNetwork, Ipv4Network, Ipv6Network}; use std::net::{Ipv4Addr, Ipv6Addr}; - #[cfg(feature = "with-serde")] + #[cfg(feature = "serde")] #[test] fn test_ipv4_json() { let json_string = r#"{"ipnetwork":"127.1.0.0/24"}"#; @@ -32,7 +32,7 @@ mod tests { assert_eq!(::serde_json::to_string(&mystruct).unwrap(), json_string); } - #[cfg(feature = "with-serde")] + #[cfg(feature = "serde")] #[test] fn test_ipv6_json() { let json_string = r#"{"ipnetwork":"::1/0"}"#; @@ -53,7 +53,7 @@ mod tests { assert_eq!(::serde_json::to_string(&mystruct).unwrap(), json_string); } - #[cfg(feature = "with-serde")] + #[cfg(feature = "serde")] #[test] fn test_ipnetwork_json() { let json_string = r#"{"ipnetwork":["127.1.0.0/24","::1/0"]}"#; From c6ead9d65429f89f7104936e817bf1990971f9b8 Mon Sep 17 00:00:00 2001 From: sharks Date: Tue, 17 Apr 2018 19:28:38 -0500 Subject: [PATCH 11/12] remove serde feature flag --- .travis.yml | 2 +- Cargo.toml | 4 ++-- src/ipv4.rs | 3 --- src/ipv6.rs | 3 --- src/lib.rs | 7 ++----- tests/test_json.rs | 8 ++------ 6 files changed, 7 insertions(+), 20 deletions(-) diff --git a/.travis.yml b/.travis.yml index ce876ac..6a24e10 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ env: matrix: include: - rust: nightly - env: FEATURES=ipv6-iterator,ipv6-methods,serde,serde-derive + env: FEATURES=ipv6-iterator,ipv6-methods script: - cargo build --features $FEATURES --verbose diff --git a/Cargo.toml b/Cargo.toml index 8421dc7..1fa71e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,8 +12,8 @@ categories = ["network-programming", "os"] [dependencies] clippy = {version = "0.0.104", optional = true} -serde = { version = ">=0.8.0, <2.0", optional = true } -serde_derive = { version = ">=0.8.0, <2.0", optional = true } +serde = ">=0.8.0, <2.0" +serde_derive = ">=0.8.0, <2.0" [dev-dependencies] serde_json = "1.0" diff --git a/src/ipv4.rs b/src/ipv4.rs index 3202a1c..52af26f 100644 --- a/src/ipv4.rs +++ b/src/ipv4.rs @@ -2,7 +2,6 @@ use std::fmt; use std::net::Ipv4Addr; use std::str::FromStr; -#[cfg(feature = "serde")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use common::{cidr_parts, parse_addr, parse_prefix, IpNetworkError}; @@ -16,7 +15,6 @@ pub struct Ipv4Network { prefix: u8, } -#[cfg(feature = "serde")] impl<'de> Deserialize<'de> for Ipv4Network { fn deserialize(deserializer: D) -> Result where @@ -27,7 +25,6 @@ impl<'de> Deserialize<'de> for Ipv4Network { } } -#[cfg(feature = "serde")] impl Serialize for Ipv4Network { fn serialize(&self, serializer: S) -> Result where diff --git a/src/ipv6.rs b/src/ipv6.rs index 6e3c1ac..d7ec53c 100644 --- a/src/ipv6.rs +++ b/src/ipv6.rs @@ -3,7 +3,6 @@ use std::fmt; use std::net::Ipv6Addr; use std::str::FromStr; -#[cfg(feature = "serde")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use common::{cidr_parts, parse_prefix, IpNetworkError}; @@ -18,7 +17,6 @@ pub struct Ipv6Network { prefix: u8, } -#[cfg(feature = "serde")] impl<'de> Deserialize<'de> for Ipv6Network { fn deserialize(deserializer: D) -> Result where @@ -29,7 +27,6 @@ impl<'de> Deserialize<'de> for Ipv6Network { } } -#[cfg(feature = "serde")] impl Serialize for Ipv6Network { fn serialize(&self, serializer: S) -> Result where diff --git a/src/lib.rs b/src/lib.rs index f5fffaa..b82ee12 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,9 +8,7 @@ #![crate_type = "lib"] #![doc(html_root_url = "https://docs.rs/ipnetwork/0.12.8")] -#[cfg(feature = "serde")] extern crate serde; -#[cfg(feature = "serde")] #[macro_use] extern crate serde_derive; @@ -29,9 +27,8 @@ pub use ipv6::{ipv6_mask_to_prefix, Ipv6Network}; /// Represents a generic network range. This type can have two variants: /// the v4 and the v6 case. -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "serde", serde(untagged))] -#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +#[serde(untagged)] pub enum IpNetwork { V4(Ipv4Network), V6(Ipv6Network), diff --git a/tests/test_json.rs b/tests/test_json.rs index 373290c..bcb625b 100644 --- a/tests/test_json.rs +++ b/tests/test_json.rs @@ -1,8 +1,7 @@ -#[cfg(feature = "serde")] extern crate serde; -#[cfg(feature = "serde")] + extern crate serde_json; -#[cfg(feature = "serde")] + #[macro_use] extern crate serde_derive; @@ -14,7 +13,6 @@ mod tests { use ipnetwork::{IpNetwork, Ipv4Network, Ipv6Network}; use std::net::{Ipv4Addr, Ipv6Addr}; - #[cfg(feature = "serde")] #[test] fn test_ipv4_json() { let json_string = r#"{"ipnetwork":"127.1.0.0/24"}"#; @@ -32,7 +30,6 @@ mod tests { assert_eq!(::serde_json::to_string(&mystruct).unwrap(), json_string); } - #[cfg(feature = "serde")] #[test] fn test_ipv6_json() { let json_string = r#"{"ipnetwork":"::1/0"}"#; @@ -53,7 +50,6 @@ mod tests { assert_eq!(::serde_json::to_string(&mystruct).unwrap(), json_string); } - #[cfg(feature = "serde")] #[test] fn test_ipnetwork_json() { let json_string = r#"{"ipnetwork":["127.1.0.0/24","::1/0"]}"#; From 2286d587289a4362da80f3880a22d83f216979c9 Mon Sep 17 00:00:00 2001 From: sharks Date: Tue, 17 Apr 2018 19:28:45 -0500 Subject: [PATCH 12/12] cargo fmt and clippy --- src/ipv4.rs | 12 +++--------- src/ipv6.rs | 5 +---- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/ipv4.rs b/src/ipv4.rs index 52af26f..8295890 100644 --- a/src/ipv4.rs +++ b/src/ipv4.rs @@ -41,10 +41,7 @@ impl Ipv4Network { if prefix > IPV4_BITS { Err(IpNetworkError::InvalidPrefix) } else { - Ok(Ipv4Network { - addr: addr, - prefix: prefix, - }) + Ok(Ipv4Network { addr, prefix }) } } @@ -54,10 +51,7 @@ impl Ipv4Network { pub fn iter(&self) -> Ipv4NetworkIterator { let start = u64::from(u32::from(self.network())); let end = start + self.size(); - Ipv4NetworkIterator { - next: start, - end: end, - } + Ipv4NetworkIterator { next: start, end } } pub fn ip(&self) -> Ipv4Addr { @@ -254,7 +248,7 @@ pub fn ipv4_mask_to_prefix(mask: Ipv4Addr) -> Result { let mask = u32::from(mask); let prefix = (!mask).leading_zeros() as u8; - if ((mask as u64) << prefix) & 0xffff_ffff != 0 { + if (u64::from(mask) << prefix) & 0xffff_ffff != 0 { Err(IpNetworkError::InvalidPrefix) } else { Ok(prefix) diff --git a/src/ipv6.rs b/src/ipv6.rs index d7ec53c..4f272ba 100644 --- a/src/ipv6.rs +++ b/src/ipv6.rs @@ -43,10 +43,7 @@ impl Ipv6Network { if prefix > IPV6_BITS { Err(IpNetworkError::InvalidPrefix) } else { - Ok(Ipv6Network { - addr: addr, - prefix: prefix, - }) + Ok(Ipv6Network { addr, prefix }) } }