aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc/jsonrpc_macro
diff options
context:
space:
mode:
authorhozan23 <hozan23@karyontech.net>2024-05-21 02:20:45 +0200
committerhozan23 <hozan23@karyontech.net>2024-05-22 15:02:06 +0200
commit028940fe3e0a87cdc421a6d07f1ecfb6c208b9d0 (patch)
tree3272d5c71cafb098e548cb9811e8f9ddc260ef2f /jsonrpc/jsonrpc_macro
parent0f0cefb62ee8b641dcabcc0a2a1cf019c1de4843 (diff)
jsonrpc: support pubsub
Diffstat (limited to 'jsonrpc/jsonrpc_macro')
-rw-r--r--jsonrpc/jsonrpc_macro/src/lib.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/jsonrpc/jsonrpc_macro/src/lib.rs b/jsonrpc/jsonrpc_macro/src/lib.rs
index c3d51e8..5acfa5e 100644
--- a/jsonrpc/jsonrpc_macro/src/lib.rs
+++ b/jsonrpc/jsonrpc_macro/src/lib.rs
@@ -45,3 +45,41 @@ pub fn rpc_impl(_attr: TokenStream, item: TokenStream) -> TokenStream {
quoted.into()
}
+
+// TODO remove duplicate code
+#[proc_macro_attribute]
+pub fn rpc_pubsub_impl(_attr: TokenStream, item: TokenStream) -> TokenStream {
+ let mut methods: Vec<Ident> = vec![];
+
+ let item2 = item.clone();
+ let parsed_input = parse_macro_input!(item2 as ItemImpl);
+
+ let self_ty = match *parsed_input.self_ty {
+ Type::Path(p) => p,
+ _ => err!(
+ parsed_input.span(),
+ "implementing the trait `RPCService` on this type is unsupported"
+ ),
+ };
+
+ if parsed_input.items.is_empty() {
+ err!(self_ty.span(), "At least one method should be implemented");
+ }
+
+ for item in parsed_input.items {
+ match item {
+ ImplItem::Method(method) => {
+ methods.push(method.sig.ident);
+ }
+ _ => err!(item.span(), "unexpected item"),
+ }
+ }
+
+ let item2: TokenStream2 = item.into();
+ let quoted = quote! {
+ karyon_jsonrpc::impl_pubsub_rpc_service!(#self_ty, #(#methods),*);
+ #item2
+ };
+
+ quoted.into()
+}