From 17c3478673a57a9729c43a20820c44cc2dbdc4a9 Mon Sep 17 00:00:00 2001 From: Jacob Rothstein Date: Wed, 17 Apr 2024 11:58:22 -0700 Subject: [PATCH] feat(client): impl IntoUrl for IpAddr and SocketAddr for convenience --- client/src/into_url.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/client/src/into_url.rs b/client/src/into_url.rs index f3eda697f5..04d0dd03f1 100644 --- a/client/src/into_url.rs +++ b/client/src/into_url.rs @@ -1,5 +1,8 @@ use crate::{Error, Result}; -use std::str::FromStr; +use std::{ + net::{IpAddr, SocketAddr}, + str::FromStr, +}; use trillium_server_common::url::{ParseError, Url}; /// attempt to construct a url, with base if present @@ -65,3 +68,21 @@ impl> IntoUrl for Vec { self.as_slice().into_url(base) } } + +impl IntoUrl for SocketAddr { + /// note that http is assumed regardless of port + fn into_url(self, base: Option<&Url>) -> Result { + format!("http://{self}").into_url(base) + } +} + +impl IntoUrl for IpAddr { + /// note that http is assumed regardless of port + fn into_url(self, base: Option<&Url>) -> Result { + match self { + IpAddr::V4(v4) => format!("http://{v4}"), + IpAddr::V6(v6) => format!("http://[{v6}]"), + } + .into_url(base) + } +}