| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
use std::fmt::{self, Debug, Formatter}; |
| 8 |
|
| 9 |
use url::{ParseError, Url}; |
| 10 |
|
| 11 |
pub trait RequestTarget: Debug { |
| 12 |
fn scheme(&self) -> &str; |
| 13 |
fn authority(&self) -> &str; |
| 14 |
fn path(&self) -> &str; |
| 15 |
} |
| 16 |
|
| 17 |
pub struct RefRequestTarget<'s, 'a, 'p> { |
| 18 |
scheme: &'s str, |
| 19 |
authority: &'a str, |
| 20 |
path: &'p str, |
| 21 |
} |
| 22 |
|
| 23 |
impl RequestTarget for RefRequestTarget<'_, '_, '_> { |
| 24 |
fn scheme(&self) -> &str { |
| 25 |
self.scheme |
| 26 |
} |
| 27 |
|
| 28 |
fn authority(&self) -> &str { |
| 29 |
self.authority |
| 30 |
} |
| 31 |
|
| 32 |
fn path(&self) -> &str { |
| 33 |
self.path |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
impl<'s, 'a, 'p> RefRequestTarget<'s, 'a, 'p> { |
| 38 |
#[must_use] |
| 39 |
pub const fn new(scheme: &'s str, authority: &'a str, path: &'p str) -> Self { |
| 40 |
Self { |
| 41 |
scheme, |
| 42 |
authority, |
| 43 |
path, |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
impl Debug for RefRequestTarget<'_, '_, '_> { |
| 49 |
fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 50 |
write!(f, "{}://{}{}", self.scheme, self.authority, self.path) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/// `AsRequestTarget` is a trait that produces a `RequestTarget` that |
| 55 |
/// refers to the identified object. |
| 56 |
pub trait AsRequestTarget<'x> { |
| 57 |
type Target: RequestTarget; |
| 58 |
type Error; |
| 59 |
/// Produce a `RequestTarget` that refers to `self`. |
| 60 |
/// |
| 61 |
/// # Errors |
| 62 |
/// |
| 63 |
/// This method can generate an error of type `Self::Error` |
| 64 |
/// if the conversion is unsuccessful. |
| 65 |
fn as_request_target(&'x self) -> Result<Self::Target, Self::Error>; |
| 66 |
} |
| 67 |
|
| 68 |
impl<'x, S, A, P> AsRequestTarget<'x> for (S, A, P) |
| 69 |
where |
| 70 |
S: AsRef<str> + 'x, |
| 71 |
A: AsRef<str> + 'x, |
| 72 |
P: AsRef<str> + 'x, |
| 73 |
{ |
| 74 |
type Target = RefRequestTarget<'x, 'x, 'x>; |
| 75 |
type Error = (); |
| 76 |
fn as_request_target(&'x self) -> Result<Self::Target, Self::Error> { |
| 77 |
Ok(RefRequestTarget::new( |
| 78 |
self.0.as_ref(), |
| 79 |
self.1.as_ref(), |
| 80 |
self.2.as_ref(), |
| 81 |
)) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
impl<'x> AsRequestTarget<'x> for Url { |
| 86 |
type Target = RefRequestTarget<'x, 'x, 'x>; |
| 87 |
type Error = (); |
| 88 |
fn as_request_target(&'x self) -> Result<Self::Target, Self::Error> { |
| 89 |
Ok(RefRequestTarget::new( |
| 90 |
self.scheme(), |
| 91 |
self.host_str().unwrap_or(""), |
| 92 |
self.path(), |
| 93 |
)) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
pub struct UrlRequestTarget { |
| 98 |
url: Url, |
| 99 |
} |
| 100 |
|
| 101 |
impl RequestTarget for UrlRequestTarget { |
| 102 |
fn scheme(&self) -> &str { |
| 103 |
self.url.scheme() |
| 104 |
} |
| 105 |
|
| 106 |
fn authority(&self) -> &str { |
| 107 |
self.url.host_str().unwrap_or("") |
| 108 |
} |
| 109 |
|
| 110 |
fn path(&self) -> &str { |
| 111 |
self.url.path() |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
impl Debug for UrlRequestTarget { |
| 116 |
fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 117 |
self.url.fmt(f) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
impl<'x> AsRequestTarget<'x> for str { |
| 122 |
type Target = UrlRequestTarget; |
| 123 |
type Error = ParseError; |
| 124 |
fn as_request_target(&'x self) -> Result<Self::Target, Self::Error> { |
| 125 |
let url = Url::parse(self)?; |
| 126 |
Ok(UrlRequestTarget { url }) |
| 127 |
} |
| 128 |
} |