Added initial implementation of IpAddr
This commit is contained in:
parent
0f7eb5a102
commit
2033d9b2b8
45
.vscode/launch.json
vendored
Normal file
45
.vscode/launch.json
vendored
Normal file
@ -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}"
|
||||
}
|
||||
]
|
||||
}
|
57
src/ip.rs
Normal file
57
src/ip.rs
Normal file
@ -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<Self, Self::Err> {
|
||||
// //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")
|
||||
}
|
||||
}
|
@ -1,3 +1,6 @@
|
||||
mod network;
|
||||
mod ip;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
12
src/network.rs
Normal file
12
src/network.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use crate::ip::IpAddr;
|
||||
|
||||
pub struct Network {
|
||||
network_address: IpAddr,
|
||||
broadcast_addr: IpAddr,
|
||||
subnet_mask: Option<IpAddr>
|
||||
}
|
||||
|
||||
impl Network {
|
||||
// pub fn generate_subnets(self) -> Vec<Network> {}
|
||||
// fn get_net_id(self) -> u8 {}
|
||||
}
|
Loading…
Reference in New Issue
Block a user