zng_ext_hot_reload_proc_macros/
util.rs

1use proc_macro2::{Span, TokenStream};
2use quote::{ToTokens, quote_spanned};
3
4/// `Ident` with custom span.
5macro_rules! ident_spanned {
6    ($span:expr=> $($format_name:tt)+) => {
7        proc_macro2::Ident::new(&format!($($format_name)+), $span)
8    };
9}
10
11/// `Ident` with call_site span.
12macro_rules! ident {
13    ($($tt:tt)*) => {
14        ident_spanned!(proc_macro2::Span::call_site()=> $($tt)*)
15    };
16}
17
18/// Collection of compile errors.
19#[derive(Default)]
20pub struct Errors {
21    tokens: TokenStream,
22}
23impl Errors {
24    /// Push a compile error.
25    pub fn push(&mut self, error: impl ToString, span: Span) {
26        let error = error.to_string();
27        self.tokens.extend(quote_spanned! {span=>
28            compile_error!{#error}
29        })
30    }
31
32    /// Push all compile errors in `error`.
33    pub fn push_syn(&mut self, error: syn::Error) {
34        for error in error {
35            let span = error.span();
36            self.push(error, span);
37        }
38    }
39
40    pub fn is_empty(&self) -> bool {
41        self.tokens.is_empty()
42    }
43}
44impl ToTokens for Errors {
45    fn to_tokens(&self, tokens: &mut TokenStream) {
46        tokens.extend(self.tokens.clone())
47    }
48    fn to_token_stream(&self) -> TokenStream {
49        self.tokens.clone()
50    }
51    fn into_token_stream(self) -> TokenStream {
52        self.tokens
53    }
54}