Created new errors for CIDR / Subnet mask generation
This commit is contained in:
parent
d1d4bc02ca
commit
a067a3e349
@ -1,10 +1,11 @@
|
|||||||
use std::{str::FromStr};
|
use std::{str::FromStr};
|
||||||
use ip::IpAddr;
|
use ip::IpAddr;
|
||||||
|
|
||||||
use self::ip::InvalidIPErr;
|
|
||||||
|
|
||||||
pub mod ip;
|
pub mod ip;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub struct InvalidCIDRErr;
|
||||||
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub struct Network {
|
pub struct Network {
|
||||||
network_address: IpAddr,
|
network_address: IpAddr,
|
||||||
@ -13,8 +14,8 @@ pub struct Network {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Network {
|
impl Network {
|
||||||
// pub fn new(given_address: IpAddr, CIDR: u8) -> Network {
|
// pub fn new(given_address: &IpAddr, CIDR: u8) -> Network {
|
||||||
// //Generate subnet Mask
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
@ -27,9 +28,9 @@ impl Network {
|
|||||||
///
|
///
|
||||||
/// >> IpAddr::V4(255, 255, 252, 0)
|
/// >> IpAddr::V4(255, 255, 252, 0)
|
||||||
/// ```
|
/// ```
|
||||||
pub fn gen_subnet_mask(mut CIDR: u8) -> Result<IpAddr, InvalidIPErr> {
|
pub fn gen_subnet_mask(mut CIDR: u8) -> Result<IpAddr, InvalidCIDRErr> {
|
||||||
if CIDR > 32 {
|
if CIDR > 32 {
|
||||||
return Err(InvalidIPErr)
|
return Err(InvalidCIDRErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut oct: [u8; 4] = [0; 4];
|
let mut oct: [u8; 4] = [0; 4];
|
||||||
@ -52,7 +53,6 @@ impl Network {
|
|||||||
Ok(IpAddr::V4(oct[0], oct[1], oct[2], oct[3]))
|
Ok(IpAddr::V4(oct[0], oct[1], oct[2], oct[3]))
|
||||||
}
|
}
|
||||||
//pub fn generate_subnets(self) -> Vec<Network> {}
|
//pub fn generate_subnets(self) -> Vec<Network> {}
|
||||||
//fn get_net_id(self) -> u8 {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum NetworkClassBits {
|
enum NetworkClassBits {
|
||||||
@ -118,11 +118,11 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn cidr_to_ip_test() {
|
fn cidr_to_ip_test() {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
assert_eq!(Network::gen_subnet_mask(22).unwrap(), IpAddr::V4(255, 255, 252, 0));
|
assert_eq!(Network::gen_subnet_mask(22).unwrap(), IpAddr::V4(255, 255, 252, 0));
|
||||||
assert_eq!(Network::gen_subnet_mask(24).unwrap(), IpAddr::V4(255, 255, 255, 0));
|
assert_eq!(Network::gen_subnet_mask(24).unwrap(), IpAddr::V4(255, 255, 255, 0));
|
||||||
assert_eq!(Network::gen_subnet_mask(0).unwrap(), IpAddr::V4(0, 0, 0, 0));
|
assert_eq!(Network::gen_subnet_mask(0).unwrap(), IpAddr::V4(0, 0, 0, 0));
|
||||||
assert_eq!(Network::gen_subnet_mask(4).unwrap(), IpAddr::V4(240, 0, 0, 0));
|
assert_eq!(Network::gen_subnet_mask(4).unwrap(), IpAddr::V4(240, 0, 0, 0));
|
||||||
assert_eq!(Network::gen_subnet_mask(35).unwrap_err(), InvalidIPErr);
|
assert_eq!(Network::gen_subnet_mask(35).unwrap_err(), InvalidCIDRErr);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user