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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::{fmt, num}; | ||
use std::str::FromStr; | ||
|
||
/// The port component of a URI. | ||
#[derive(Debug)] | ||
pub struct Port<'a> { | ||
bytes: &'a str, | ||
port: u16, | ||
} | ||
|
||
impl<'a> Port<'a> { | ||
/// Converts a `str` to a port number. | ||
/// | ||
/// 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 })) | ||
} | ||
|
||
/// Returns the port number as a `u16`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// Port as `u16` | ||
/// | ||
/// ``` | ||
/// # use http::uri::Authority; | ||
/// let authority: Authority = "example.org:80".parse().unwrap(); | ||
/// | ||
/// let port = authority.port_part().unwrap(); | ||
/// assert_eq!(port.as_u16(), 80); | ||
/// ``` | ||
pub fn as_u16(&self) -> u16 { | ||
self.port | ||
} | ||
|
||
/// Returns the port number as a `str`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// Port as `str`. | ||
/// | ||
/// ``` | ||
/// # use http::uri::Authority; | ||
/// let authority: Authority = "example.org:80".parse().unwrap(); | ||
/// | ||
/// let port = authority.port_part().unwrap(); | ||
/// assert_eq!(port.as_str(), "80"); | ||
/// ``` | ||
pub fn as_str(&self) -> &'a str { | ||
self.bytes | ||
} | ||
} | ||
|
||
impl<'a> PartialEq for Port<'a> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not required for this PR, but it would be nice to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll put up a subsequent PR adding a generic implementation for |
||
fn eq(&self, other: &Self) -> bool { | ||
self.port == other.port | ||
} | ||
} | ||
|
||
impl<'a> fmt::Display for Port<'a> { | ||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
write!(fmt, "{}", self.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. Howeverfrom_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.