Skip to content

Commit c809542

Browse files
LucioFrancoseanmonstar
authored andcommitted
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.
1 parent be5ec45 commit c809542

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/client/connect/mod.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ pub(super) enum Alpn {
6161
}
6262

6363
impl Destination {
64+
/// Try to convert a `Uri` into a `Destination`
65+
///
66+
/// # Error
67+
///
68+
/// Returns an error if the uri contains no authority or
69+
/// no scheme.
70+
pub fn try_from_uri(uri: Uri) -> ::Result<Self> {
71+
uri.authority_part().ok_or(::error::Parse::Uri)?;
72+
uri.scheme_part().ok_or(::error::Parse::Uri)?;
73+
Ok(Destination { uri })
74+
}
75+
6476
/// Get the protocol scheme.
6577
#[inline]
6678
pub fn scheme(&self) -> &str {
@@ -590,4 +602,3 @@ mod tests {
590602
assert_eq!(res2.extensions().get::<Ex2>(), Some(&Ex2("hiccup")));
591603
}
592604
}
593-

0 commit comments

Comments
 (0)