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

improve docs #15

Closed
wants to merge 2 commits into from
Closed

improve docs #15

wants to merge 2 commits into from

Conversation

robjtede
Copy link
Contributor

@robjtede robjtede commented Mar 2, 2023

Improves docs on a large portion of methods on IpCidr, Ipv4Cidr, and Ipv6Cidr. Tries to adhere to standard rustdoc grammar and formatting guidelines including using imperative mood for first line of function docs.

Also includes some trivial code quality improvements. No changes to public API have been made.

@robjtede robjtede marked this pull request as draft March 2, 2023 01:01
@robjtede robjtede marked this pull request as ready for review March 30, 2023 02:48
README.md Outdated
assert_eq!(true, cidr.contains(IpAddr::from_str("192.168.51.103").unwrap()));
assert_eq!(false, cidr.contains(IpAddr::from_str("192.168.50.103").unwrap()));
assert_eq!(true, cidr.contains(IpAddr::from([192, 168, 51, 103])));
assert_eq!(false, cidr.contains(IpAddr::from([192, 168, 50, 103])));
```
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use from_str?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

examples should be clean and concise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed this particular change from this PR

README.md Outdated
@@ -160,7 +160,7 @@ Enable the `serde` feature to support the serde framework.

```toml
[dependencies.cidr-utils]
version = "*"
Copy link
Owner

@magiclen magiclen Apr 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer using *. Dot-dot-dot is not in correct format.

Copy link
Contributor Author

@robjtede robjtede Apr 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encouraging use of * is dangerous. Using the "wrong" format requires users to select the version properly.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes I just want to copy-paste and run, and see what happens. I don't want to make an extra effort to correct the format.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My experience with helping Rust newcomers is that this is a real footgun. Regardless, I've dropped this commit.

@@ -17,6 +17,7 @@ pub enum IpCidr {

impl IpCidr {
#[allow(clippy::should_implement_trait)]
#[doc(hidden)] // TODO: remove and rely on std FromStr
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function necessary to be hidden?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should encourage use of the FromStr trait. If a breaking change were acceptable, I'd say to make this fn private. Hiding it is an alternative that is backwards compatible.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is more convenient that a type has from_str itself. We don't need to add the use std::str::FromStr statement every time.

Copy link
Contributor Author

@robjtede robjtede Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using str::parse() is the recommended method, not FromStr, which is why it's not in the std prelude.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed this particular change from this PR

@@ -62,6 +62,8 @@ impl DoubleEndedIterator for IpCidrIpAddrIterator {
}

impl IpCidr {
#[doc(hidden)]
#[deprecated(note = "Prefer `.iter()`.")]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why this function is named iter_as ip_addr is to be consistent with Ipv4Cidr::iter_as_ipv4_addr and Ipv6Cidr::iter_as_ipv6_addr.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough, reverted

@@ -28,8 +28,9 @@ pub struct Ipv4Cidr {
}

impl Ipv4Cidr {
/// Returns an integer which represents the prefix an IPv4 byte array of this CIDR in big-endian
/// (BE) order.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer not to wrap comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't want them wrapped at all, even for extremely long lines?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the comment line is extremely long, I will wrap it by myself instead of tools,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

up to you, I unwrapped these long lines

}

#[inline]
pub fn get_bits(&self) -> u8 {
mask_to_bits(self.mask).unwrap()
}

/// Returns an integer which represents the mask an IPv4 byte array of this CIDR in big-endian
/// (BE) order.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer not to wrap comments.

@@ -111,6 +109,7 @@ impl Ipv4Cidr {
}

#[allow(clippy::should_implement_trait)]
#[doc(hidden)] // TODO: remove and rely on std FromStr
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function necessary to be hidden?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed this particular change from this PR

@@ -229,7 +243,7 @@ impl Ipv4Cidr {

impl Debug for Ipv4Cidr {
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need to add <'_> explicitly?

Copy link
Contributor Author

@robjtede robjtede Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's part of the rust_2018_idioms lint, omitting this anon lifetime will be a hard error in the future.

@@ -123,6 +119,7 @@ impl Ipv6Cidr {
}

#[allow(clippy::should_implement_trait)]
#[doc(hidden)] // TODO: remove and rely on std FromStr
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function necessary to be hidden?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed this particular change from this PR

src/lib.rs Outdated
@@ -49,8 +49,8 @@ use cidr_utils::cidr::IpCidr;

let cidr = IpCidr::from_str("192.168.51.0/24").unwrap();

assert_eq!(true, cidr.contains(IpAddr::from_str("192.168.51.103").unwrap()));
assert_eq!(false, cidr.contains(IpAddr::from_str("192.168.50.103").unwrap()));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use from_str?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed this particular change from this PR

src/lib.rs Outdated
@@ -158,12 +158,16 @@ Enable the `serde` feature to support the serde framework.

```toml
[dependencies.cidr-utils]
version = "*"
version = "..."
Copy link
Owner

@magiclen magiclen Apr 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer using *. Dot-dot-dot is not in correct format.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed this particular change from this PR

#![deny(rust_2018_idioms, nonstandard_style)]
#![warn(future_incompatible)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need to add these?

Copy link
Contributor Author

@robjtede robjtede Apr 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They provide good defaults for contributors and encourage better code style.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why fn fmt(&self, f: &mut Formatter<'_>) is better than fn fmt(&self, f: &mut Formatter)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because hidden lifetimes are harmful to comprehension. These lints are likely to become errors in a future edition.

@robjtede robjtede force-pushed the improve-docs branch 3 times, most recently from 95a3389 to f7adf2d Compare September 12, 2023 05:16
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

Successfully merging this pull request may close these issues.

None yet

2 participants