rustify_derive/
error.rs

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
use proc_macro2::Span;
use quote::quote_spanned;

/// The general error object returned by functions in this crate.
///
/// The error object can be directly converted from a [syn::Error] as well as
/// be converted directly into a [proc_macro2::TokenStream] to be returned to
/// the compiler.
#[derive(Debug)]
pub struct Error(proc_macro2::TokenStream);

impl Error {
    /// Returns a new instance of [Error] using the given [Span] and message.
    ///
    /// This uses [quote_spanned!] in order to provide more accurate information
    /// to the compiler about the exact location of the error.
    pub fn new(span: Span, message: &str) -> Error {
        Error(quote_spanned! { span =>
            compile_error!(#message);
        })
    }

    pub fn into_tokens(self) -> proc_macro2::TokenStream {
        self.0
    }
}

impl From<syn::Error> for Error {
    fn from(e: syn::Error) -> Error {
        Error(e.to_compile_error())
    }
}