use std::net::SocketAddr;
pub(crate) fn map_url(addr: SocketAddr, focus: Option<&str>, depth: u8) -> String {
let base = format!("http://{addr}/");
let Some(focus_id) = focus else {
return base;
};
if depth == 1 {
format!("{base}#/focus/{focus_id}")
} else {
format!("{base}#/focus/{focus_id}?depth={depth}")
}
}
pub(crate) fn open_browser(url: &str) -> anyhow::Result<()> {
webbrowser::open(url).map_err(Into::into)
}
#[cfg(test)]
#[expect(clippy::unwrap_used, reason = "test code")]
mod tests {
use super::*;
use std::str::FromStr;
#[test]
fn url_no_focus() {
let addr = SocketAddr::from_str("127.0.0.1:8080").unwrap();
assert_eq!(map_url(addr, None, 1), "http://127.0.0.1:8080/");
}
#[test]
fn url_focus_depth_default() {
let addr = SocketAddr::from_str("127.0.0.1:8080").unwrap();
assert_eq!(
map_url(addr, Some("SL-001"), 1),
"http://127.0.0.1:8080/#/focus/SL-001"
);
}
#[test]
fn url_focus_depth_2() {
let addr = SocketAddr::from_str("127.0.0.1:8080").unwrap();
assert_eq!(
map_url(addr, Some("SL-001"), 2),
"http://127.0.0.1:8080/#/focus/SL-001?depth=2"
);
}
#[test]
fn url_ipv6_no_focus() {
let addr = SocketAddr::from_str("[::1]:8080").unwrap();
assert_eq!(map_url(addr, None, 1), "http://[::1]:8080/");
}
}