github_proxy/
proxy.rs

1use crate::{GitHubResource, 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)]
9pub enum Proxy {
10    /// Native GitHub (no proxy)
11    GitHub,
12    /// gh-proxy.com service
13    GhProxy,
14    /// xget.xi-xu.me service
15    Xget,
16    /// cdn.jsdelivr.net service
17    Jsdelivr,
18}
19
20impl FromStr for Proxy {
21    type Err = ConversionError;
22    fn from_str(s: &str) -> Result<Self, Self::Err> {
23        match s.to_lowercase().as_str() {
24            "github" => Ok(Proxy::GitHub),
25            "gh-proxy" => Ok(Proxy::GhProxy),
26            "xget" => Ok(Proxy::Xget),
27            "jsdelivr" => Ok(Proxy::Jsdelivr),
28            _ => Err(ConversionError::InvalidProxyType(s.to_string())),
29        }
30    }
31}
32
33impl fmt::Display for Proxy {
34    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35        match self {
36            Proxy::GitHub => write!(f, "github"),
37            Proxy::GhProxy => write!(f, "gh-proxy"),
38            Proxy::Xget => write!(f, "xget"),
39            Proxy::Jsdelivr => write!(f, "jsdelivr"),
40        }
41    }
42}
43
44impl Proxy {
45    pub fn url(&self, resource: GitHubResource) -> String {
46        resource.url(self)
47    }
48}