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
Conversation
There was a problem hiding this 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
src/uri/authority.rs
Outdated
/// 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> { |
There was a problem hiding this comment.
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?
src/uri/authority.rs
Outdated
@@ -196,28 +203,28 @@ impl Authority { | |||
/// | |||
/// # Examples | |||
/// | |||
/// Authority with port | |||
/// Authority with port path |
There was a problem hiding this comment.
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”.
src/uri/authority.rs
Outdated
s.rfind(":").and_then(|i| { | ||
u16::from_str(&s[i+1..]).ok() | ||
}) | ||
pub fn port_path(&self) -> Option<Port> { |
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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.
@carllerche updated :) |
There was a problem hiding this 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 _path
references left. I pointed out a few, but there are some more.
This is probably causing CI to fail too.
src/uri/authority.rs
Outdated
@@ -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")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stray _path
reference.
src/uri/authority.rs
Outdated
/// 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stray "path" reference.
src/uri/authority.rs
Outdated
/// let authority: Authority = "example.org:80".parse().unwrap(); | ||
/// | ||
/// assert_eq!(authority.port(), Some(80)); | ||
/// assert_eq!(authority.port_path(), Port::from_str("80").ok()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_path reference
src/uri/authority.rs
Outdated
/// let authority: Authority = "example.org".parse().unwrap(); | ||
/// | ||
/// assert!(authority.port().is_none()); | ||
/// assert!(authority.port_path().is_none()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_path reference.
@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 })) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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?
src/uri/authority.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 |
There was a problem hiding this comment.
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.
src/uri/authority.rs
Outdated
/// 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. |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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
, ...).
There was a problem hiding this comment.
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
😄
Looks good to me when nits are fixed. Thanks! |
Add the
uri::Port
struct an use it withAuthority::port_path
, andUri::port_path
methods, which deprecateAuthority::port
andUri::port
methods.Closes #173 - Consider a Port type for
Uri::port()