blob: 81ff9254b19e8884abb051692da0f3c1ba2a7251 (
plain)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 | use std::path::PathBuf;
use std::str::FromStr;
use std::string::ParseError;
use url::{self,Url};
use serde_derive::Serialize;
#[derive(Debug,PartialEq,Serialize)]
#[serde(untagged)]
pub enum Target {
    #[serde(serialize_with = "serialize_url")]
    Url(Url),
    Path(PathBuf),
}
impl From<Url> for Target {
    fn from(url: Url) -> Self {
        Target::Url(url)
    }
}
impl From<PathBuf> for Target {
    fn from(path: PathBuf) -> Self {
        Target::Path(path)
    }
}
impl FromStr for Target {
    type Err = ParseError;
	fn from_str(input: &str) -> Result<Self, Self::Err> {
        Ok(match Url::parse(input) {
            Ok(url) => url.into(),
            Err(_) => PathBuf::from(input.trim()).into(),
        })
    }
}
pub fn serialize_url<S>(url: &Url, serializer: S) -> Result<S::Ok, S::Error> where S: serde::ser::Serializer {
	serializer.serialize_str(url.as_str())
}
 |