Skip to content

Commit

Permalink
Add convenience Uri getters (#287)
Browse files Browse the repository at this point in the history
Parts of the `Uri` that now have specific types meant that several
methods were deprecated, such as `Uri::scheme()` for
`Uri::scheme_part()`. This means that many use-cases have extra
`map`ping code to get the original value.

This adds convenience getters to reduce the need to map when the actual
type is not needed:

- `scheme_str`
- `port_u16`
  • Loading branch information
seanmonstar committed Jan 8, 2019
1 parent 5018e48 commit 4644dae
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
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

0 comments on commit 4644dae

Please sign in to comment.