github-proxy 0.1.1

github-proxy
Documentation
use crate::{Resource, error::ConversionError};
use std::{fmt, str::FromStr};
use strum_macros::EnumIter;

/// Proxy service types
#[cfg_attr(feature = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(EnumIter, Debug, PartialEq, Hash, Eq, Clone, Copy, Default)]
pub enum Proxy {
    /// Native Github (no proxy)
    #[default]
    Github,
    /// gh-proxy.com service
    GhProxy,
    /// xget.xi-xu.me service
    Xget,
    /// cdn.jsdelivr.net service
    Jsdelivr,
    /// cdn.statically.io service
    Statically,
}

impl FromStr for Proxy {
    type Err = ConversionError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "github" => Ok(Proxy::Github),
            "gh-proxy" => Ok(Proxy::GhProxy),
            "xget" => Ok(Proxy::Xget),
            "jsdelivr" => Ok(Proxy::Jsdelivr),
            "statically" => Ok(Proxy::Statically),
            _ => Err(ConversionError::InvalidProxyType(s.to_string())),
        }
    }
}

impl fmt::Display for Proxy {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Proxy::Github => write!(f, "github"),
            Proxy::GhProxy => write!(f, "gh-proxy"),
            Proxy::Xget => write!(f, "xget"),
            Proxy::Jsdelivr => write!(f, "jsdelivr"),
            Proxy::Statically => write!(f, "statically"),
        }
    }
}

impl Proxy {
    pub fn url(&self, resource: Resource) -> Option<String> {
        resource.url(self)
    }
}