aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc/src/lib.rs
blob: 8a547d98d1336dba4664862b8b1d74952c39c5ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! A fast and lightweight async [JSONRPC 2.0](https://www.jsonrpc.org/specification) implementation.
//!
//! # Example
//!
//! ```
//! use std::sync::Arc;
//!
//! use serde_json::Value;
//!
//! use karyons_jsonrpc::{JsonRPCError, Server, Client, register_service};
//!
//! struct HelloWorld {}
//!
//! impl HelloWorld {
//!     async fn say_hello(&self, params: Value) -> Result<Value, JsonRPCError> {
//!         let msg: String = serde_json::from_value(params)?;
//!         Ok(serde_json::json!(format!("Hello {msg}!")))
//!     }
//! }
//!
//! // Server
//! async {
//!     let ex = Arc::new(smol::Executor::new());
//!
//!     // Creates a new server
//!     let endpoint = "tcp://127.0.0.1:60000".parse().unwrap();
//!     let server = Server::new_with_endpoint(&endpoint, ex.clone()).await.unwrap();
//!
//!     // Register the HelloWorld service
//!     register_service!(HelloWorld, say_hello);
//!     server.attach_service(HelloWorld{});
//!
//!     // Starts the server
//!     ex.run(server.start());
//! };
//!
//! // Client
//! async {
//!
//!     // Creates a new client
//!     let endpoint = "tcp://127.0.0.1:60000".parse().unwrap();
//!     let client = Client::new_with_endpoint(&endpoint, None).await.unwrap();
//!
//!     let result: String = client.call("HelloWorld.say_hello", "world".to_string()).await.unwrap();
//! };
//!
//! ```

mod client;
mod error;
pub mod message;
mod server;
mod utils;

pub const JSONRPC_VERSION: &str = "2.0";

use error::{Error, Result};

pub use client::Client;
pub use error::Error as JsonRPCError;
pub use server::{RPCMethod, RPCService, Server};