Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add and use a Port struct for Uri::port() (#252)
Fixes #173
- Loading branch information
1 parent
3078c3c
commit 0072b9d
Showing
4 changed files
with
126 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
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) | ||
} | ||
} |
Oops, something went wrong.