Skip to content

Commit

Permalink
Added to_string and display methods for TargetAddr
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohit Kulkarni committed Feb 14, 2025
1 parent 2eeb99c commit d27cbc9
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
borrow::Cow,
fmt,
io::Result as IoResult,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs},
pin::Pin,
Expand Down Expand Up @@ -110,6 +111,25 @@ pub enum TargetAddr<'a> {
Domain(Cow<'a, str>, u16),
}

impl<'a> fmt::Display for TargetAddr<'a> {

Check failure on line 114 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable)

the following explicit lifetimes could be elided: 'a

Check failure on line 114 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (beta)

the following explicit lifetimes could be elided: 'a

Check failure on line 114 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

the following explicit lifetimes could be elided: 'a
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TargetAddr::Ip(addr) => write!(f, "{}", addr),
TargetAddr::Domain(domain, port) => write!(f, "{}:{}", domain, port),
}
}
}

impl<'a> TargetAddr<'a> {

Check failure on line 123 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable)

the following explicit lifetimes could be elided: 'a

Check failure on line 123 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (beta)

the following explicit lifetimes could be elided: 'a

Check failure on line 123 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

the following explicit lifetimes could be elided: 'a
/// Converts the `TargetAddr` to a `String`.
pub fn to_string(&self) -> String {

Check failure on line 125 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable)

type `TargetAddr<'a>` implements inherent method `to_string(&self) -> String` which shadows the implementation of `Display`

Check failure on line 125 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (beta)

type `TargetAddr<'a>` implements inherent method `to_string(&self) -> String` which shadows the implementation of `Display`

Check failure on line 125 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

type `TargetAddr<'a>` implements inherent method `to_string(&self) -> String` which shadows the implementation of `Display`
match self {
TargetAddr::Ip(addr) => addr.to_string(),
TargetAddr::Domain(domain, port) => format!("{}:{}", domain, port),
}
}
}

impl<'a> TargetAddr<'a> {

Check failure on line 133 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (stable)

the following explicit lifetimes could be elided: 'a

Check failure on line 133 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (beta)

the following explicit lifetimes could be elided: 'a

Check failure on line 133 in src/lib.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

the following explicit lifetimes could be elided: 'a
/// Creates owned `TargetAddr` by cloning. It is usually used to eliminate
/// the lifetime bound.
Expand Down Expand Up @@ -236,7 +256,8 @@ impl IntoTargetAddr<'static> for (String, u16) {
}

impl<'a, T> IntoTargetAddr<'a> for &'a T
where T: IntoTargetAddr<'a> + Copy
where
T: IntoTargetAddr<'a> + Copy,
{
fn into_target_addr(self) -> Result<TargetAddr<'a>> {
(*self).into_target_addr()
Expand Down Expand Up @@ -274,6 +295,30 @@ mod tests {
Ok(block_on(t.to_proxy_addrs().map(Result::unwrap).collect()))
}

#[test]
fn test_display_ip() {
let addr = TargetAddr::Ip(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080));
assert_eq!(format!("{}", addr), "127.0.0.1:8080");
}

#[test]
fn test_display_domain() {
let addr = TargetAddr::Domain(Cow::Borrowed("example.com"), 80);
assert_eq!(format!("{}", addr), "example.com:80");
}

#[test]
fn test_to_string_ip() {
let addr = TargetAddr::Ip(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080));
assert_eq!(addr.to_string(), "127.0.0.1:8080");
}

#[test]
fn test_to_string_domain() {
let addr = TargetAddr::Domain(Cow::Borrowed("example.com"), 80);
assert_eq!(addr.to_string(), "example.com:80");
}

#[test]
fn converts_socket_addr_to_proxy_addrs() -> Result<()> {
let addr = SocketAddr::from(([1, 1, 1, 1], 443));
Expand Down Expand Up @@ -302,7 +347,9 @@ mod tests {
}

fn into_target_addr<'a, T>(t: T) -> Result<TargetAddr<'a>>
where T: IntoTargetAddr<'a> {
where
T: IntoTargetAddr<'a>,
{
t.into_target_addr()
}

Expand Down

0 comments on commit d27cbc9

Please sign in to comment.