Skip to content

Commit ed06c31

Browse files
committed
use passed attributes for Attribute Macro, use getset for derive test
1 parent e2aa05e commit ed06c31

File tree

2 files changed

+28
-42
lines changed

2 files changed

+28
-42
lines changed

src/lib.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ impl ProcMacroLibraryLibloading {
102102
let lib = Library::new(file).map_err(|e| e.to_string())?;
103103

104104
let exported_macros = {
105-
// data already implies reference
106105
let macros: libloading::Symbol<&&[ProcMacro]> = unsafe { lib.get(symbol_name.as_bytes()) }
107106
.map_err(|e| e.to_string())?;
108107

@@ -199,44 +198,51 @@ impl Expander {
199198

200199
pub fn expand(
201200
&self,
202-
code: &str,
203-
macro_to_expand: &str,
201+
macro_name: &str,
202+
macro_body: &str,
203+
attributes: Option<&String>,
204204
) -> Result<String, proc_macro::bridge::PanicMessage> {
205-
let token_stream =
206-
parse_string(code).expect(&format!("Error while parsing this code: '{}'", code));
205+
let parsed_body = parse_string(macro_body).expect(
206+
&format!("Error while parsing this code: '{}'", macro_body)
207+
);
208+
209+
let parsed_attributes = attributes.map_or(proc_macro2::TokenStream::new(), |attr| {
210+
parse_string(attr).expect(
211+
&format!("Error while parsing this code: '{}'", attr)
212+
)
213+
});
207214

208215
for lib in &self.libs {
209216
for proc_macro in &lib.exported_macros {
210217
match proc_macro {
211218
ProcMacro::CustomDerive {
212219
trait_name, client, ..
213-
} if *trait_name == macro_to_expand => {
220+
} if *trait_name == macro_name => {
214221
let res = client.run(
215222
&EXEC_STRATEGY,
216223
rustc_server::Rustc::default(),
217-
token_stream,
224+
parsed_body,
218225
);
219226

220227
return res.map(|token_stream| token_stream.to_string());
221228
}
222229

223-
ProcMacro::Bang { name, client } if *name == macro_to_expand => {
230+
ProcMacro::Bang { name, client } if *name == macro_name => {
224231
let res = client.run(
225232
&EXEC_STRATEGY,
226233
rustc_server::Rustc::default(),
227-
token_stream,
234+
parsed_body,
228235
);
229236

230237
return res.map(|token_stream| token_stream.to_string());
231238
}
232239

233-
ProcMacro::Attr { name, client } if *name == macro_to_expand => {
234-
// fixme attr macro needs two inputs
240+
ProcMacro::Attr { name, client } if *name == macro_name => {
235241
let res = client.run(
236242
&EXEC_STRATEGY,
237243
rustc_server::Rustc::default(),
238-
proc_macro2::TokenStream::new(),
239-
token_stream,
244+
parsed_attributes,
245+
parsed_body,
240246
);
241247

242248
return res.map(|token_stream| token_stream.to_string());
@@ -260,7 +266,7 @@ pub fn expand_task(task: &ExpansionTask) -> ExpansionResult {
260266
&format!("Cannot expand with provided libraries: ${:?}", &task.libs)
261267
);
262268

263-
let result = match expander.expand(&task.macro_body, &task.macro_name) {
269+
let result = match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) {
264270
Ok(expansion) => ExpansionResult::Success { expansion },
265271

266272
Err(msg) => {

tests/expansion_correctness_tests.rs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -208,46 +208,26 @@ fn test_simple_bang_proc_macros() -> io::Result<()> {
208208
fn test_proc_macro_libraries() {
209209
let tmp_dir = TempDir::new().expect("Cannot create temp dir");
210210
setup_project_with_derives(&tmp_dir.path()).expect("Cannot setup test project");
211-
let serde_derive_lib = compile_proc_macro(&tmp_dir.path(), "serde_derive")
211+
let getset_lib = compile_proc_macro(&tmp_dir.path(), "getset")
212212
.expect("Cannot find proc macro!");
213213

214214
{
215-
let serialize_macro_task = ExpansionTask {
216-
libs: vec![serde_derive_lib.clone()],
217-
macro_body: "struct S {}".to_string(),
218-
macro_name: "Serialize".to_string(),
215+
let expansion_task = ExpansionTask {
216+
libs: vec![getset_lib.clone()],
217+
macro_body: "struct S { #[set] y: i32 }".to_string(),
218+
macro_name: "Setters".to_string(),
219219
attributes: None,
220220
};
221221

222-
let expansion_result = perform_expansion(serialize_macro_task).expect(
223-
"Cannot perform expansion for 'id_macro'"
222+
let expansion_result = perform_expansion(expansion_task).expect(
223+
"Cannot perform expansion for 'Setters'"
224224
);
225225

226226
assert_matches!(
227227
expansion_result,
228228
ExpansionResult::Success { ref expansion }
229-
if expansion.contains("_serde :: Serialize")
229+
if expansion.contains("fn set_y")
230230
);
231231
}
232-
233-
{
234-
let deserialize_macro_task = ExpansionTask {
235-
libs: vec![serde_derive_lib.clone()],
236-
macro_body: "struct S { x: i32 } ".to_string(),
237-
macro_name: "Deserialize".to_string(),
238-
attributes: None,
239-
};
240-
241-
let expansion_result = perform_expansion(deserialize_macro_task).expect(
242-
"Cannot perform expansion for 'serde::Deserialize'"
243-
);
244-
245-
assert_matches!(
246-
expansion_result,
247-
ExpansionResult::Success { ref expansion }
248-
if expansion.contains("_serde :: Deserializer")
249-
);
250-
}
251-
252232
}
253233

0 commit comments

Comments
 (0)