Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement core::error::Error for ErrorIterator #451

Open
tgross35 opened this issue Feb 5, 2024 · 0 comments
Open

Implement core::error::Error for ErrorIterator #451

tgross35 opened this issue Feb 5, 2024 · 0 comments

Comments

@tgross35
Copy link

tgross35 commented Feb 5, 2024

To do this ErrorIterator needs a Display impl, which could require making it a wrapper over Box<dyn Iterator<Item = ValidationError<'a>> + Sync + Send + 'a>, rather than just a type alias (a mild breaking change).

This will allow throwing this error to dyn Error or anyhow without any manual mapping. This impl should probably just print a list of all errors, like the examples show. Roughly:

type ErrorIterTy<'a> = Box<dyn Iterator<Item = ValidationError<'a>> + Sync + Send + 'a>;
pub struct ErrorIterator<'a>(ErrorIterTy<'a>);

impl<'a> IntoIterator for ErrorIterator<'a> {
    type Item = ValidationError<'a>;
    type IntoIter: ErrorIterTy<'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.0
    }
}

impl fmt::Display for ErrorIterator {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        writeln!(f, "Validation errors:");
        for (idx, e) in self.enumerate() {
            writeln!(f, "{idx:02}: {e}")?;
        }
        Ok(())
    }
}

impl core::error::Error for ErrorIterator {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant