use std::collections::HashSet;
use crate::error::{Error, Result};
const PORT_RANGE_START: u16 = 10000;
const PORT_RANGE_END: u16 = 11000;
pub fn allocate_port_excluding(
extra_used: &HashSet<u16>,
port_in_use: &dyn Fn(u16) -> bool,
) -> Result<u16> {
let mut used: HashSet<u16> = crate::list_installed()
.unwrap_or_default()
.into_iter()
.flat_map(|s| s.ports.into_values())
.collect();
used.extend(extra_used.iter().copied());
(PORT_RANGE_START..PORT_RANGE_END)
.find(|p| !used.contains(p) && !port_in_use(*p))
.ok_or(Error::PortsExhausted {
start: PORT_RANGE_START,
end: PORT_RANGE_END,
})
}