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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#![cfg_attr(feature = "nightly", feature(proc_macro_expand))]
use camino::{Utf8Path, Utf8PathBuf};
use quote::{format_ident, quote};
use std::env;
use syn::{bracketed, punctuated::Punctuated, LitStr, Token};
mod export;
mod util;
use self::export::expand_export;
#[proc_macro_attribute]
pub fn export(
_attr: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let input2 = proc_macro2::TokenStream::from(input.clone());
let gen_output = || {
let mod_path = util::mod_path()?;
let item = syn::parse(input)?;
expand_export(item, &mod_path)
};
let output = gen_output().unwrap_or_else(syn::Error::into_compile_error);
quote! {
#input2
#output
}
.into()
}
#[proc_macro]
pub fn build_foreign_language_testcases(paths: proc_macro::TokenStream) -> proc_macro::TokenStream {
let paths = syn::parse_macro_input!(paths as FilePaths);
let pkg_dir = env::var("CARGO_MANIFEST_DIR")
.expect("Missing $CARGO_MANIFEST_DIR, cannot build tests for generated bindings");
let udl_files = &paths
.udl_files
.iter()
.map(|file_path| {
let pathbuf: Utf8PathBuf = [&pkg_dir, file_path].iter().collect();
let path = pathbuf.to_string();
quote! { #path }
})
.collect::<Vec<proc_macro2::TokenStream>>();
let test_functions = paths.test_scripts
.iter()
.map(|file_path| {
let test_file_pathbuf: Utf8PathBuf = [&pkg_dir, file_path].iter().collect();
let test_file_path = test_file_pathbuf.to_string();
let test_file_name = test_file_pathbuf
.file_name()
.expect("Test file has no name, cannot build tests for generated bindings");
let test_name = format_ident!(
"uniffi_foreign_language_testcase_{}",
test_file_name.replace(|c: char| !c.is_alphanumeric(), "_")
);
let maybe_ignore = if should_skip_path(&test_file_pathbuf) {
quote! { #[ignore] }
} else {
quote! { }
};
quote! {
#maybe_ignore
#[test]
fn #test_name () -> uniffi::deps::anyhow::Result<()> {
uniffi::testing::run_foreign_language_testcase(#pkg_dir, &[ #(#udl_files),* ], #test_file_path)
}
}
})
.collect::<Vec<proc_macro2::TokenStream>>();
let test_module = quote! {
#(#test_functions)*
};
proc_macro::TokenStream::from(test_module)
}
fn should_skip_path(path: &Utf8Path) -> bool {
let ext = path.extension().expect("File has no extension!");
env::var("UNIFFI_TESTS_DISABLE_EXTENSIONS")
.map(|v| v.split(',').any(|look| look == ext))
.unwrap_or(false)
}
#[derive(Debug)]
struct FilePaths {
udl_files: Vec<String>,
test_scripts: Vec<String>,
}
impl syn::parse::Parse for FilePaths {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let udl_array;
bracketed!(udl_array in input);
let udl_files = Punctuated::<LitStr, Token![,]>::parse_terminated(&udl_array)?
.iter()
.map(|s| s.value())
.collect();
let _comma: Token![,] = input.parse()?;
let scripts_array;
bracketed!(scripts_array in input);
let test_scripts = Punctuated::<LitStr, Token![,]>::parse_terminated(&scripts_array)?
.iter()
.map(|s| s.value())
.collect();
Ok(FilePaths {
udl_files,
test_scripts,
})
}
}
#[proc_macro]
pub fn include_scaffolding(component_name: proc_macro::TokenStream) -> proc_macro::TokenStream {
let name = syn::parse_macro_input!(component_name as syn::LitStr);
if std::env::var("OUT_DIR").is_err() {
quote! {
compile_error!("This macro assumes the crate has a build.rs script, but $OUT_DIR is not present");
}
} else {
quote! {
include!(concat!(env!("OUT_DIR"), "/", #name, ".uniffi.rs"));
}
}.into()
}
#[proc_macro]
pub fn generate_and_include_scaffolding(
udl_file: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let udl_file = syn::parse_macro_input!(udl_file as syn::LitStr);
let udl_file_string = udl_file.value();
let udl_file_path = Utf8Path::new(&udl_file_string);
if std::env::var("OUT_DIR").is_err() {
quote! {
compile_error!("This macro assumes the crate has a build.rs script, but $OUT_DIR is not present");
}
} else if uniffi_build::generate_scaffolding(udl_file_path).is_err() {
quote! {
compile_error!(concat!("Failed to generate scaffolding from UDL file at ", #udl_file));
}
} else {
let name = LitStr::new(udl_file_path.file_stem().unwrap(), udl_file.span());
quote! {
uniffi_macros::include_scaffolding!(#name);
}
}.into()
}