From bd44ab804fc135f0d84c570a19cbe3391d291050 Mon Sep 17 00:00:00 2001 From: Jed Denlea Date: Wed, 16 Mar 2022 17:43:27 -0700 Subject: [PATCH 1/2] From impls of PathAndQuery and Authority for Uri A Uri may logically hold only an Authority or only a PathAndQuery. Should an application want to make such Uris, it would be easier to safely create them by first making just an Authority or just a PathAndQuery, then cheaply convert them directly into a Uri. --- src/uri/mod.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/uri/mod.rs b/src/uri/mod.rs index b79cb704..3881f82e 100644 --- a/src/uri/mod.rs +++ b/src/uri/mod.rs @@ -736,6 +736,28 @@ impl<'a> TryFrom<&'a Uri> for Uri { } } +/// Convert an `Authority` into a `Uri`. +impl From for Uri { + fn from(authority: Authority) -> Self { + Self { + scheme: Scheme::empty(), + authority, + path_and_query: PathAndQuery::empty(), + } + } +} + +/// Convert a `PathAndQuery` into a `Uri`. +impl From for Uri { + fn from(path_and_query: PathAndQuery) -> Self { + Self { + scheme: Scheme::empty(), + authority: Authority::empty(), + path_and_query, + } + } +} + /// Convert a `Uri` from parts /// /// # Examples From 435132dbb5ef626a60c251706337fbe6a5340439 Mon Sep 17 00:00:00 2001 From: Jed Denlea Date: Wed, 16 Mar 2022 17:43:27 -0700 Subject: [PATCH 2/2] Fix Uri docs The doc comments for `impl From for Parts` actually described `Uri::from_parts`. --- src/uri/mod.rs | 70 +++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/uri/mod.rs b/src/uri/mod.rs index 3881f82e..30be83b5 100644 --- a/src/uri/mod.rs +++ b/src/uri/mod.rs @@ -201,7 +201,40 @@ impl Uri { Builder::new() } - /// Attempt to convert a `Uri` from `Parts` + /// Attempt to convert a `Parts` into a `Uri`. + /// + /// # Examples + /// + /// Relative URI + /// + /// ``` + /// # use http::uri::*; + /// let mut parts = Parts::default(); + /// parts.path_and_query = Some("/foo".parse().unwrap()); + /// + /// let uri = Uri::from_parts(parts).unwrap(); + /// + /// assert_eq!(uri.path(), "/foo"); + /// + /// assert!(uri.scheme().is_none()); + /// assert!(uri.authority().is_none()); + /// ``` + /// + /// Absolute URI + /// + /// ``` + /// # use http::uri::*; + /// let mut parts = Parts::default(); + /// parts.scheme = Some("http".parse().unwrap()); + /// parts.authority = Some("foo.com".parse().unwrap()); + /// parts.path_and_query = Some("/foo".parse().unwrap()); + /// + /// let uri = Uri::from_parts(parts).unwrap(); + /// + /// assert_eq!(uri.scheme().unwrap().as_str(), "http"); + /// assert_eq!(uri.authority().unwrap(), "foo.com"); + /// assert_eq!(uri.path(), "/foo"); + /// ``` pub fn from_parts(src: Parts) -> Result { if src.scheme.is_some() { if src.authority.is_none() { @@ -758,40 +791,7 @@ impl From for Uri { } } -/// Convert a `Uri` from parts -/// -/// # Examples -/// -/// Relative URI -/// -/// ``` -/// # use http::uri::*; -/// let mut parts = Parts::default(); -/// parts.path_and_query = Some("/foo".parse().unwrap()); -/// -/// let uri = Uri::from_parts(parts).unwrap(); -/// -/// assert_eq!(uri.path(), "/foo"); -/// -/// assert!(uri.scheme().is_none()); -/// assert!(uri.authority().is_none()); -/// ``` -/// -/// Absolute URI -/// -/// ``` -/// # use http::uri::*; -/// let mut parts = Parts::default(); -/// parts.scheme = Some("http".parse().unwrap()); -/// parts.authority = Some("foo.com".parse().unwrap()); -/// parts.path_and_query = Some("/foo".parse().unwrap()); -/// -/// let uri = Uri::from_parts(parts).unwrap(); -/// -/// assert_eq!(uri.scheme().unwrap().as_str(), "http"); -/// assert_eq!(uri.authority().unwrap(), "foo.com"); -/// assert_eq!(uri.path(), "/foo"); -/// ``` +/// Convert a `Uri` into `Parts` impl From for Parts { fn from(src: Uri) -> Self { let path_and_query = if src.has_path() {