rustify_derive/
error.rs

1use proc_macro2::Span;
2use quote::quote_spanned;
3
4/// The general error object returned by functions in this crate.
5///
6/// The error object can be directly converted from a [syn::Error] as well as
7/// be converted directly into a [proc_macro2::TokenStream] to be returned to
8/// the compiler.
9#[derive(Debug)]
10pub struct Error(proc_macro2::TokenStream);
11
12impl Error {
13    /// Returns a new instance of [Error] using the given [Span] and message.
14    ///
15    /// This uses [quote_spanned!] in order to provide more accurate information
16    /// to the compiler about the exact location of the error.
17    pub fn new(span: Span, message: &str) -> Error {
18        Error(quote_spanned! { span =>
19            compile_error!(#message);
20        })
21    }
22
23    pub fn into_tokens(self) -> proc_macro2::TokenStream {
24        self.0
25    }
26}
27
28impl From<syn::Error> for Error {
29    fn from(e: syn::Error) -> Error {
30        Error(e.to_compile_error())
31    }
32}