arch_mirrors/
protocol.rs

1//! This is where the [`Protocol`](Protocol) structs and its dependencies go.
2use quick_error::quick_error;
3use serde::{Deserialize, Serialize};
4use std::str::FromStr;
5
6quick_error! {
7    /// The possible errors that could happen when working with [`Protocol`](Protocol).
8    #[derive(Debug)]
9    pub enum Error {
10        /// An invalid string was passed to [`Protocol::from_str()`].
11        InvalidProtocol(protocol: String) {
12            display("can't parse '{}' to a valid protocol", protocol)
13        }
14    }
15}
16
17/// This contains every supported protocol by Arch Linux mirror status as of the time of writing
18/// (05/20/2021).
19#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
20pub enum Protocol {
21    /// The HTTP protocol.
22    #[serde(rename = "http")]
23    Http,
24
25    /// The HTTPS protocol.
26    #[serde(rename = "https")]
27    Https,
28
29    /// The rsync protocol.
30    #[serde(rename = "rsync")]
31    Rsync,
32}
33
34impl FromStr for Protocol {
35    type Err = Error;
36
37    fn from_str(s: &str) -> Result<Self, Self::Err> {
38        match s {
39            "http" => Ok(Self::Http),
40            "https" => Ok(Self::Https),
41            "rsync" => Ok(Self::Rsync),
42            other => Err(Error::InvalidProtocol(other.into())),
43        }
44    }
45}