Skip to content

Commit 607c4da

Browse files
faernseanmonstar
authored andcommitted
feat(client): add FromStr impl for Name
1 parent ec7b93c commit 607c4da

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/client/connect/dns.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
//! - The [`Resolve`](Resolve) trait and related types to build a custom
88
//! resolver for use with the `HttpConnector`.
99
use std::{fmt, io, vec};
10+
use std::error::Error;
1011
use std::net::{
1112
IpAddr, Ipv4Addr, Ipv6Addr,
1213
SocketAddr, ToSocketAddrs,
1314
SocketAddrV4, SocketAddrV6,
1415
};
16+
use std::str::FromStr;
1517
use std::sync::Arc;
1618

1719
use futures::{Async, Future, Poll};
@@ -72,6 +74,32 @@ impl fmt::Debug for Name {
7274
}
7375
}
7476

77+
impl FromStr for Name {
78+
type Err = InvalidNameError;
79+
80+
fn from_str(host: &str) -> Result<Self, Self::Err> {
81+
// Possibly add validation later
82+
Ok(Name::new(host.to_owned()))
83+
}
84+
}
85+
86+
/// Error indicating a given string was not a valid domain name.
87+
#[derive(Debug)]
88+
pub struct InvalidNameError(());
89+
90+
impl fmt::Display for InvalidNameError {
91+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92+
self.description().fmt(f)
93+
}
94+
}
95+
96+
impl Error for InvalidNameError {
97+
fn description(&self) -> &str {
98+
"Not a valid domain name"
99+
}
100+
}
101+
102+
75103
impl GaiResolver {
76104
/// Construct a new `GaiResolver`.
77105
///
@@ -317,4 +345,10 @@ mod tests {
317345
assert!(preferred.next().unwrap().is_ipv6());
318346
assert!(fallback.next().unwrap().is_ipv4());
319347
}
348+
349+
#[test]
350+
fn test_name_from_str() {
351+
let name = Name::from_str("test.example.com").expect("Should be a valid domain");
352+
assert_eq!(name.as_str(), "test.example.com");
353+
}
320354
}

0 commit comments

Comments
 (0)