Skip to main content

uri_to_host_port/
lib.rs

1use hyper::Uri;
2
3mod error;
4pub use crate::error::Error;
5
6pub type Result<T> = std::result::Result<T, Error>;
7
8pub fn parse(uri: impl TryInto<Uri> + std::fmt::Debug + Clone) -> Result<(String, u16)> {
9  let uri_object = uri
10    .clone()
11    .try_into()
12    .map_err(|_| Error::InvalidUri(format!("{uri:?}")))?;
13  let host = uri_object
14    .host()
15    .ok_or_else(|| Error::MissingHost(format!("{uri:?}")))?;
16  let port = uri_object
17    .port_u16()
18    .unwrap_or_else(|| match uri_object.scheme_str() {
19      Some("https") => 443,
20      _ => 80,
21    });
22  Ok((host.to_string(), port))
23}