From da7ba3265e9d96fe5873c760d8e63febe62a6611 Mon Sep 17 00:00:00 2001 From: Anthony Turcios Date: Thu, 2 Apr 2026 09:45:07 -0400 Subject: [PATCH] introduce Uri::try_new method --- Cargo.toml | 1 + src/uri.rs | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 27badc8..8720d19 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ hyper-util = { version = "0.1.2", optional = true } tokio = { version = "1.35", default-features = false, features = ["net"] } tower-service = { version = "0.3", optional = true } pin-project-lite = "0.2" +http = "1.4.0" [dev-dependencies] thiserror = "1.0" diff --git a/src/uri.rs b/src/uri.rs index 9c5d23e..b7cd1a0 100644 --- a/src/uri.rs +++ b/src/uri.rs @@ -1,3 +1,4 @@ +use http::uri::InvalidUri; use hyper::Uri as HyperUri; use std::path::Path; @@ -26,11 +27,22 @@ impl Uri { socket: impl AsRef, path: &str, ) -> Self { + Self::try_new(socket, path).unwrap() + } + + /// Create a new `[Uri]` from a socket address and a path + /// + /// # Errors + /// Returns `http::uri::InvalidUri` if the combined host and path are malformed. + pub fn try_new( + socket: impl AsRef, + path: &str, + ) -> Result { let host = hex::encode(socket.as_ref().to_string_lossy().as_bytes()); let host_str = format!("unix://{host}:0{path}"); - let hyper_uri: HyperUri = host_str.parse().unwrap(); + let hyper_uri: HyperUri = host_str.parse()?; - Self { hyper_uri } + Ok(Self { hyper_uri }) } } @@ -51,4 +63,13 @@ mod tests { let expected: HyperUri = "unix://666f6f2e736f636b:0/".parse().unwrap(); assert_eq!(unix, expected); } + + #[test] + fn test_try_new_uri() { + let good_unix_result = Uri::try_new("foo.sock", "/some/valid/path"); + assert!(good_unix_result.is_ok()); + + let bad_unix_result = Uri::try_new("bar.sock", "/some/bad/path/?€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€"); + assert!(bad_unix_result.is_err()); + } }