github_proxy/
proxy.rs

1use crate::{Resource, error::ConversionError};
2use std::{fmt, str::FromStr};
3use strum_macros::EnumIter;
4
5/// Proxy service types
6#[cfg_attr(feature = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
7#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
8#[derive(EnumIter, Debug, PartialEq, Hash, Eq, Clone, Copy, Default)]
9pub enum Proxy {
10    /// Native Github (no proxy)
11    #[default]
12    Github,
13    /// gh-proxy.com service
14    GhProxy,
15    /// xget.xi-xu.me service
16    Xget,
17    /// cdn.jsdelivr.net service
18    Jsdelivr,
19    /// cdn.statically.io service
20    Statically,
21}
22
23impl FromStr for Proxy {
24    type Err = ConversionError;
25    fn from_str(s: &str) -> Result<Self, Self::Err> {
26        match s.to_lowercase().as_str() {
27            "github" => Ok(Proxy::Github),
28            "gh-proxy" => Ok(Proxy::GhProxy),
29            "xget" => Ok(Proxy::Xget),
30            "jsdelivr" => Ok(Proxy::Jsdelivr),
31            "statically" => Ok(Proxy::Statically),
32            _ => Err(ConversionError::InvalidProxyType(s.to_string())),
33        }
34    }
35}
36
37impl fmt::Display for Proxy {
38    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39        match self {
40            Proxy::Github => write!(f, "github"),
41            Proxy::GhProxy => write!(f, "gh-proxy"),
42            Proxy::Xget => write!(f, "xget"),
43            Proxy::Jsdelivr => write!(f, "jsdelivr"),
44            Proxy::Statically => write!(f, "statically"),
45        }
46    }
47}
48
49impl Proxy {
50    pub fn url(&self, resource: Resource) -> Option<String> {
51        resource.url(self)
52    }
53}