use std::collections::HashSet;
use std::error::Error;
use std::fmt;
use std::panic::Location;
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>,
entries: Vec<PrepareEntry>,
text: Box<dyn Fn(&HashSet<TypeName>) -> String + Send + Sync>,
permanent: bool,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct PrepareEntry {
names: Vec<TypeName>,
location: Option<&'static Location<'static>>,
subject: Option<String>,
scopes: Vec<String>,
}
impl PrepareEntry {
pub fn new(names: impl IntoIterator<Item = TypeName>) -> Self {
PrepareEntry { names: names.into_iter().collect(), ..PrepareEntry::default() }
}
pub fn at(mut self, location: &'static Location<'static>) -> Self {
self.location = Some(location);
self
}
pub fn about(mut self, subject: impl Into<String>) -> Self {
self.subject = Some(subject.into());
self
}
pub fn scoped(mut self, scopes: impl IntoIterator<Item = String>) -> Self {
self.scopes = scopes.into_iter().collect();
self
}
pub fn names(&self) -> &[TypeName] {
&self.names
}
pub fn location(&self) -> Option<&'static Location<'static>> {
self.location
}
pub fn subject(&self) -> Option<&str> {
self.subject.as_deref()
}
pub fn scopes(&self) -> &[String] {
&self.scopes
}
}
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(), entries: Vec::new(), text: Box::new(text), permanent: false }
}
pub fn with_permanent(mut self, permanent: bool) -> Self {
self.permanent = permanent;
self
}
pub fn is_permanent(&self) -> bool {
self.permanent
}
pub fn with_entries(mut self, entries: Vec<PrepareEntry>) -> Self {
self.entries = entries;
self
}
pub fn names(&self) -> &[TypeName] {
&self.names
}
pub fn entries(&self) -> &[PrepareEntry] {
&self.entries
}
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 {}