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 crate::ParseError;

use core::cell::RefCell;

/// A handler with which you can emit errors.
///
/// (For now these are only parser errors, but this will change in the future.)
#[derive(Default)]
pub struct Handler {
    /// The inner handler.
    /// This construction is used to avoid `&mut` all over the compiler.
    inner: RefCell<HandlerInner>,
}

/// Contains the actual data for `Handler`.
/// Modelled this way to afford an API using interior mutability.
#[derive(Default)]
struct HandlerInner {
    /// The sink through which errors will be emitted.
    sink: Vec<ParseError>,
}

impl Handler {
    /// Emit the error `err`.
    pub fn emit_err(&self, err: ParseError) {
        self.inner.borrow_mut().sink.push(err);
    }

    /// Extract all the errors from this handler.
    pub fn into_errors(self) -> Vec<ParseError> {
        self.inner.into_inner().sink
    }
}