diff --git a/scarb/src/ops/proc_macro_server/methods/expand_attribute.rs b/scarb/src/ops/proc_macro_server/methods/expand_attribute.rs new file mode 100644 index 000000000..eafa085f7 --- /dev/null +++ b/scarb/src/ops/proc_macro_server/methods/expand_attribute.rs @@ -0,0 +1,29 @@ +use crate::{ + compiler::plugin::proc_macro::{ExpansionKind, ProcMacroHost}, + ops::proc_macro_server::json_rpc::Handler, +}; +use anyhow::Result; +use proc_macro_server_api::methods::{expand::ExpandAttribute, ProcMacroResult}; +use std::sync::Arc; + +impl Handler for ExpandAttribute { + fn handle(proc_macros: Arc, params: Self::Params) -> Result { + let instance = proc_macros + .macros() + .into_iter() + .find(|e| { + e.get_expansions() + .iter() + .filter(|expansion| expansion.kind == ExpansionKind::Attr) + .any(|expansion| expansion.name == params.attr) + }) + .unwrap(); + + let result = instance.generate_code(params.attr.into(), params.args, params.item); + + Ok(ProcMacroResult { + token_stream: result.token_stream, + diagnostics: result.diagnostics, + }) + } +} diff --git a/scarb/src/ops/proc_macro_server/methods/mod.rs b/scarb/src/ops/proc_macro_server/methods/mod.rs index fea512b83..d4c1e839e 100644 --- a/scarb/src/ops/proc_macro_server/methods/mod.rs +++ b/scarb/src/ops/proc_macro_server/methods/mod.rs @@ -1 +1,2 @@ pub mod defined_macros; +pub mod expand_attribute; diff --git a/scarb/src/ops/proc_macro_server/mod.rs b/scarb/src/ops/proc_macro_server/mod.rs index 1450b9620..b63c130a2 100644 --- a/scarb/src/ops/proc_macro_server/mod.rs +++ b/scarb/src/ops/proc_macro_server/mod.rs @@ -2,7 +2,10 @@ use crate::compiler::plugin::proc_macro::ProcMacroHost; use anyhow::{anyhow, Result}; use connection::Connection; use json_rpc::{ErrResponse, Handler}; -use proc_macro_server_api::{methods::defined_macros::DefinedMacros, Method, RpcResponse}; +use proc_macro_server_api::{ + methods::{defined_macros::DefinedMacros, expand::ExpandAttribute}, + Method, RpcResponse, +}; use serde_json::Value; use std::sync::Arc; @@ -27,6 +30,9 @@ pub fn start_proc_macro_server(proc_macros: ProcMacroHost) -> Result<()> { DefinedMacros::METHOD => { run_handler::(proc_macros.clone(), request.value) } + ExpandAttribute::METHOD => { + run_handler::(proc_macros.clone(), request.value) + } _ => Err(anyhow!("method not found")), };