Skip to content

Commit

Permalink
feat: add openrpc_specification() method to RpcServer trait
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Aug 1, 2023
1 parent 72d2576 commit 74b0bb8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion yerpc-derive/src/openrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn generate_method(method: &RemoteProcedure, definitions: Ident) -> TokenStream
}

/// A macro generating an OpenRPC Document.
fn generate_doc(info: &RpcInfo) -> TokenStream {
pub(crate) fn generate_doc(info: &RpcInfo) -> TokenStream {
let definitions_ident = Ident::new("definitions", Span::call_site());
let methods = &info
.methods
Expand Down
20 changes: 20 additions & 0 deletions yerpc-derive/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use crate::{util::is_result_ty, Inputs, RpcInfo};
use proc_macro2::TokenStream;
use quote::quote;

#[cfg(feature = "openrpc")]
use crate::openrpc::generate_doc;

pub(crate) fn generate_rpc_impl(info: &RpcInfo) -> TokenStream {
let mut request_arms = vec![];
let mut notification_arms = vec![];
Expand Down Expand Up @@ -66,12 +69,29 @@ pub(crate) fn generate_rpc_impl(info: &RpcInfo) -> TokenStream {

let struc = &info.self_ty;
let crat = quote! { ::yerpc };

#[cfg(feature = "openrpc")]
let openrpc_doc = generate_doc(info);

#[cfg(not(feature = "openrpc"))]
let openrpc_specification_method = quote! {};

#[cfg(feature = "openrpc")]
let openrpc_specification_method = quote! {
fn openrpc_specification() -> Result<String, #crat::Error> {
let doc = #openrpc_doc;
let json = ::serde_json::to_string_pretty(&doc)?;
Ok(json.to_string())
}
};
let (impl_generics, _ty_generics, where_clause) = &info.generics.split_for_impl();

quote! {
#[automatically_derived]
#[::yerpc::async_trait]
impl #impl_generics #crat::RpcServer for #struc #where_clause {
#openrpc_specification_method

async fn handle_request(
&self,
method: String,
Expand Down
6 changes: 6 additions & 0 deletions yerpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ pub use integrations::*;

#[async_trait]
pub trait RpcServer: Sync + Send + 'static {
/// Returns OpenRPC specification as a string.
#[cfg(feature = "openrpc")]
fn openrpc_specification() -> Result<String> {
Ok(String::new())
}

async fn handle_notification(&self, _method: String, _params: serde_json::Value) -> Result<()> {
Ok(())
}
Expand Down

0 comments on commit 74b0bb8

Please sign in to comment.