aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_proc_macro_srv/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_proc_macro_srv/src/lib.rs')
-rw-r--r--crates/ra_proc_macro_srv/src/lib.rs36
1 files changed, 14 insertions, 22 deletions
diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs
index c62b0ed89..7faf36834 100644
--- a/crates/ra_proc_macro_srv/src/lib.rs
+++ b/crates/ra_proc_macro_srv/src/lib.rs
@@ -3,10 +3,10 @@
3//! This library is able to call compiled Rust custom derive dynamic libraries on arbitrary code. 3//! This library is able to call compiled Rust custom derive dynamic libraries on arbitrary code.
4//! The general idea here is based on https://github.com/fedochet/rust-proc-macro-expander. 4//! The general idea here is based on https://github.com/fedochet/rust-proc-macro-expander.
5//! 5//!
6//! But we change some several design for fitting RA needs: 6//! But we adapt it to better fit RA needs:
7//! 7//!
8//! * We use `ra_tt` for proc-macro `TokenStream` server, it is easy to manipute and interact with 8//! * We use `ra_tt` for proc-macro `TokenStream` server, it is easy to manipulate and interact with
9//! RA then proc-macro2 token stream. 9//! RA than `proc-macro2` token stream.
10//! * By **copying** the whole rustc `lib_proc_macro` code, we are able to build this with `stable` 10//! * By **copying** the whole rustc `lib_proc_macro` code, we are able to build this with `stable`
11//! rustc rather than `unstable`. (Although in gerenal ABI compatibility is still an issue) 11//! rustc rather than `unstable`. (Although in gerenal ABI compatibility is still an issue)
12 12
@@ -21,36 +21,28 @@ mod dylib;
21 21
22use proc_macro::bridge::client::TokenStream; 22use proc_macro::bridge::client::TokenStream;
23use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask}; 23use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask};
24use std::path::Path;
24 25
25pub(crate) fn expand_task(task: &ExpansionTask) -> Result<ExpansionResult, String> { 26pub(crate) fn expand_task(task: &ExpansionTask) -> Result<ExpansionResult, String> {
26 let expander = dylib::Expander::new(&task.lib) 27 let expander = create_expander(&task.lib);
27 .expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib));
28 28
29 match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) { 29 match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) {
30 Ok(expansion) => Ok(ExpansionResult { expansion }), 30 Ok(expansion) => Ok(ExpansionResult { expansion }),
31 Err(msg) => { 31 Err(msg) => {
32 let reason = format!( 32 Err(format!("Cannot perform expansion for {}: error {:?}", &task.macro_name, msg))
33 "Cannot perform expansion for {}: error {:?}!",
34 &task.macro_name,
35 msg.as_str()
36 );
37 Err(reason)
38 } 33 }
39 } 34 }
40} 35}
41 36
42pub(crate) fn list_macros(task: &ListMacrosTask) -> Result<ListMacrosResult, String> { 37pub(crate) fn list_macros(task: &ListMacrosTask) -> ListMacrosResult {
43 let expander = dylib::Expander::new(&task.lib) 38 let expander = create_expander(&task.lib);
44 .expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib));
45 39
46 match expander.list_macros() { 40 ListMacrosResult { macros: expander.list_macros() }
47 Ok(macros) => Ok(ListMacrosResult { macros }), 41}
48 Err(msg) => { 42
49 let reason = 43fn create_expander(lib: &Path) -> dylib::Expander {
50 format!("Cannot perform expansion for {:?}: error {:?}!", &task.lib, msg.as_str()); 44 dylib::Expander::new(lib)
51 Err(reason) 45 .unwrap_or_else(|err| panic!("Cannot create expander for {:?}: {:?}", lib, err))
52 }
53 }
54} 46}
55 47
56pub mod cli; 48pub mod cli;