use std::sync::Arc;
use async_trait::async_trait;
use mentra::provider_core::{CredentialSource, ProviderCredentials, ProviderError};
#[derive(Clone)]
pub(super) struct Credential(Option<Arc<str>>);
impl Credential {
pub(super) fn new(api_key: Option<&str>) -> Self {
Self(api_key.map(Arc::from))
}
pub(super) const fn is_some(&self) -> bool {
self.0.is_some()
}
}
#[async_trait]
impl CredentialSource for Credential {
async fn credentials(&self) -> Result<ProviderCredentials, ProviderError> {
Ok(ProviderCredentials {
bearer_token: self.0.as_deref().map(str::to_string),
..ProviderCredentials::default()
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn a_key_becomes_the_bearer_token() {
let credentials = Credential::new(Some("k"))
.credentials()
.await
.expect("resolves");
assert_eq!(credentials.bearer_token.as_deref(), Some("k"));
}
#[tokio::test]
async fn no_key_means_no_token_rather_than_an_empty_one() {
let credentials = Credential::new(None).credentials().await.expect("resolves");
assert_eq!(credentials.bearer_token, None);
assert!(!Credential::new(None).is_some());
}
}