achanda / ipnetwork

A library to work with CIDRs in rust
Apache License 2.0
121 stars 38 forks source link

IpNetwork: add is_subnet_of and is_supernet_of #129

Closed mcginty closed 3 years ago

mcginty commented 4 years ago

Very similar to the implementations for contains() that's already in the IpNetwork enum.

achanda commented 4 years ago

Thanks @mcginty I think the case where the two IP versions do not match should result in an error, to make the API less confusing. I'd add a new error type here https://github.com/achanda/ipnetwork/blob/master/src/common.rs#L5 and also add some tests. What do you think?

mcginty commented 4 years ago

In my application, the current API is simpler to read while still being technically correct.

As an alternative to the idea of having these methods return a Result<bool, IpNetworkError>, would it possibly be nicer to instead add another fn is_same_ip_version(other: IpNetwork) -> bool so people can do if net1.is_same_ip_version(net2) && net1.is_supernet_of(net2) if they care to distinguish?