gix_url/
scheme.rs

1/// A scheme or protocol for use in a [`Url`][crate::Url].
2///
3/// It defines how to talk to a given repository.
4#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[allow(missing_docs)]
7pub enum Scheme {
8    /// A local resource that is accessible on the current host.
9    File,
10    /// A git daemon, like `File` over TCP/IP.
11    Git,
12    /// Launch `git-upload-pack` through an `ssh` tunnel.
13    Ssh,
14    /// Use the HTTP protocol to talk to git servers.
15    Http,
16    /// Use the HTTPS protocol to talk to git servers.
17    Https,
18    /// Any other protocol or transport that isn't known at compile time.
19    ///
20    /// It's used to support plug-in transports.
21    Ext(String),
22}
23
24impl<'a> From<&'a str> for Scheme {
25    fn from(value: &'a str) -> Self {
26        match value {
27            // "ssh+git" and "git+ssh" are legacy, but Git still allows them and so should we
28            "ssh" | "ssh+git" | "git+ssh" => Scheme::Ssh,
29            "file" => Scheme::File,
30            "git" => Scheme::Git,
31            "http" => Scheme::Http,
32            "https" => Scheme::Https,
33            unknown => Scheme::Ext(unknown.into()),
34        }
35    }
36}
37
38impl Scheme {
39    /// Return ourselves parseable name.
40    pub fn as_str(&self) -> &str {
41        use Scheme::*;
42        match self {
43            File => "file",
44            Git => "git",
45            Ssh => "ssh",
46            Http => "http",
47            Https => "https",
48            Ext(name) => name.as_str(),
49        }
50    }
51}
52
53impl std::fmt::Display for Scheme {
54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        f.write_str(self.as_str())
56    }
57}