use std::borrow::Cow;
use std::error::Error;
use std::fmt;
pub mod credentials;
pub mod region;
pub mod token;
#[derive(Debug)]
pub struct ProviderAttempt<E> {
name: Cow<'static, str>,
error: E,
}
impl<E> ProviderAttempt<E> {
pub(crate) fn new(name: Cow<'static, str>, error: E) -> Self {
Self { name, error }
}
pub fn name(&self) -> &str {
&self.name
}
pub fn error(&self) -> &E {
&self.error
}
}
#[derive(Debug)]
pub struct ProviderChainError<E: Error> {
attempts: Vec<ProviderAttempt<E>>,
}
impl<E: Error> ProviderChainError<E> {
pub(crate) fn new(attempts: Vec<ProviderAttempt<E>>) -> Self {
Self { attempts }
}
pub fn attempts(&self) -> &[ProviderAttempt<E>] {
&self.attempts
}
}
impl<E: Error> fmt::Display for ProviderChainError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.attempts.is_empty() {
return write!(f, "no providers were configured in the chain");
}
write!(f, "no credentials found in chain. Attempted:")?;
for attempt in &self.attempts {
write!(f, "\n {}: {}", attempt.name, attempt.error)?;
let mut source = attempt.error.source();
while let Some(cause) = source {
write!(f, ": {cause}")?;
source = cause.source();
}
}
Ok(())
}
}
impl<E: Error + 'static> Error for ProviderChainError<E> {}