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
#![forbid(unsafe_code)]
#![cfg_attr(DIAGNOSTICS, feature(proc_macro_diagnostic))]
use proc_macro::TokenStream;
use std::{fs, sync::Mutex};
mod antlion;
mod haskell;
mod rust;
mod toml;
#[proc_macro_attribute]
pub fn hs_bindgen(attrs: TokenStream, input: TokenStream) -> TokenStream {
let mut output = input.clone();
let item_fn: syn::ItemFn = syn::parse(input)
.expect("failed to parse as Rust code the content of `#[hs_bindgen]` macro");
let (signature, extern_c_wrapper) = rust::generate(attrs, item_fn);
static SIGNATURES: Mutex<Vec<haskell::Signature>> = Mutex::new(vec![]);
let signatures = &mut *SIGNATURES.lock().unwrap();
signatures.push(signature);
let module = toml::config()
.default
.expect("your `hsbindgen.toml` file should contain a `default` field");
fs::write(
format!("src/{module}.hs"),
haskell::template(&module, signatures),
)
.unwrap_or_else(|_| panic!("fail to write `src/{module}.hs` file"));
output.extend(extern_c_wrapper);
output
}