Skip to main content

leyline/core/
into_url.rs

1use url::Url;
2
3use crate::core::error::{Error, Kind, Result};
4
5mod sealed {
6    pub trait Sealed {}
7}
8
9pub trait IntoUrl: sealed::Sealed {
10    fn into_url(self) -> Result<Url>;
11}
12
13impl sealed::Sealed for &str {}
14impl IntoUrl for &str {
15    fn into_url(self) -> Result<Url> {
16        if self.chars().any(|c| c.is_ascii_control()) {
17            return Err(Error::new(Kind::Url).with_message("URL contains a control character"));
18        }
19        Url::parse(self).map_err(Error::from_url_parse)
20    }
21}
22
23impl sealed::Sealed for String {}
24impl IntoUrl for String {
25    fn into_url(self) -> Result<Url> {
26        self.as_str().into_url()
27    }
28}
29
30impl sealed::Sealed for &String {}
31impl IntoUrl for &String {
32    fn into_url(self) -> Result<Url> {
33        self.as_str().into_url()
34    }
35}
36
37impl sealed::Sealed for Url {}
38impl IntoUrl for Url {
39    fn into_url(self) -> Result<Url> {
40        Ok(self)
41    }
42}
43
44impl sealed::Sealed for &Url {}
45impl IntoUrl for &Url {
46    fn into_url(self) -> Result<Url> {
47        Ok(self.clone())
48    }
49}