about summary refs log tree commit diff
path: root/tvix/nix-compat-derive/src/internal/ctx.rs
blob: ba770e044bc21058883c456cfbf9d8edab36b1b6 (plain) (blame)
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
39
40
41
42
43
44
45
46
47
48
49
50
use std::cell::RefCell;
use std::fmt;
use std::thread::panicking;

use quote::ToTokens;

pub struct Context {
    errors: RefCell<Option<Vec<syn::Error>>>,
}

impl Context {
    pub fn new() -> Context {
        Context {
            errors: RefCell::new(Some(Vec::new())),
        }
    }

    pub fn syn_error(&self, error: syn::Error) {
        self.errors
            .borrow_mut()
            .as_mut()
            .take()
            .unwrap()
            .push(error);
    }

    pub fn error_spanned<T: ToTokens, D: fmt::Display>(&self, tokens: T, message: D) {
        self.syn_error(syn::Error::new_spanned(tokens, message));
    }

    pub fn check(&self) -> syn::Result<()> {
        let mut iter = self.errors.borrow_mut().take().unwrap().into_iter();
        let mut err = match iter.next() {
            None => return Ok(()),
            Some(err) => err,
        };
        for next_err in iter {
            err.combine(next_err);
        }
        Err(err)
    }
}

impl Drop for Context {
    fn drop(&mut self) {
        if self.errors.borrow().is_some() && !panicking() {
            panic!("Context dropped without checking errors");
        }
    }
}