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

Add and use a Port struct for Uri::port() #252

Merged
merged 2 commits into from Sep 20, 2018
Merged

Add and use a Port struct for Uri::port() #252

merged 2 commits into from Sep 20, 2018

Conversation

joshleeb
Copy link
Contributor

Add the uri::Port struct an use it with Authority::port_path, and Uri::port_path methods, which deprecate Authority::port and Uri::port methods.

Closes #173 - Consider a Port type for Uri::port()

Copy link
Collaborator

@carllerche carllerche left a comment

Choose a reason for hiding this comment

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

Looks good. Left thoughts inline

/// Get the port of this `Authority`.
/// Get the port of this `Authority`. Deprecated: see `port_path` instead.
#[deprecated(since="0.2.0", note="please use `port_path` instead")]
pub fn port(&self) -> Option<u16> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could this be doc hidden as well?

@@ -196,28 +203,28 @@ impl Authority {
///
/// # Examples
///
/// Authority with port
/// Authority with port path
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would keep the docs the same. No need to add “part”.

s.rfind(":").and_then(|i| {
u16::from_str(&s[i+1..]).ok()
})
pub fn port_path(&self) -> Option<Port> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We have been using a _part suffix for this situation. We probably want to stick with that pattern.

src/uri/mod.rs Outdated
/// Get the port of this `Uri`.
/// Get the port of this `Uri`. Deprecated: see `port_path` instead.
#[deprecated(since="0.2.0", note="please use `port_path` instead")]
pub fn port(&self) -> Option<u16> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same re doc hidden.

src/uri/port.rs Outdated
/// assert!(Port::from_str("80").is_ok());
/// assert!(Port::from_str("abcd").is_err());
/// ```
pub fn from_str(bytes: &'a str) -> Result<Self, num::ParseIntError> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would not expose this fn yet in favor of not letting the type be publicly constructed yet.

@joshleeb
Copy link
Contributor Author

@carllerche updated :)

Copy link
Collaborator

@carllerche carllerche left a comment

Choose a reason for hiding this comment

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

Thanks. It looks like there are a bunch of _pathreferences left. I pointed out a few, but there are some more.

This is probably causing CI to fail too.

@@ -180,12 +180,19 @@ impl Authority {
host(self.as_str())
}

/// Get the port of this `Authority`.
#[deprecated(since="0.2.0", note="please use `port_path` instead")]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Stray _path reference.

/// colon (":") character. A value is only returned if one is specified in
/// the URI string, i.e., default port values are **not** returned.

/// The port path subcomponent of authority is designated by an optional port
Copy link
Collaborator

Choose a reason for hiding this comment

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

Stray "path" reference.

/// let authority: Authority = "example.org:80".parse().unwrap();
///
/// assert_eq!(authority.port(), Some(80));
/// assert_eq!(authority.port_path(), Port::from_str("80").ok());
Copy link
Collaborator

Choose a reason for hiding this comment

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

_path reference

/// let authority: Authority = "example.org".parse().unwrap();
///
/// assert!(authority.port().is_none());
/// assert!(authority.port_path().is_none());
Copy link
Collaborator

Choose a reason for hiding this comment

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

_path reference.

@joshleeb
Copy link
Contributor Author

joshleeb commented Sep 17, 2018

@carllerche should be fixed up properly now

///
/// The supplied `str` must be a valid u16.
pub(crate) fn from_str(bytes: &'a str) -> Result<Self, num::ParseIntError> {
u16::from_str(bytes).and_then(|port| Ok(Port { port, bytes }))

Choose a reason for hiding this comment

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

The BNF of a URI port is *DIGIT, see https://tools.ietf.org/html/rfc3986#section-3.2.3. However from_str allows an optional + sign, see https://doc.rust-lang.org/nightly/std/primitive.u16.html#method.from_str_radix.

Copy link
Collaborator

Choose a reason for hiding this comment

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

In theory, this should already have been validated when constructing the Uri, so checking again here is not necessary.

Copy link
Collaborator

@carllerche carllerche left a comment

Choose a reason for hiding this comment

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

Looks good to me. I added some nits inline, but they are not required for the PR to land. @seanmonstar any follow up thoughts?

/// number in decimal following the host and delimited from it by a single
/// colon (":") character. A value is only returned if one is specified in
/// the URI string, i.e., default port values are **not** returned.
/// number as bytes following the host and delimited from it by a single
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: I would remove as bytes here as that is an implementation detail.

/// the URI string, i.e., default port values are **not** returned.
/// number as bytes following the host and delimited from it by a single
/// colon (":") character. It can be turned into a decimal port number with
/// the `as_u16` method.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe also add "or as a str with the as_str method.

src/uri/mod.rs Outdated
/// number in decimal following the host and delimited from it by a single
/// colon (":") character. A value is only returned if one is specified in
/// the URI string, i.e., default port values are **not** returned.
/// number as bytes following the host and delimited from it by a single
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same doc nits as above.

}
}

impl<'a> PartialEq for Port<'a> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not required for this PR, but it would be nice to add PartialEq implementations for numeric types (u16, u32, ...).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll put up a subsequent PR adding a generic implementation for PartialEq 😄

@seanmonstar
Copy link
Member

Looks good to me when nits are fixed. Thanks!

@carllerche carllerche merged commit 0072b9d into hyperium:master Sep 20, 2018
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

4 participants