1use crate::{GitHubResource, error::ConversionError};
2use std::{fmt, str::FromStr};
3use strum_macros::EnumIter;
4
5#[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 GitHub,
12 GhProxy,
14 Xget,
16 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}