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:
committed by
Sean McArthur
parent
be5ec45571
commit
c809542c83
@@ -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 {
|
||||
@@ -590,4 +602,3 @@ mod tests {
|
||||
assert_eq!(res2.extensions().get::<Ex2>(), Some(&Ex2("hiccup")));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user