ra_proc_macro: cleanups here and there

This commit is contained in:
veetaha 2020-04-20 21:26:10 +03:00
parent 36840bd6c7
commit d3019164dc
12 changed files with 141 additions and 190 deletions

View file

@ -3,10 +3,10 @@
//! This library is able to call compiled Rust custom derive dynamic libraries on arbitrary code.
//! The general idea here is based on https://github.com/fedochet/rust-proc-macro-expander.
//!
//! But we change some several design for fitting RA needs:
//! But we adapt it to better fit RA needs:
//!
//! * We use `ra_tt` for proc-macro `TokenStream` server, it is easy to manipute and interact with
//! RA then proc-macro2 token stream.
//! * We use `ra_tt` for proc-macro `TokenStream` server, it is easy to manipulate and interact with
//! RA than `proc-macro2` token stream.
//! * By **copying** the whole rustc `lib_proc_macro` code, we are able to build this with `stable`
//! rustc rather than `unstable`. (Although in gerenal ABI compatibility is still an issue)
@ -21,36 +21,28 @@ mod dylib;
use proc_macro::bridge::client::TokenStream;
use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask};
use std::path::Path;
pub(crate) fn expand_task(task: &ExpansionTask) -> Result<ExpansionResult, String> {
let expander = dylib::Expander::new(&task.lib)
.expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib));
let expander = create_expander(&task.lib);
match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) {
Ok(expansion) => Ok(ExpansionResult { expansion }),
Err(msg) => {
let reason = format!(
"Cannot perform expansion for {}: error {:?}!",
&task.macro_name,
msg.as_str()
);
Err(reason)
Err(format!("Cannot perform expansion for {}: error {:?}", &task.macro_name, msg))
}
}
}
pub(crate) fn list_macros(task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
let expander = dylib::Expander::new(&task.lib)
.expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib));
pub(crate) fn list_macros(task: &ListMacrosTask) -> ListMacrosResult {
let expander = create_expander(&task.lib);
match expander.list_macros() {
Ok(macros) => Ok(ListMacrosResult { macros }),
Err(msg) => {
let reason =
format!("Cannot perform expansion for {:?}: error {:?}!", &task.lib, msg.as_str());
Err(reason)
}
}
ListMacrosResult { macros: expander.list_macros() }
}
fn create_expander(lib: &Path) -> dylib::Expander {
dylib::Expander::new(lib)
.unwrap_or_else(|err| panic!("Cannot create expander for {:?}: {:?}", lib, err))
}
pub mod cli;