use crate::{Resource, error::ConversionError};
use std::{fmt, str::FromStr};
use strum_macros::EnumIter;
#[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 {
#[default]
Github,
GhProxy,
Xget,
Jsdelivr,
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)
}
}