krisprice / ipnet

IpNet, Ipv4Net, and Ipv6Net types and methods for Rust
Apache License 2.0
122 stars 26 forks source link

Enhancement: Allow parsing of netmask #45

Open etiedem opened 1 year ago

etiedem commented 1 year ago

Allow: let net: Ipv4Net = "10.0.0.0/255.255.255.252".parse().unwrap();

This would allow additional flexibility since unfortunately not everything represents networks using bit notation.

krisprice commented 1 year ago

Hi @etiedem, coincidentally I just merged #46 which added support for netmasks, but not a parser. The parser does needs a bit of work anyway due to issue #32 but I'm not sure about the value in adding this. In the mean time you could maybe write some code to split these and parse them first into two IpAddrs, then pass them to the new .with_netmask() constructors?

etiedem commented 1 year ago

Thanks for the answer. I did make a little utility function to convert the mask to bitlength and just used the normal parser.

fn netmask_to_bit(netmask: &str) -> u32 {
    let bits: u32 = netmask.split(".")
                .map(|x| x.parse::<u8>().unwrap().count_ones())
                .sum();
    bits
}