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:
		
							
								
								
									
										33
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								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::<u8>() {
 | 
			
		||||
                    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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user