summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--learn_16/Cargo.lock7
-rw-r--r--learn_16/Cargo.toml6
-rw-r--r--learn_16/src/main.rs116
3 files changed, 129 insertions, 0 deletions
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<Endpoint> 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<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 mut ep: Endpoint = Endpoint::default();
+ ep.port = value.port;
+
+ for i in 0..4 {
+ match addr_elts[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],
+ ),
+ ));
+ },
+ }
+ }
+ 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:?}"),
+ }
+ }
+}