uri_to_host_port 0.1.4

A lightweight, robust Rust utility to parse a URI and extract its host and port / 一个轻量、健壮的 Rust 工具,用于解析 URI 并提取其主机和端口
Documentation
use hyper::Uri;

mod error;
pub use crate::error::Error;

pub type Result<T> = std::result::Result<T, Error>;

pub fn parse(uri: impl TryInto<Uri> + std::fmt::Debug + Clone) -> Result<(String, u16)> {
  let uri_object = uri
    .clone()
    .try_into()
    .map_err(|_| Error::InvalidUri(format!("{uri:?}")))?;
  let host = uri_object
    .host()
    .ok_or_else(|| Error::MissingHost(format!("{uri:?}")))?;
  let port = uri_object
    .port_u16()
    .unwrap_or_else(|| match uri_object.scheme_str() {
      Some("https") => 443,
      _ => 80,
    });
  Ok((host.to_string(), port))
}