xref: /third_party/rust/crates/cxx/syntax/report.rs (revision 33d722a9)
1use quote::ToTokens;
2use std::fmt::Display;
3use syn::{Error, Result};
4
5pub struct Errors {
6    errors: Vec<Error>,
7}
8
9impl Errors {
10    pub fn new() -> Self {
11        Errors { errors: Vec::new() }
12    }
13
14    pub fn error(&mut self, sp: impl ToTokens, msg: impl Display) {
15        self.errors.push(Error::new_spanned(sp, msg));
16    }
17
18    pub fn push(&mut self, error: Error) {
19        self.errors.push(error);
20    }
21
22    pub fn propagate(&mut self) -> Result<()> {
23        let mut iter = self.errors.drain(..);
24        let mut all_errors = match iter.next() {
25            Some(err) => err,
26            None => return Ok(()),
27        };
28        for err in iter {
29            all_errors.combine(err);
30        }
31        Err(all_errors)
32    }
33}
34