use crate::config;
use crate::print;
use std::net::IpAddr;
use std::u8;
pub fn ip2id(
ip: IpAddr
) -> u8
{
let ip_str = ip.to_string();
let mut ip_int = config::ERROR_ID;
let id_str = ip_str.split('.') .nth(3) .and_then(|s| s.split(':') .next()) .and_then(|s| s.parse::<u8>().ok());
match id_str
{
Some(value) =>
{
ip_int = value;
}
None =>
{
print::err(format!("Failed to extract ID from IP"));
}
}
ip_int
}
pub fn get_root_ip(
ip: IpAddr
) -> String
{
match ip
{
IpAddr::V4(addr) =>
{
let octets = addr.octets();
format!("{}.{}.{}", octets[0], octets[1], octets[2])
}
IpAddr::V6(addr) =>
{
let segments = addr.segments();
let root_segments = &segments[..segments.len() - 1]; root_segments.iter().map(|s| s.to_string()).collect::<Vec<_>>().join(":")
}
}
}