use crate::types::ErrorCode;
use crate::types::Result;
pub fn split_host_port(
input: &str,
host: &mut String,
port: &mut String,
def_port: &str,
) -> Result<()> {
port.clear();
port.push_str(def_port);
if input.starts_with('[') {
let end = input.find(']').ok_or(ErrorCode::Internal)?;
let addr = &input[1..end];
*host = addr.to_string();
let rest = &input[end + 1..];
if rest.starts_with(':') {
port.clear();
port.push_str(&rest[1..]);
} else if !rest.is_empty() {
return Err(ErrorCode::Internal);
}
return Ok(());
}
let colons = input.chars().filter(|&c| c == ':').count();
if colons > 1 {
*host = input.to_string();
return Ok(());
}
if colons == 1 {
let (h, p) = input.split_once(':').unwrap();
*host = h.to_string();
port.clear();
port.push_str(p);
return Ok(());
}
*host = input.to_string();
Ok(())
}