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
34
35
36
37
38
use crate::error::CompileError;

use core::cell::RefCell;

/// A handler with which you can emit errors.
#[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<CompileError>,
}

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

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

/// Proof that an error was emitted through a `Handler`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ErrorEmitted {
    _priv: (),
}