File: util.rs

package info (click to toggle)
rust-deluxe-macros 0.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 276 kB
  • sloc: makefile: 4
file content (48 lines) | stat: -rw-r--r-- 1,350 bytes parent folder | download | duplicates (2)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use syn::parse::{Parse, Parser};

use deluxe_core::Errors;

#[inline]
pub fn parse<T: Parse>(input: proc_macro::TokenStream, errors: &Errors) -> Option<T> {
    errors.push_result(<T as Parse>::parse.parse(input))
}

fn crate_path(errors: Option<&Errors>) -> Option<syn::Path> {
    use proc_macro_crate::FoundCrate;
    const CRATE_NAME: &str = "deluxe";

    let crate_name = match proc_macro_crate::crate_name(CRATE_NAME) {
        Ok(FoundCrate::Name(name)) => name,
        Ok(FoundCrate::Itself) => CRATE_NAME.into(),
        Err(e) => {
            if let Some(errors) = errors {
                errors.push(proc_macro2::Span::call_site(), e.to_string());
            }
            return None;
        }
    };

    let ident = syn::Ident::new(&crate_name, proc_macro2::Span::call_site());
    Some(syn::parse_quote! { ::#ident })
}

#[inline]
pub fn get_crate_path(path: Option<Option<syn::Path>>, errors: &Errors) -> Option<syn::Path> {
    match path {
        Some(Some(path)) => Some(path),
        Some(None) => crate_path(Some(errors)),
        None => crate_path(None),
    }
}

macro_rules! quote_mixed {
    ($($tt:tt)*) => {
        quote::quote_spanned! { Span::mixed_site() => $($tt)* }
    };
}

macro_rules! parse_quote_mixed {
    ($($tt:tt)*) => {
        syn::parse_quote_spanned! { Span::mixed_site() => $($tt)* }
    };
}