use crate::credentials::Credential;
use crate::error::Error;
use crate::error::Result;
use crate::provider::ApiKeyConfig;
pub fn from_env(config: &ApiKeyConfig) -> Option<String> {
let var = config.env_var.as_deref()?;
match std::env::var(var) {
Ok(value) if !value.trim().is_empty() => Some(value.trim().to_string()),
_ => None,
}
}
pub fn accept(provider: &str, config: &ApiKeyConfig, key: &str) -> Result<Credential> {
let key = key.trim();
if key.is_empty() {
return Err(Error::Malformed("API key was empty".into()));
}
if let Some(prefix) = &config.key_prefix {
if !key.starts_with(prefix) {
return Err(Error::Malformed(format!(
"API key does not start with the expected `{prefix}` prefix"
)));
}
}
Ok(Credential::api_key(provider.to_string(), key.to_string()))
}