From d792ea8c3092f760489d274c7840e1fe2c281f07 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Mon, 13 Feb 2023 22:05:33 +0000 Subject: [PATCH] Added CIDR parser -> Really Janky code so will look to review / refactor the method at some point in the very near future --- src/main.rs | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index cac86fc..aeb8016 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,21 +3,44 @@ use std::{io, str::FromStr}; mod networking; use networking::ip::{IpAddr}; +#[allow(unused_variables, unused_mut, unused_assignments)] fn main() { - println!("Enter the IP and CIDR for your given network"); + println!("Enter the IP and cidr for your given network"); loop { - let mut ip = String::new(); - io::stdin().read_line(&mut ip).unwrap(); + let mut ip_buf = String::new(); + 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::() { + Err(_) => continue, + Ok(cidr) => cidr + } + }; + println!("CIDR: {:?}", cidr); + } + let ip_address = match IpAddr::from_str(&ip.trim()) { Err(_) => { - println!("{} is an invalid IP Address... Please try again", ip); + println!("{} is an invalid IP Address... Please try again", ip_buf); continue; }, Ok(ip) => ip, }; - println!("you entered, {:?}", ip_address); + println!("IP: {:?}", ip_address); + + break; } }