Skip to content

Commit 14e46ff

Browse files
seanmonstarstedfn
andauthored
FromStr trait implementation for Name (#2212)
Co-authored-by: Stefan Neamtu <stefan.neamtu@gmail.com>
1 parent 6768a8e commit 14e46ff

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/dns/resolve.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::collections::HashMap;
55
use std::future::Future;
66
use std::net::SocketAddr;
77
use std::pin::Pin;
8+
use std::str::FromStr;
89
use std::sync::Arc;
910
use std::task::{Context, Poll};
1011

@@ -40,6 +41,16 @@ impl Name {
4041
}
4142
}
4243

44+
impl FromStr for Name {
45+
type Err = sealed::InvalidNameError;
46+
47+
fn from_str(host: &str) -> Result<Self, Self::Err> {
48+
HyperName::from_str(host.into())
49+
.map(Name)
50+
.map_err(|_| sealed::InvalidNameError { _ext: () })
51+
}
52+
}
53+
4354
#[derive(Clone)]
4455
pub(crate) struct DynResolver {
4556
resolver: Arc<dyn Resolve>,
@@ -93,3 +104,20 @@ impl Resolve for DnsResolverWithOverrides {
93104
}
94105
}
95106
}
107+
108+
mod sealed {
109+
use std::fmt;
110+
111+
#[derive(Debug)]
112+
pub struct InvalidNameError {
113+
pub(super) _ext: (),
114+
}
115+
116+
impl fmt::Display for InvalidNameError {
117+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118+
f.write_str("invalid DNS name")
119+
}
120+
}
121+
122+
impl std::error::Error for InvalidNameError {}
123+
}

0 commit comments

Comments
 (0)