cxxbridge_macro/syntax/
report.rs

1use quote::ToTokens;
2use std::fmt::Display;
3use syn::{Error, Result};
4
5pub(crate) struct Errors {
6    errors: Vec<Error>,
7}
8
9impl Errors {
10    pub(crate) fn new() -> Self {
11        Errors { errors: Vec::new() }
12    }
13
14    pub(crate) fn error(&mut self, sp: impl ToTokens, msg: impl Display) {
15        self.errors.push(Error::new_spanned(sp, msg));
16    }
17
18    pub(crate) fn push(&mut self, error: Error) {
19        self.errors.push(error);
20    }
21
22    pub(crate) 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}