-
Notifications
You must be signed in to change notification settings - Fork 8
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
Consider allowing Cidr construction from addresses where host part is not zero #8
Comments
Hi. You should be able to construct an To move from an (All that works with IPv6 too of course.) Hope that helps :) cheers, |
Would you consider relaxing this check and instead having the type fixup the host part to be zero, or otherwise providing an alternative constructor to allow this more relaxed construction? Alternatively, could the error message be made more actionable? For example, it already knows that for |
Yes, agreed. Perhaps a |
And this is really the way it should be, and I think many other implementations agree with me on this. (I just checked python As stated above the proper way to ignore the host part is to use I think this should only be used if you actually want to accept an "interface configuration address", and want to extract the network from it, but if you really think your program should just do it's best to guess what the user meant, this is the way to go. (I'm also not going to add unsafe I also don't see a good reason to add a I am open to improve the documentation though; I'd appreciate feedback in which places it would be useful to show how to ignore the host part. |
For clarity, in my use case, users provide CIDRs like When that fails, I'd like the error message that is displayed to them to be instructive. I understand that you don't want to change your interface, I'm just looking for a low effort way to provide maximum clarity; I'd rather not have to parse the CIDR myself and replicate the internal logic of the cidr library. Would you consider expanding the
and having the
|
Hm. I admit I don't have a good solution for Extending the |
* Separate enum for Cidr parsing * Add inet address to InvalidHostPart variant to allow easier recovery from invalid host part errors in case you want to ignore them.
@stbuehler db749ad looks perfect for my use case! Would you consider rolling that out as a release? In the meantime, I'm pointing my build to that branch of this repo |
This is done in two parts: 1. Showing the list of bad cidrs and their corresponding error messages 2. Upgrading to a yet-to-be-released branch of the underlying cidr library that can provide a helpful suggestion of how to fix it refs: stbuehler/rust-cidr#8
I guess I still don't fully understand the rationale here. If the host part is always 0, that means it's irrelevant information. In which case, a CIDR could just be specified by a bit count. Further it's mostly backward compatible if the host part of the CIDR is specified to always be zero as relaxing that check shouldn't break anything except code that explicitly uses the error enum for it, right? |
@wez:
I've decided against extending the error. The other errors (e.g. I'll expose some parser methods (including some additional ones), so you could choose to either just ignore host-bits or have your parser check the host-bits explicitly with a pretty message by using the (As a sidenote: I will probably drop accepting "127" as short for "127.0.0.0/32" and similar in 0.3.0; the |
A CIDR is "mathematically" a bitstring; this bitstring is a prefix of all addresses that are part of that CIDR. Which is why it is often called a "prefix". To properly ("canonically") represent it we append 0s ("host part") to make it a full address and print that together with the length of the bitstring. And I decided (similar as other projects did) to only accept canonically formatted representations, because other representations very likely indicate a mistake in the input. If you think this isn't the case in your application, feel free to use any of the mentioned alternatives. (This also means the CIDR is more than just the bit count.) |
The upstream cidr crate has a released a version that exposes the more relaxed parser, so refactor the prior work from 7eb5bdc (which was pointing to a temporary development branch) to reference its "final form". This is done by exposing a `parse_cidr` function from our cidr-map crate, and standardizing all callers to route through that. refs: stbuehler/rust-cidr#8
@stbuehler Thanks, that gives a lot of choice and flexibility and works for me! |
I'm getting an address from a list of interfaces and then need to construct a CIDR from one of those host addresses and a specific network length.
However, this library seems to always want to return
host part of address was not zero
, and sincebitstring
is private I can'tclip(24)
on anIpv4Inet
object to force the address down to 24 bits, for example.Is there a way to pass an address to the Cidr constructor to ignore the host bits?
The text was updated successfully, but these errors were encountered: