Created new function to get IP and CIDR from a string independently + test

This commit is contained in:
Luke Else 2023-02-20 18:06:57 +00:00
parent d792ea8c30
commit 6567553f8a
2 changed files with 53 additions and 27 deletions

View File

@ -1,46 +1,26 @@
use std::{io, str::FromStr}; use std::{io};
mod networking; mod networking;
use networking::ip::{IpAddr};
#[allow(unused_variables, unused_mut, unused_assignments)] #[allow(unused_variables, unused_mut, unused_assignments)]
fn main() { fn main() {
println!("Enter the IP and cidr for your given network"); println!("Enter the IP and cidr for your given network");
loop { loop {
let mut cidr: u8;
let mut ip: String = Default::default();
let mut ip_buf = String::new(); let mut ip_buf = String::new();
io::stdin().read_line(&mut ip_buf).unwrap_or_default(); io::stdin().read_line(&mut ip_buf).unwrap_or_default();
let mut cidr: u8; let ip_and_cidr = match networking::ip_and_cidr_from_string(&ip_buf) {
let mut ip: String = Default::default();
if ip_buf.contains("/") {
let split_ip_cidr = ip_buf.split("/");
if split_ip_cidr.clone().count() > 2 {
continue;
}
ip = split_ip_cidr.clone().into_iter().next().unwrap_or("0.0.0.0").to_owned();
cidr = match split_ip_cidr.into_iter().last() {
None => continue,
Some(cidr) => match cidr.trim().parse::<u8>() {
Err(_) => continue,
Ok(cidr) => cidr
}
};
println!("CIDR: {:?}", cidr);
}
let ip_address = match IpAddr::from_str(&ip.trim()) {
Err(_) => { Err(_) => {
println!("{} is an invalid IP Address... Please try again", ip_buf); println!("{} is an invalid IP Address... Please try again", ip_buf);
continue; continue;
}, },
Ok(ip) => ip, Ok(ip_and_cidr) => ip_and_cidr
}; };
println!("IP: {:?}", ip_address);
break; break;
} }
} }

View File

@ -1,6 +1,8 @@
pub mod ip; use std::{str::FromStr};
use ip::IpAddr; use ip::IpAddr;
pub mod ip;
#[allow(unused)] #[allow(unused)]
pub struct Network { pub struct Network {
network_address: IpAddr, network_address: IpAddr,
@ -11,4 +13,48 @@ pub struct Network {
impl Network { impl Network {
// pub fn generate_subnets(self) -> Vec<Network> {} // pub fn generate_subnets(self) -> Vec<Network> {}
// fn get_net_id(self) -> u8 {} // fn get_net_id(self) -> u8 {}
}
pub fn ip_and_cidr_from_string(ip_and_cidr: &String) -> Result<(IpAddr, u8), ip::InvalidIPErr>{
let mut cidr: u8 = Default::default();
let mut ip: String = Default::default();
if ip_and_cidr.contains("/") {
let split_ip_cidr = ip_and_cidr.split("/");
if split_ip_cidr.clone().count() > 2 {
return Err(ip::InvalidIPErr);
}
ip = split_ip_cidr.clone().into_iter().next().unwrap_or("0.0.0.0").to_owned();
cidr = match split_ip_cidr.into_iter().last() {
None => return Err(ip::InvalidIPErr),
Some(cidr) => match cidr.trim().parse::<u8>() {
Err(_) => return Err(ip::InvalidIPErr),
Ok(cidr) => cidr
}
};
println!("CIDR: {:?}", cidr);
}
let ip_address: IpAddr = match IpAddr::from_str(&ip.trim()) {
Err(_) => return Err(ip::InvalidIPErr),
Ok(ip) => ip,
};
return Ok((ip_address, cidr))
}
mod tests {
#[cfg(test)]
#[test]
fn string_to_ip_cidr_test() {
use super::*;
let ip_string = String::from("127.0.0.1/8");
let result = match ip_and_cidr_from_string(&ip_string) {
Err(_) => panic!(),
Ok(ip_and_cidr) => ip_and_cidr
};
assert_eq!(result.0, IpAddr::V4(127, 0, 0, 1));
assert_eq!(result.1, 8);
}
} }