Compare commits
No commits in common. "f2ad0de89b860c4eda43e73466775ad44d55edd7" and "247587d99b258f7fdb93c7f3744f4c501ed901ab" have entirely different histories.
f2ad0de89b
...
247587d99b
20
src/main.rs
20
src/main.rs
@ -1,23 +1,5 @@
|
|||||||
use std::{io, str::FromStr};
|
|
||||||
|
|
||||||
mod networking;
|
mod networking;
|
||||||
use networking::ip::{IpAddr};
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Enter the IP and CIDR for your given network");
|
println!("Hello, World!")
|
||||||
|
|
||||||
loop {
|
|
||||||
let mut ip = String::new();
|
|
||||||
io::stdin().read_line(&mut ip).unwrap();
|
|
||||||
let ip_address = match IpAddr::from_str(x) {
|
|
||||||
Err(_) => {
|
|
||||||
println!("fucked it");
|
|
||||||
IpAddr::V4(0, 0, 0, 0)
|
|
||||||
},
|
|
||||||
Ok(ip) => ip,
|
|
||||||
};
|
|
||||||
|
|
||||||
println!("you entered, {:?}", ip_address);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
|
||||||
pub struct InvalidIPErr;
|
|
||||||
|
|
||||||
/// Ip address enum that includes associated type
|
/// Ip address enum that includes associated type
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
@ -18,6 +15,9 @@ pub enum IpAddr {
|
|||||||
V6(String)
|
V6(String)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct InvalidIPErr;
|
||||||
|
|
||||||
impl ToString for IpAddr {
|
impl ToString for IpAddr {
|
||||||
/// Function that returns an IP address in string form
|
/// Function that returns an IP address in string form
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
@ -32,16 +32,6 @@ impl ToString for IpAddr {
|
|||||||
impl FromStr for IpAddr {
|
impl FromStr for IpAddr {
|
||||||
type Err = InvalidIPErr;
|
type Err = InvalidIPErr;
|
||||||
/// Function that generates a IpAddr / Err from a string
|
/// Function that generates a IpAddr / Err from a string
|
||||||
///
|
|
||||||
/// # Limitation
|
|
||||||
/// Currently only works for IPs of type IPv4
|
|
||||||
/// # Example
|
|
||||||
/// ```
|
|
||||||
/// let ip: &str = "127.0.0.1";
|
|
||||||
///
|
|
||||||
/// let parsed_ip: IpAddr = IpAddr::from_str(ip);
|
|
||||||
/// ///parsed_ip = IpAddr::V4(127,0,0,1)
|
|
||||||
/// ```
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
let split_ip = s.split('.');
|
let split_ip = s.split('.');
|
||||||
if split_ip.clone().count() != 4 {
|
if split_ip.clone().count() != 4 {
|
||||||
@ -51,10 +41,6 @@ impl FromStr for IpAddr {
|
|||||||
let mut ip: [u8; 4] = Default::default();
|
let mut ip: [u8; 4] = Default::default();
|
||||||
//Go through each octet and ensure it can be parsed;
|
//Go through each octet and ensure it can be parsed;
|
||||||
for (i, oct) in split_ip.into_iter().enumerate() {
|
for (i, oct) in split_ip.into_iter().enumerate() {
|
||||||
if i > ip.len() {
|
|
||||||
//Ip string is out of the range of the 4 octets in an IPv4 Address
|
|
||||||
return Err(InvalidIPErr)
|
|
||||||
}
|
|
||||||
match oct.parse::<u8>() {
|
match oct.parse::<u8>() {
|
||||||
Ok(parsed_oct) => ip[i] = parsed_oct,
|
Ok(parsed_oct) => ip[i] = parsed_oct,
|
||||||
Err(_) => return Err(InvalidIPErr)
|
Err(_) => return Err(InvalidIPErr)
|
||||||
@ -100,12 +86,9 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
///Tests if an invalid Ip will cause a panic when
|
|
||||||
/// converting from a string to an IpAddr
|
|
||||||
fn invalid_string_to_ip() {
|
fn invalid_string_to_ip() {
|
||||||
use super::*;
|
use super::*;
|
||||||
let ip = "127.0.0.0.1";
|
let ip = "Hello, world";
|
||||||
IpAddr::from_str(ip).unwrap();
|
IpAddr::from_str(ip).unwrap();
|
||||||
assert_eq!(IpAddr::from_str(ip), Err(InvalidIPErr))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
pub mod ip;
|
mod ip;
|
||||||
use ip::IpAddr;
|
use ip::IpAddr;
|
||||||
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user