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.
This commit is contained in:
Lucio Franco
2019-01-15 12:45:30 -05:00
committed by Sean McArthur
parent be5ec45571
commit c809542c83

View File

@@ -61,6 +61,18 @@ pub(super) enum Alpn {
} }
impl Destination { 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<Self> {
uri.authority_part().ok_or(::error::Parse::Uri)?;
uri.scheme_part().ok_or(::error::Parse::Uri)?;
Ok(Destination { uri })
}
/// Get the protocol scheme. /// Get the protocol scheme.
#[inline] #[inline]
pub fn scheme(&self) -> &str { pub fn scheme(&self) -> &str {
@@ -590,4 +602,3 @@ mod tests {
assert_eq!(res2.extensions().get::<Ex2>(), Some(&Ex2("hiccup"))); assert_eq!(res2.extensions().get::<Ex2>(), Some(&Ex2("hiccup")));
} }
} }