From 2033d9b2b864bf9136fb8474fe80a006f2dfb7e8 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Tue, 31 Jan 2023 20:58:13 +0000 Subject: [PATCH] Added initial implementation of IpAddr --- .vscode/launch.json | 45 +++++++++++++++++++++++++++++++++++ src/ip.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 3 +++ src/network.rs | 12 ++++++++++ 4 files changed, 117 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 src/ip.rs create mode 100644 src/network.rs diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..e60077e --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,45 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'subnet_calculator'", + "cargo": { + "args": [ + "build", + "--bin=subnet_calculator", + "--package=subnet_calculator" + ], + "filter": { + "name": "subnet_calculator", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'subnet_calculator'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=subnet_calculator", + "--package=subnet_calculator" + ], + "filter": { + "name": "subnet_calculator", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/src/ip.rs b/src/ip.rs new file mode 100644 index 0000000..90bbdc4 --- /dev/null +++ b/src/ip.rs @@ -0,0 +1,57 @@ +//use std::str::FromStr; + +/// Ip address enum that includes associated type +/// +/// # Example +/// ``` +/// //Loopback Addresses: +/// IpAddr::V4(127, 0, 0, 1) +/// IpAddr::V6(String::from("::1")) +/// ``` +#[allow(unused)] +pub enum IpAddr { + V4(u8, u8, u8, u8), + V6(String) +} + +impl ToString for IpAddr { + /// Function that returns an IP address in string form + fn to_string(&self) -> String { + match self { + IpAddr::V4(oct1, oct2, oct3, oct4) => format!("{}.{}.{}.{}", oct1, oct2, oct3, oct4), + IpAddr::V6(addr) => format!("{}", addr) + } + } +} + +// impl FromStr for IpAddr { +// /// Function that generates a IpAddr / Err from a string +// fn from_str(s: &str) -> Result { +// //Awating implementation +// } +// } + +/// Function that takes in an IP address and then prints a formatted string to the CLI +/// +/// # Example +/// ``` +/// let ip = IpAddr::V4(127, 0, 0, 1); +/// +/// print_ip(ip); +/// //Output: IP Address: 127.0.0.1 +/// ``` +pub fn print_ip(ip_address: IpAddr) { + println!("IP Address: {}", ip_address.to_string()) +} + +mod tests { + #[test] + /// Tests if an IP is converted to a string + /// correctly using the to_string trait + fn ip_to_string() { + use super::*; + + let ip = IpAddr::V4(192, 168, 0, 1); + assert_eq!(ip.to_string(), "192.168.0.1") + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e7a11a9..e8eae6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +mod network; +mod ip; + fn main() { println!("Hello, world!"); } diff --git a/src/network.rs b/src/network.rs new file mode 100644 index 0000000..487c281 --- /dev/null +++ b/src/network.rs @@ -0,0 +1,12 @@ +use crate::ip::IpAddr; + +pub struct Network { + network_address: IpAddr, + broadcast_addr: IpAddr, + subnet_mask: Option +} + +impl Network { + // pub fn generate_subnets(self) -> Vec {} + // fn get_net_id(self) -> u8 {} +} \ No newline at end of file