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
49
50
51
52
53
54
55
56
57
58
59
60
#![recursion_limit = "128"]
#![doc(html_root_url = "https://docs.rs/externref-macro/0.1.0")]
#![warn(missing_debug_implementations, missing_docs, bare_trait_objects)]
#![warn(clippy::all, clippy::pedantic)]
#![allow(clippy::must_use_candidate, clippy::module_name_repetitions)]
extern crate proc_macro;
use proc_macro::TokenStream;
use syn::Item;
mod externref;
use crate::externref::{for_export, for_foreign_module};
#[proc_macro_attribute]
pub fn externref(_attr: TokenStream, input: TokenStream) -> TokenStream {
const MSG: &str = "Unsupported item; only `extern \"C\" {}` modules and `extern \"C\" fn ...` \
exports are supported";
let output = match syn::parse::<Item>(input) {
Ok(Item::ForeignMod(mut module)) => for_foreign_module(&mut module),
Ok(Item::Fn(mut function)) => for_export(&mut function),
Ok(other_item) => {
return darling::Error::custom(MSG)
.with_span(&other_item)
.write_errors()
.into()
}
Err(err) => return err.into_compile_error().into(),
};
output.into()
}