#![deny(clippy::all)]
#![deny(clippy::pedantic)]
#![forbid(unsafe_code)]
pub struct ErrorIterator<'a> {
inner: Option<&'a (dyn std::error::Error + 'static)>,
}
impl<'a> Iterator for ErrorIterator<'a> {
type Item = &'a (dyn std::error::Error + 'static);
fn next(&mut self) -> Option<Self::Item> {
if let Some(error) = self.inner.take() {
self.inner = error.source();
return Some(error);
}
None
}
}
pub trait ErrorIter: std::error::Error + Sized + 'static {
fn sources(&self) -> ErrorIterator {
ErrorIterator { inner: Some(self) }
}
}
impl<T> ErrorIter for T where T: std::error::Error + Sized + 'static {}
#[cfg(test)]
mod tests {
use super::*;
use thiserror::Error;
#[derive(Debug, Error)]
enum Error {
#[error("Nested error: {0}")]
Nested(#[source] Box<Error>),
#[error("Leaf error")]
Leaf,
}
#[test]
fn iter_sources_ok() {
let error = Error::Nested(Box::new(Error::Nested(Box::new(Error::Leaf))));
let mut iter = error.sources();
assert_eq!(
"Nested error: Nested error: Leaf error".to_string(),
iter.next().unwrap().to_string()
);
assert_eq!(
"Nested error: Leaf error".to_string(),
iter.next().unwrap().to_string()
);
assert_eq!("Leaf error".to_string(), iter.next().unwrap().to_string());
assert!(iter.next().is_none());
assert!(iter.next().is_none());
}
}