use crate::config::{Config, ModelEndpoint};
use std::collections::HashSet;
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct EndpointName(String);
impl EndpointName {
pub fn new(name: String, config: &Config) -> Result<Self, String> {
let endpoint_name = Self(name.clone());
if endpoint_name.is_valid(config) {
Ok(endpoint_name)
} else {
Err(format!(
"Unknown endpoint: '{}'. Available endpoints: {}",
name,
Self::list_available(config).join(", ")
))
}
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn is_valid(&self, config: &Config) -> bool {
config.models.fast.iter().any(|e| e.name() == self.0)
|| config.models.balanced.iter().any(|e| e.name() == self.0)
|| config.models.deep.iter().any(|e| e.name() == self.0)
}
fn list_available(config: &Config) -> Vec<String> {
let mut names = Vec::new();
names.extend(config.models.fast.iter().map(|e| e.name().to_string()));
names.extend(config.models.balanced.iter().map(|e| e.name().to_string()));
names.extend(config.models.deep.iter().map(|e| e.name().to_string()));
names
}
}
impl From<&ModelEndpoint> for EndpointName {
fn from(endpoint: &ModelEndpoint) -> Self {
Self(endpoint.name().to_string())
}
}
#[cfg(test)]
impl From<String> for EndpointName {
fn from(name: String) -> Self {
Self(name)
}
}
#[cfg(test)]
impl From<&str> for EndpointName {
fn from(name: &str) -> Self {
Self(name.to_string())
}
}
pub type ExclusionSet = HashSet<EndpointName>;