use crate::{
ProviderName,
ProviderRegistryError,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProviderSelection {
Auto,
Named {
primary: ProviderName,
fallbacks: Vec<ProviderName>,
},
}
impl ProviderSelection {
#[inline]
pub fn auto() -> Self {
Self::Auto
}
#[inline]
pub fn named(primary: &str) -> Result<Self, ProviderRegistryError> {
Ok(Self::Named {
primary: ProviderName::new(primary)?,
fallbacks: Vec::new(),
})
}
#[inline]
pub fn from_names(primary: &str, fallbacks: &[&str]) -> Result<Self, ProviderRegistryError> {
Ok(Self::Named {
primary: ProviderName::new(primary)?,
fallbacks: normalize_borrowed_names(fallbacks)?,
})
}
#[inline]
pub fn from_owned_names(
primary: &str,
fallbacks: &[String],
) -> Result<Self, ProviderRegistryError> {
Ok(Self::Named {
primary: ProviderName::new(primary)?,
fallbacks: normalize_owned_names(fallbacks)?,
})
}
#[inline]
pub fn is_auto(&self) -> bool {
matches!(self, Self::Auto)
}
#[inline]
pub fn primary(&self) -> Option<&ProviderName> {
match self {
Self::Auto => None,
Self::Named { primary, .. } => Some(primary),
}
}
#[inline]
pub fn fallbacks(&self) -> &[ProviderName] {
match self {
Self::Auto => &[],
Self::Named { fallbacks, .. } => fallbacks,
}
}
}
impl Default for ProviderSelection {
#[inline]
fn default() -> Self {
Self::Auto
}
}
fn normalize_owned_names(names: &[String]) -> Result<Vec<ProviderName>, ProviderRegistryError> {
names
.iter()
.map(String::as_str)
.map(ProviderName::new)
.collect()
}
fn normalize_borrowed_names(names: &[&str]) -> Result<Vec<ProviderName>, ProviderRegistryError> {
names.iter().copied().map(ProviderName::new).collect()
}