Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Allow parsing of netmask #45

Open
etiedem opened this issue Nov 27, 2022 · 2 comments
Open

Enhancement: Allow parsing of netmask #45

etiedem opened this issue Nov 27, 2022 · 2 comments

Comments

@etiedem
Copy link

etiedem commented Nov 27, 2022

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
Copy link
Owner

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
Copy link
Author

etiedem commented Dec 12, 2022

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants