aboutsummaryrefslogtreecommitdiff
path: root/syntax/report.rs
blob: 1997182ada5cadc43c6f277345ee4c0a494b4a3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use quote::ToTokens;
use std::fmt::Display;
use syn::{Error, Result};

pub(crate) struct Errors {
    errors: Vec<Error>,
}

impl Errors {
    pub(crate) fn new() -> Self {
        Errors { errors: Vec::new() }
    }

    pub(crate) fn error(&mut self, sp: impl ToTokens, msg: impl Display) {
        self.errors.push(Error::new_spanned(sp, msg));
    }

    pub(crate) fn push(&mut self, error: Error) {
        self.errors.push(error);
    }

    pub(crate) fn propagate(&mut self) -> Result<()> {
        let mut iter = self.errors.drain(..);
        let mut all_errors = match iter.next() {
            Some(err) => err,
            None => return Ok(()),
        };
        for err in iter {
            all_errors.combine(err);
        }
        Err(all_errors)
    }
}