Added new functionality to Network and changed error handling to be a single enum. create_subnet() is still WIP
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
use std::str::FromStr;
|
||||
use super::NetworkingErr;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct InvalidIPErr;
|
||||
@ -12,7 +13,7 @@ pub struct InvalidIPErr;
|
||||
/// IpAddr::V6(String::from("::1"))
|
||||
/// ```
|
||||
#[allow(unused)]
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
pub enum IpAddr {
|
||||
V4(u8, u8, u8, u8),
|
||||
V6(String)
|
||||
@ -30,7 +31,7 @@ impl ToString for IpAddr {
|
||||
|
||||
|
||||
impl FromStr for IpAddr {
|
||||
type Err = InvalidIPErr;
|
||||
type Err = NetworkingErr;
|
||||
/// Function that generates a IpAddr / Err from a string
|
||||
///
|
||||
/// # Limitation
|
||||
@ -46,18 +47,18 @@ impl FromStr for IpAddr {
|
||||
let split_ip = s.split('.');
|
||||
if split_ip.clone().count() != 4 {
|
||||
//Invalid IP address entered
|
||||
return Err(InvalidIPErr)
|
||||
return Err(super::NetworkingErr::InvalidIPErr)
|
||||
}
|
||||
let mut ip: [u8; 4] = Default::default();
|
||||
//Go through each octet and ensure it can be parsed;
|
||||
for (i, oct) in split_ip.into_iter().enumerate() {
|
||||
if i > ip.len() {
|
||||
//Ip string is out of the range of the 4 octets in an IPv4 Address
|
||||
return Err(InvalidIPErr)
|
||||
return Err(NetworkingErr::InvalidIPErr)
|
||||
}
|
||||
match oct.parse::<u8>() {
|
||||
Ok(parsed_oct) => ip[i] = parsed_oct,
|
||||
Err(_) => return Err(InvalidIPErr)
|
||||
Err(_) => return Err(NetworkingErr::InvalidIPErr)
|
||||
}
|
||||
}
|
||||
Ok(IpAddr::V4(ip[0],ip[1],ip[2],ip[3]))
|
||||
@ -106,6 +107,6 @@ mod tests {
|
||||
use super::*;
|
||||
let ip = "127.0.0.0.1";
|
||||
IpAddr::from_str(ip).unwrap();
|
||||
assert_eq!(IpAddr::from_str(ip), Err(InvalidIPErr))
|
||||
assert_eq!(IpAddr::from_str(ip), Err(NetworkingErr::InvalidIPErr))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user