diff options
| author | Brad Clawsie <brad@b7j0c.org> | 2026-11-21 12:22:27 -0600 |
|---|---|---|
| committer | Brad Clawsie <brad@b7j0c.org> | 2026-11-21 12:22:27 -0600 |
| commit | 8ffa5037cf935d6f4551c3b3dbda9d8a16af1bae (patch) | |
| tree | f1ffedd82d39a8e84814f51c30f537d8c08d6ce6 /learn_16/src | |
| parent | 4074e05a8291489ed7bc7f59cfa7802d3d76221c (diff) | |
feat: learn_16
Diffstat (limited to 'learn_16/src')
| -rw-r--r-- | learn_16/src/main.rs | 105 |
1 files changed, 82 insertions, 23 deletions
diff --git a/learn_16/src/main.rs b/learn_16/src/main.rs index e8f4379..c382094 100644 --- a/learn_16/src/main.rs +++ b/learn_16/src/main.rs @@ -35,38 +35,37 @@ impl From<Endpoint> for EndpointText { #[derive(Debug, PartialEq, Eq)] enum ParseEndpointError { - InvalidAddress(String), + WrongComponentCount, + EmptyComponent, + InvalidComponent, + OutOfRangeComponent, } impl TryFrom<EndpointText> for Endpoint { type Error = ParseEndpointError; fn try_from(value: EndpointText) -> Result<Self, Self::Error> { - 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 address_component: Vec<&str> = value.address.split('.').collect(); + if address_component.len() != 4 { + return Err(Self::Error::WrongComponentCount); } let mut ep: Endpoint = Endpoint::default(); ep.port = value.port; for i in 0..4 { - match addr_elts[i].parse::<u8>() { + if address_component[i].is_empty() { + return Err(Self::Error::EmptyComponent); + } + if !address_component[i].bytes().all(|x| x.is_ascii_digit()) { + return Err(Self::Error::InvalidComponent); + } + match address_component[i].parse::<u8>() { Ok(v) => { ep.address[i] = v; }, _ => { - return Err(Self::Error::InvalidAddress( - format!( - "{} elt {} ({}) is not u8", - value.address, i, addr_elts[i], - ), - )); + return Err(Self::Error::OutOfRangeComponent); }, } } @@ -91,26 +90,86 @@ mod tests { assert_eq!(et_min.address, "0.0.0.0".to_string()); assert_eq!(et_min.port, 0); + let et_min_into: EndpointText = Endpoint{ + address: [0,0,0,0], + port: 0, + }.into(); + assert_eq!(et_min_into.address, "0.0.0.0".to_string()); + assert_eq!(et_min_into.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); + + let et_max_into: EndpointText = Endpoint{ + address: [255,255,255,255], + port: 65335, + }.into(); + assert_eq!(et_max_into.address, "255.255.255.255".to_string()); + assert_eq!(et_max_into.port, 65335); + } #[test] - fn test_tryfrom() { + fn test_tryfrom_valid() { let t0 = Endpoint::try_from( - EndpointText{address:"".to_string(), port:0}, + EndpointText{address: "192.168.1.10".to_string(), port:0}, ); + assert_eq!(t0, Ok(Endpoint{ address: [192,168,1,10], port:0})); - match t0 { - Err(ParseEndpointError::InvalidAddress(msg)) => { - assert!(msg.contains("dotted quad")); - } - other => panic!("expected InvalidAddress error, got {other:?}"), + let t1 = + Endpoint::try_from( + EndpointText{address: "001.002.030.004".to_string(), port:0}, + ); + assert_eq!(t1, Ok(Endpoint{ address: [1,2,30,4], port:0})); + } + + #[test] + fn test_tryfrom_invalid_dotted_quad() { + for sample in ["", "1.2.3", "1.2.3.4.5"] { + let t = + Endpoint::try_from( + EndpointText{address:sample.to_string(), port:0}, + ); + assert_eq!(t, Err(ParseEndpointError::WrongComponentCount)); + } + } + + #[test] + fn test_tryfrom_empty_component() { + for sample in ["0.0.0.", ".0.0.0", "1..xyz.4"] { + let t = + Endpoint::try_from( + EndpointText{address:sample.to_string(), port:0}, + ); + assert_eq!(t, Err(ParseEndpointError::EmptyComponent)); + } + } + + #[test] + fn test_tryfrom_invalid_component() { + for sample in ["0.0.0.-1", "x.0.0.0", "0.0.x.0", + "1.a.3.4", "1.-2.3.4"] { + let t = + Endpoint::try_from( + EndpointText{address:sample.to_string(), port:0}, + ); + assert_eq!(t, Err(ParseEndpointError::InvalidComponent)); + } + } + + #[test] + fn test_tryfrom_out_of_range_component() { + for sample in ["1.256.3.4", "1.2.99999999999.4"] { + let t = + Endpoint::try_from( + EndpointText{address:sample.to_string(), port:0}, + ); + assert_eq!(t, Err(ParseEndpointError::OutOfRangeComponent)); } } } |
