Skip to content

Commit

Permalink
feat(client): impl IntoUrl for IpAddr and SocketAddr for convenience
Browse files Browse the repository at this point in the history
  • Loading branch information
jbr committed Apr 17, 2024
1 parent 0bc2435 commit 17c3478
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion client/src/into_url.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -65,3 +68,21 @@ impl<S: AsRef<str>> IntoUrl for Vec<S> {
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<Url> {
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<Url> {
match self {
IpAddr::V4(v4) => format!("http://{v4}"),
IpAddr::V6(v6) => format!("http://[{v6}]"),
}
.into_url(base)
}
}

0 comments on commit 17c3478

Please sign in to comment.