use std::collections::HashSet;
use std::error::Error;
use std::fmt;
use crate::redact::{Redacted, SecretRegistry, redact, redact_as};
use crate::timer::BoxError;
use crate::type_name::TypeName;
pub struct ConfigureErrors {
errors: Vec<ConfigureError>,
}
#[non_exhaustive]
#[derive(Debug)]
pub struct ConfigureError {
pub transport: TypeName,
pub source: Redacted,
transport_full: bool,
}
impl ConfigureErrors {
pub(crate) fn collect(refused: Vec<(TypeName, BoxError)>, secrets: &SecretRegistry) -> Self {
let named = refused.iter().filter_map(|(_, error)| error.downcast_ref::<PrepareError>());
let transports = refused.iter().map(|(transport, _)| *transport);
let full = TypeName::colliding(transports.chain(named.flat_map(|error| error.names.iter().copied())));
let errors = refused
.into_iter()
.map(|(transport, error)| {
let text = error.downcast_ref::<PrepareError>().map(|prepared| prepared.text(&full));
let source = match text {
Some(text) => redact_as(secrets, error, text),
None => redact(secrets, error),
};
ConfigureError { transport, source, transport_full: full.contains(&transport) }
})
.collect();
ConfigureErrors { errors }
}
pub fn iter(&self) -> std::slice::Iter<'_, ConfigureError> {
self.errors.iter()
}
pub fn len(&self) -> usize {
self.errors.len()
}
pub fn is_empty(&self) -> bool {
self.errors.is_empty()
}
}
impl<'a> IntoIterator for &'a ConfigureErrors {
type Item = &'a ConfigureError;
type IntoIter = std::slice::Iter<'a, ConfigureError>;
fn into_iter(self) -> Self::IntoIter {
self.errors.iter()
}
}
impl fmt::Display for ConfigureErrors {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let count = self.errors.len();
let noun = if count == 1 { "error" } else { "errors" };
write!(f, "error: configuration failed with {count} {noun}")?;
for error in &self.errors {
write!(f, "\n\n × {error}")?;
}
Ok(())
}
}
impl fmt::Debug for ConfigureErrors {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl Error for ConfigureErrors {}
impl fmt::Display for ConfigureError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.transport_full {
write!(f, "transport `{:#}`: {}", self.transport, self.source)
} else {
write!(f, "transport `{}`: {}", self.transport, self.source)
}
}
}
impl Error for ConfigureError {}
pub struct PrepareError {
names: Vec<TypeName>,
text: Box<dyn Fn(&HashSet<TypeName>) -> String + Send + Sync>,
}
impl PrepareError {
pub fn new(
names: impl IntoIterator<Item = TypeName>,
text: impl Fn(&HashSet<TypeName>) -> String + Send + Sync + 'static,
) -> Self {
PrepareError { names: names.into_iter().collect(), text: Box::new(text) }
}
pub fn names(&self) -> &[TypeName] {
&self.names
}
pub fn text(&self, full: &HashSet<TypeName>) -> String {
(self.text)(full)
}
}
impl fmt::Display for PrepareError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.text(&TypeName::colliding(self.names.iter().copied())))
}
}
impl fmt::Debug for PrepareError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl Error for PrepareError {}