wasmi/derive/src/error.rs

38 lines
835 B
Rust
Raw Normal View History

2019-01-25 15:47:35 +00:00
use proc_macro2::{Span, TokenStream};
2019-01-25 15:46:42 +00:00
use quote::{quote_spanned, ToTokens};
macro_rules! err_span {
($span:expr, $($msg:tt)*) => (
$crate::error::CompileError::new_spanned(&$span, format!($($msg)*))
)
}
pub struct CompileError {
msg: String,
span: Option<Span>,
}
impl CompileError {
pub fn new_spanned(span: &Span, msg: String) -> Self {
CompileError {
span: Some(*span),
msg,
}
}
pub fn new(msg: String) -> Self {
2019-01-25 15:47:35 +00:00
CompileError { span: None, msg }
2019-01-25 15:46:42 +00:00
}
}
impl ToTokens for CompileError {
fn to_tokens(&self, dst: &mut TokenStream) {
let msg = &self.msg;
let span = self.span.unwrap_or_else(|| Span::call_site());
(quote_spanned! { span=>
compile_error!(#msg);
2019-01-25 15:47:35 +00:00
})
.to_tokens(dst);
2019-01-25 15:46:42 +00:00
}
}