From 4074e05a8291489ed7bc7f59cfa7802d3d76221c Mon Sep 17 00:00:00 2001 From: Brad Clawsie Date: Fri, 20 Nov 2026 15:58:34 -0600 Subject: wip: learn_16 commit e04349d4c1c82c7940b09233f5cc50cf27ca6651 Author: Brad Clawsie Date: Fri Nov 20 15:58:20 2026 -0600 wip: learn_16 --- learn_16/Cargo.lock | 7 ++++ learn_16/Cargo.toml | 6 +++ learn_16/src/main.rs | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 learn_16/Cargo.lock create mode 100644 learn_16/Cargo.toml create mode 100644 learn_16/src/main.rs diff --git a/learn_16/Cargo.lock b/learn_16/Cargo.lock new file mode 100644 index 0000000..44cec27 --- /dev/null +++ b/learn_16/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "learn_16" +version = "0.1.0" diff --git a/learn_16/Cargo.toml b/learn_16/Cargo.toml new file mode 100644 index 0000000..5d82522 --- /dev/null +++ b/learn_16/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "learn_16" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/learn_16/src/main.rs b/learn_16/src/main.rs new file mode 100644 index 0000000..e8f4379 --- /dev/null +++ b/learn_16/src/main.rs @@ -0,0 +1,116 @@ +#[derive(Debug, PartialEq, Eq)] +struct Endpoint { + address: [u8; 4], + port: u16, +} + +impl Default for Endpoint { + fn default() -> Self { + Self { + address: [0; 4], + port: 0, + } + } +} + +#[derive(Debug, PartialEq, Eq)] +struct EndpointText { + address: String, + port: u16, +} + +impl From for EndpointText { + fn from(endpoint: Endpoint) -> Self { + Self { + address: format!("{}.{}.{}.{}", + endpoint.address[0], + endpoint.address[1], + endpoint.address[2], + endpoint.address[3], + ), + port: endpoint.port, + } + } +} + +#[derive(Debug, PartialEq, Eq)] +enum ParseEndpointError { + InvalidAddress(String), +} + +impl TryFrom for Endpoint { + type Error = ParseEndpointError; + + fn try_from(value: EndpointText) -> Result { + let addr_elts: Vec<&str> = value.address.split('.').collect(); + if addr_elts.len() != 4 { + return Err(Self::Error::InvalidAddress( + format!( + "{} does not seem to be dotted quad", + value.address, + ), + )); + } + + let mut ep: Endpoint = Endpoint::default(); + ep.port = value.port; + + for i in 0..4 { + match addr_elts[i].parse::() { + Ok(v) => { + ep.address[i] = v; + }, + _ => { + return Err(Self::Error::InvalidAddress( + format!( + "{} elt {} ({}) is not u8", + value.address, i, addr_elts[i], + ), + )); + }, + } + } + Ok(ep) + } +} + +fn main() { + () +} + +#[cfg(test)] +mod tests { + use crate::*; + + #[test] + fn test_default() { + let et_min = EndpointText::from(Endpoint{ + address: [0,0,0,0], + port: 0, + }); + assert_eq!(et_min.address, "0.0.0.0".to_string()); + assert_eq!(et_min.port, 0); + + let et_max = EndpointText::from(Endpoint{ + address: [255,255,255,255], + port: 65535, + }); + assert_eq!(et_max.address, "255.255.255.255".to_string()); + assert_eq!(et_max.port, 65535); + } + + #[test] + fn test_tryfrom() { + let t0 = + Endpoint::try_from( + EndpointText{address:"".to_string(), port:0}, + ); + + match t0 { + Err(ParseEndpointError::InvalidAddress(msg)) => { + assert!(msg.contains("dotted quad")); + } + other => panic!("expected InvalidAddress error, got {other:?}"), + } + } +} -- cgit v1.2.3