Added CIDR parser -> Really Janky code so will look to review / refactor the method at some point in the very near future

This commit is contained in:
Luke Else 2023-02-13 22:05:33 +00:00
parent 4f8412f3d0
commit d792ea8c30

View File

@ -3,21 +3,44 @@ use std::{io, str::FromStr};
mod networking; mod networking;
use networking::ip::{IpAddr}; use networking::ip::{IpAddr};
#[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 ip = String::new(); let mut ip_buf = String::new();
io::stdin().read_line(&mut ip).unwrap(); io::stdin().read_line(&mut ip_buf).unwrap_or_default();
let mut cidr: u8;
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()) { let ip_address = match IpAddr::from_str(&ip.trim()) {
Err(_) => { Err(_) => {
println!("{} is an invalid IP Address... Please try again", ip); println!("{} is an invalid IP Address... Please try again", ip_buf);
continue; continue;
}, },
Ok(ip) => ip, Ok(ip) => ip,
}; };
println!("you entered, {:?}", ip_address); println!("IP: {:?}", ip_address);
break;
} }
} }