Skip to content

Commit

Permalink
feat(client): add Destination::try_from_uri constructor
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LucioFranco authored and seanmonstar committed Jan 15, 2019
1 parent be5ec45 commit c809542
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/client/connect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
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 {
Expand Down Expand Up @@ -590,4 +602,3 @@ mod tests {
assert_eq!(res2.extensions().get::<Ex2>(), Some(&Ex2("hiccup")));
}
}

0 comments on commit c809542

Please sign in to comment.