reqwest_wasm/
into_url.rs

1use url::Url;
2
3/// A trait to try to convert some type into a `Url`.
4///
5/// This trait is "sealed", such that only types within reqwest can
6/// implement it.
7pub trait IntoUrl: IntoUrlSealed {}
8
9impl IntoUrl for Url {}
10impl IntoUrl for String {}
11impl<'a> IntoUrl for &'a str {}
12impl<'a> IntoUrl for &'a String {}
13
14pub trait IntoUrlSealed {
15    // Besides parsing as a valid `Url`, the `Url` must be a valid
16    // `http::Uri`, in that it makes sense to use in a network request.
17    fn into_url(self) -> crate::Result<Url>;
18
19    fn as_str(&self) -> &str;
20}
21
22impl IntoUrlSealed for Url {
23    fn into_url(self) -> crate::Result<Url> {
24        if self.has_host() {
25            Ok(self)
26        } else {
27            Err(crate::error::url_bad_scheme(self))
28        }
29    }
30
31    fn as_str(&self) -> &str {
32        self.as_ref()
33    }
34}
35
36impl<'a> IntoUrlSealed for &'a str {
37    fn into_url(self) -> crate::Result<Url> {
38        Url::parse(self).map_err(crate::error::builder)?.into_url()
39    }
40
41    fn as_str(&self) -> &str {
42        self
43    }
44}
45
46impl<'a> IntoUrlSealed for &'a String {
47    fn into_url(self) -> crate::Result<Url> {
48        (&**self).into_url()
49    }
50
51    fn as_str(&self) -> &str {
52        self.as_ref()
53    }
54}
55
56impl<'a> IntoUrlSealed for String {
57    fn into_url(self) -> crate::Result<Url> {
58        (&*self).into_url()
59    }
60
61    fn as_str(&self) -> &str {
62        self.as_ref()
63    }
64}
65
66if_hyper! {
67    pub(crate) fn expect_uri(url: &Url) -> http::Uri {
68        url.as_str()
69            .parse()
70            .expect("a parsed Url should always be a valid Uri")
71    }
72
73    pub(crate) fn try_uri(url: &Url) -> Option<http::Uri> {
74        url.as_str().parse().ok()
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn into_url_file_scheme() {
84        let err = "file:///etc/hosts".into_url().unwrap_err();
85        assert_eq!(
86            err.to_string(),
87            "builder error for url (file:///etc/hosts): URL scheme is not allowed"
88        );
89    }
90}