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 convenience Uri getters #287

Merged
merged 1 commit into from Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/uri/authority.rs
Expand Up @@ -197,10 +197,10 @@ impl Authority {
host(self.as_str())
}

#[deprecated(since="0.1.14", note="use `port_part` instead")]
#[deprecated(since="0.1.14", note="use `port_part` or `port_u16` instead")]
#[doc(hidden)]
pub fn port(&self) -> Option<u16> {
self.port_part().and_then(|p| Some(p.as_u16()))
self.port_u16()
}

/// Get the port part of this `Authority`.
Expand All @@ -227,6 +227,7 @@ impl Authority {
///
/// let port = authority.port_part().unwrap();
/// assert_eq!(port.as_u16(), 80);
/// assert_eq!(port.as_str(), "80");
/// ```
///
/// Authority without port
Expand All @@ -244,6 +245,20 @@ impl Authority {
.and_then(|i| Port::from_str(&bytes[i + 1..]).ok())
}

/// Get the port of this `Authority` as a `u16`.
///
/// # Example
///
/// ```
/// # use http::uri::Authority;
/// let authority: Authority = "example.org:80".parse().unwrap();
///
/// assert_eq!(authority.port_u16(), Some(80));
/// ```
pub fn port_u16(&self) -> Option<u16> {
self.port_part().and_then(|p| Some(p.as_u16()))
}

/// Return a str representation of the authority
#[inline]
pub fn as_str(&self) -> &str {
Expand Down
43 changes: 37 additions & 6 deletions src/uri/mod.rs
Expand Up @@ -437,10 +437,11 @@ impl Uri {
/// Absolute URI
///
/// ```
/// # use http::Uri;
/// use http::uri::{Scheme, Uri};
///
/// let uri: Uri = "http://example.org/hello/world".parse().unwrap();
///
/// assert_eq!(uri.scheme_part().map(|s| s.as_str()), Some("http"));
/// assert_eq!(uri.scheme_part(), Some(&Scheme::HTTP));
/// ```
///
///
Expand All @@ -461,10 +462,25 @@ impl Uri {
}
}

#[deprecated(since = "0.1.2", note = "use scheme_part instead")]
#[deprecated(since = "0.1.2", note = "use scheme_part or scheme_str instead")]
#[doc(hidden)]
#[inline]
pub fn scheme(&self) -> Option<&str> {
self.scheme_str()
}

/// Get the scheme of this `Uri` as a `&str`.
///
/// # Example
///
/// ```
/// # use http::Uri;
/// let uri: Uri = "http://example.org/hello/world".parse().unwrap();
///
/// assert_eq!(uri.scheme_str(), Some("http"));
/// ```
#[inline]
pub fn scheme_str(&self) -> Option<&str> {
if self.scheme.inner.is_none() {
None
} else {
Expand Down Expand Up @@ -569,10 +585,10 @@ impl Uri {
self.authority_part().map(|a| a.host())
}

#[deprecated(since="0.1.14", note="use `port_part` instead")]
#[deprecated(since="0.1.14", note="use `port_part` or `port_u16` instead")]
#[doc(hidden)]
pub fn port(&self) -> Option<u16> {
self.port_part().and_then(|p| Some(p.as_u16()))
self.port_u16()
}

/// Get the port part of this `Uri`.
Expand All @@ -594,7 +610,7 @@ impl Uri {
/// Absolute URI with port
///
/// ```
/// # use http::{Uri, uri::Port};
/// # use http::Uri;
/// let uri: Uri = "http://example.org:80/hello/world".parse().unwrap();
///
/// let port = uri.port_part().unwrap();
Expand Down Expand Up @@ -623,6 +639,21 @@ impl Uri {
.and_then(|a| a.port_part())
}

/// Get the port of this `Uri` as a `u16`.
///
///
/// # Example
///
/// ```
/// # use http::{Uri, uri::Port};
/// let uri: Uri = "http://example.org:80/hello/world".parse().unwrap();
///
/// assert_eq!(uri.port_u16(), Some(80));
/// ```
pub fn port_u16(&self) -> Option<u16> {
self.port_part().and_then(|p| Some(p.as_u16()))
}

/// Get the query string of this `Uri`, starting after the `?`.
///
/// The query component contains non-hierarchical data that, along with data
Expand Down