2021-07-01 09:50:54 +00:00
|
|
|
use quote::ToTokens;
|
|
|
|
use std::{cell::RefCell, fmt::Display, thread};
|
|
|
|
|
|
|
|
#[derive(Default)]
|
2022-12-01 08:03:03 +00:00
|
|
|
pub struct ASTResult {
|
2023-02-13 01:29:49 +00:00
|
|
|
errors: RefCell<Option<Vec<syn::Error>>>,
|
2021-07-01 09:50:54 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 08:03:03 +00:00
|
|
|
impl ASTResult {
|
2023-02-13 01:29:49 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
ASTResult {
|
|
|
|
errors: RefCell::new(Some(Vec::new())),
|
2021-07-01 09:50:54 +00:00
|
|
|
}
|
2023-02-13 01:29:49 +00:00
|
|
|
}
|
2021-07-01 09:50:54 +00:00
|
|
|
|
2023-02-13 01:29:49 +00:00
|
|
|
pub fn error_spanned_by<A: ToTokens, T: Display>(&self, obj: A, msg: T) {
|
|
|
|
self
|
|
|
|
.errors
|
|
|
|
.borrow_mut()
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.push(syn::Error::new_spanned(obj.into_token_stream(), msg));
|
|
|
|
}
|
2021-07-01 09:50:54 +00:00
|
|
|
|
2023-02-13 01:29:49 +00:00
|
|
|
pub fn syn_error(&self, err: syn::Error) {
|
|
|
|
self.errors.borrow_mut().as_mut().unwrap().push(err);
|
|
|
|
}
|
2021-07-01 09:50:54 +00:00
|
|
|
|
2023-02-13 01:29:49 +00:00
|
|
|
pub fn check(self) -> Result<(), Vec<syn::Error>> {
|
|
|
|
let errors = self.errors.borrow_mut().take().unwrap();
|
|
|
|
match errors.len() {
|
|
|
|
0 => Ok(()),
|
|
|
|
_ => Err(errors),
|
2021-07-01 09:50:54 +00:00
|
|
|
}
|
2023-02-13 01:29:49 +00:00
|
|
|
}
|
2021-07-01 09:50:54 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 08:03:03 +00:00
|
|
|
impl Drop for ASTResult {
|
2023-02-13 01:29:49 +00:00
|
|
|
fn drop(&mut self) {
|
|
|
|
if !thread::panicking() && self.errors.borrow().is_some() {
|
|
|
|
panic!("forgot to check for errors");
|
2021-07-01 09:50:54 +00:00
|
|
|
}
|
2023-02-13 01:29:49 +00:00
|
|
|
}
|
2021-07-01 09:50:54 +00:00
|
|
|
}
|