From c809542c830c8d542877a22dd54b1c5c679ae433 Mon Sep 17 00:00:00 2001 From: Lucio Franco Date: Tue, 15 Jan 2019 12:45:30 -0500 Subject: [PATCH] feat(client): add `Destination::try_from_uri` constructor This change adds a try_from_uri function for creating Destinations outside of the hyper crate. The Destination can only be built if the uri contains a valid authority and scheme as these are required to build a Destination. --- src/client/connect/mod.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/client/connect/mod.rs b/src/client/connect/mod.rs index 31a097b687..57fd12f96d 100644 --- a/src/client/connect/mod.rs +++ b/src/client/connect/mod.rs @@ -61,6 +61,18 @@ pub(super) enum Alpn { } impl Destination { + /// Try to convert a `Uri` into a `Destination` + /// + /// # Error + /// + /// Returns an error if the uri contains no authority or + /// no scheme. + pub fn try_from_uri(uri: Uri) -> ::Result { + uri.authority_part().ok_or(::error::Parse::Uri)?; + uri.scheme_part().ok_or(::error::Parse::Uri)?; + Ok(Destination { uri }) + } + /// Get the protocol scheme. #[inline] pub fn scheme(&self) -> &str { @@ -590,4 +602,3 @@ mod tests { assert_eq!(res2.extensions().get::(), Some(&Ex2("hiccup"))); } } -