Repository: achanda/ipnetwork (language: rust)

Issue:
code for cidr_parts not too correct
If one specified more than one `/` (for parsing into Ipv4Network), the error displayed would be:

> CIDR must contain '/'


Discussion / hints:

Fix patch:
diff --git a/src/common.rs b/src/common.rs
index 43c1169..943894d 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -35,10 +35,12 @@ impl Error for IpNetworkError {
 
 pub fn cidr_parts(cidr: &str) -> Result<(&str, &str), IpNetworkError> {
     let parts = cidr.split('/').collect::<Vec<&str>>();
-    if parts.len() == 2 {
+    if parts.len() == 1 {
+        Err(IpNetworkError::InvalidCidrFormat(format!("CIDR must contain '/': {}", cidr)))
+    } else if parts.len() == 2 {
         Ok((parts[0], parts[1]))
     } else {
-        Err(IpNetworkError::InvalidCidrFormat(format!("CIDR must contain '/': {}", cidr)))
+        Err(IpNetworkError::InvalidCidrFormat(format!("CIDR must contain a single '/': {}", cidr)))
     }
 }
 
diff --git a/src/ipv4.rs b/src/ipv4.rs
index 0f6d033..fb8569b 100644
--- a/src/ipv4.rs
+++ b/src/ipv4.rs
@@ -308,6 +308,12 @@ mod test {
         assert_eq!(None, cidr);
     }
 
+    #[test]
+    fn parse_v4_fail_two_slashes() {
+        let cidr: Option<Ipv4Network> = "10.1.1.1/24/".parse().ok();
+        assert_eq!(None, cidr);
+    }
+
     #[test]
     fn size_v4_24bit() {
         let net: Ipv4Network = "0/24".parse().unwrap();
diff --git a/src/ipv6.rs b/src/ipv6.rs
index 0cb859e..3ed3833 100644
--- a/src/ipv6.rs
+++ b/src/ipv6.rs
@@ -292,6 +292,12 @@ mod test {
         assert_eq!(None, cidr);
     }
 
+    #[test]
+    fn parse_v6_fail_two_slashes() {
+        let cidr: Option<Ipv6Network> = "::1/24/".parse().ok();
+        assert_eq!(None, cidr);
+    }
+
     #[test]
     fn mask_v6() {
         let cidr = Ipv6Network::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0), 40).unwrap();


Test patch: