[−][src]Crate easy_jsonrpc
Easy jsonrpc
Generates rpc handlers based on a trait definition.
use easy_jsonrpc::{self, JSONRPCServer}; // the jsonrpc_server generates a JSONRPCServer for &dyn Adder #[easy_jsonrpc::jsonrpc_server] pub trait Adder { fn checked_add(&self, a: isize, b: isize) -> Option<isize>; fn wrapping_add(&self, a: isize, b: isize) -> isize; fn is_some(&self, a: Option<usize>) -> bool; fn takes_ref(&self, rf: &isize); } struct AdderImpl; impl Adder for AdderImpl { fn checked_add(&self, a: isize, b: isize) -> Option<isize> { a.checked_add(b) } fn wrapping_add(&self, a: isize, b: isize) -> isize { a.wrapping_add(b) } fn is_some(&self, a: Option<usize>) -> bool { a.is_some() } fn takes_ref(&self, rf: &isize) {} } // create an rpc handler let adder = (&AdderImpl {} as &dyn Adder); assert_eq!( adder.handle_raw( r#"{"jsonrpc": "2.0", "method": "wrapping_add", "params": [1, 2], "id": 1}"# ), Some(r#"{"jsonrpc":"2.0","result":3,"id":1}"#.into()) ); // Named arguments are handled automatically assert_eq!( adder.handle_raw( r#"{"jsonrpc": "2.0", "method": "wrapping_add", "params": {"a": 1, "b":2}, "id": 1}"# ), Some(r#"{"jsonrpc":"2.0","result":3,"id":1}"#.into()) ); // Calls with no id are treated as notifications assert_eq!( adder.handle_raw(r#"{"jsonrpc": "2.0", "method": "wrapping_add", "params": [1, 1]}"#), None ); // Calls with no return value return unit, aka `()` in rust, aka `null` in json assert_eq!( adder.handle_raw(r#"{"jsonrpc": "2.0", "method": "takes_ref", "params": [1], "id": 1}"#), Some(r#"{"jsonrpc":"2.0","result":null,"id":1}"#.into()) );
Multilple types may implement the trait
#[easy_jsonrpc::jsonrpc_server] pub trait Useless {} struct ImplOne; impl Useless for ImplOne {} enum ImplTwo {} impl Useless for ImplTwo {}
This library contains a server generator. No client generator has been implemented yet.
Re-exports
pub use easy_jsonrpc_proc_macro::jsonrpc_server; |
Traits
JSONRPCServer | Handles jsonrpc calls. |