aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_proc_macro
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_proc_macro')
-rw-r--r--crates/ra_proc_macro/src/lib.rs81
1 files changed, 76 insertions, 5 deletions
diff --git a/crates/ra_proc_macro/src/lib.rs b/crates/ra_proc_macro/src/lib.rs
index 31e1bb209..a7a84249f 100644
--- a/crates/ra_proc_macro/src/lib.rs
+++ b/crates/ra_proc_macro/src/lib.rs
@@ -1,7 +1,78 @@
1#[cfg(test)] 1//! Client-side Proc-Macro crate
2mod tests { 2//!
3 #[test] 3//! We separate proc-macro expanding logic to an extern program to allow
4 fn it_works() { 4//! different implementations (e.g. wasm or dylib loading). And this crate
5 assert_eq!(2 + 2, 4); 5//! is used for provide basic infra-structure for commnicate between two
6//! process: Client (RA itself), Server (the external program)
7
8use ra_mbe::ExpandError;
9use ra_tt::Subtree;
10use std::{
11 path::{Path, PathBuf},
12 sync::Arc,
13};
14
15trait ProcMacroExpander: std::fmt::Debug + Send + Sync + std::panic::RefUnwindSafe {
16 fn custom_derive(&self, subtree: &Subtree, derive_name: &str) -> Result<Subtree, ExpandError>;
17}
18
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct ProcMacroProcessExpander {
21 process_path: PathBuf,
22}
23
24impl ProcMacroExpander for ProcMacroProcessExpander {
25 fn custom_derive(
26 &self,
27 _subtree: &Subtree,
28 _derive_name: &str,
29 ) -> Result<Subtree, ExpandError> {
30 // FIXME: do nothing for now
31 Ok(Subtree::default())
32 }
33}
34
35#[derive(Debug, Clone)]
36pub struct ProcMacro {
37 expander: Arc<Box<dyn ProcMacroExpander>>,
38 name: String,
39}
40
41impl Eq for ProcMacro {}
42impl PartialEq for ProcMacro {
43 fn eq(&self, other: &ProcMacro) -> bool {
44 self.name == other.name && Arc::ptr_eq(&self.expander, &other.expander)
45 }
46}
47
48impl ProcMacro {
49 pub fn name(&self) -> String {
50 self.name.clone()
51 }
52
53 pub fn custom_derive(&self, subtree: &Subtree) -> Result<Subtree, ExpandError> {
54 self.expander.custom_derive(subtree, &self.name)
55 }
56}
57
58#[derive(Debug, Clone, PartialEq, Eq)]
59pub enum ProcMacroClient {
60 Process { expander: Arc<ProcMacroProcessExpander> },
61 Dummy,
62}
63
64impl ProcMacroClient {
65 pub fn extern_process(process_path: &Path) -> ProcMacroClient {
66 let expander = ProcMacroProcessExpander { process_path: process_path.into() };
67 ProcMacroClient::Process { expander: Arc::new(expander) }
68 }
69
70 pub fn dummy() -> ProcMacroClient {
71 ProcMacroClient::Dummy
72 }
73
74 pub fn by_dylib_path(&self, _dylib_path: &Path) -> Vec<ProcMacro> {
75 // FIXME: return empty for now
76 vec![]
6 } 77 }
7} 78}