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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::{
    error::Error,
    fmt::{self, Display},
    str::FromStr,
};

/// Supported URI schemes for parsing
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Scheme {
    /// Represents `file://` url scheme
    File,
    /// Represents `ftp://` url scheme
    Ftp,
    /// Represents `ftps://` url scheme
    Ftps,
    /// Represents `git://` url scheme
    Git,
    /// Represents `git+ssh://` url scheme
    GitSsh,
    /// Represents `http://` url scheme
    Http,
    /// Represents `https://` url scheme
    Https,
    /// Represents `ssh://` url scheme
    Ssh,
    /// Represents No url scheme
    Unspecified,
}

impl Display for Scheme {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Scheme::File => write!(f, "file"),
            Scheme::Ftp => write!(f, "ftp"),
            Scheme::Ftps => write!(f, "ftps"),
            Scheme::Git => write!(f, "git"),
            Scheme::GitSsh => write!(f, "git+ssh"),
            Scheme::Http => write!(f, "http"),
            Scheme::Https => write!(f, "https"),
            Scheme::Ssh => write!(f, "ssh"),
            Scheme::Unspecified => write!(f, "unspecified"),
        }
    }
}

#[derive(Debug)]
#[non_exhaustive]
pub struct FromStrError {
    kind: FromStrErrorKind,
}

impl Display for FromStrError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.kind {
            FromStrErrorKind::UnsupportedScheme(scheme) => {
                write!(f, "unsupported scheme `{}`", scheme)
            }
        }
    }
}

impl Error for FromStrError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match &self.kind {
            FromStrErrorKind::UnsupportedScheme(_) => None,
        }
    }
}

#[derive(Debug)]
pub enum FromStrErrorKind {
    #[non_exhaustive]
    UnsupportedScheme(String),
}

impl FromStr for Scheme {
    type Err = FromStrError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "file" => Ok(Scheme::File),
            "ftp" => Ok(Scheme::Ftp),
            "ftps" => Ok(Scheme::Ftps),
            "git" => Ok(Scheme::Git),
            "git+ssh" => Ok(Scheme::GitSsh),
            "http" => Ok(Scheme::Http),
            "https" => Ok(Scheme::Https),
            "ssh" => Ok(Scheme::Ssh),
            "unspecified" => Ok(Scheme::Unspecified),
            _ => Err(FromStrError {
                kind: FromStrErrorKind::UnsupportedScheme(s.to_owned()),
            }),
        }
    }
}