Skip to content

Commit

Permalink
FromStr trait implementation for Name (#2212)
Browse files Browse the repository at this point in the history
Co-authored-by: Stefan Neamtu <stefan.neamtu@gmail.com>
  • Loading branch information
seanmonstar and stedfn authored Mar 26, 2024
1 parent 6768a8e commit 14e46ff
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/dns/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::collections::HashMap;
use std::future::Future;
use std::net::SocketAddr;
use std::pin::Pin;
use std::str::FromStr;
use std::sync::Arc;
use std::task::{Context, Poll};

Expand Down Expand Up @@ -40,6 +41,16 @@ impl Name {
}
}

impl FromStr for Name {
type Err = sealed::InvalidNameError;

fn from_str(host: &str) -> Result<Self, Self::Err> {
HyperName::from_str(host.into())
.map(Name)
.map_err(|_| sealed::InvalidNameError { _ext: () })
}
}

#[derive(Clone)]
pub(crate) struct DynResolver {
resolver: Arc<dyn Resolve>,
Expand Down Expand Up @@ -93,3 +104,20 @@ impl Resolve for DnsResolverWithOverrides {
}
}
}

mod sealed {
use std::fmt;

#[derive(Debug)]
pub struct InvalidNameError {
pub(super) _ext: (),
}

impl fmt::Display for InvalidNameError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("invalid DNS name")
}
}

impl std::error::Error for InvalidNameError {}
}

0 comments on commit 14e46ff

Please sign in to comment.