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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//! A framework-agnostic JSON-RPC 2.0 implementation with Bring Your Own Transport.
//!
//! This library handles the JSON-RPC protocol layer including message parsing,
//! method routing, and response generation. You register methods with the
//! `JsonRpc` handler, then process JSON-RPC messages from any transport.
//!
//! # Bring Your Own Transport
//!
//! The library does not include transport implementations. You read JSON strings
//! from your transport (stdio, HTTP, WebSocket, TCP, etc.), call `JsonRpc::call()`,
//! and write the response back. This gives you full control over your transport
//! layer.
//!
//! # Quick Start
//!
//! Create a handler and process a message:
//!
//! ```no_run
//! use json_rpc::JsonRpc;
//! use serde_json::Value;
//!
//! async fn echo(params: Value) -> Result<Value, json_rpc::Error> {
//! Ok(params)
//! }
//!
//! # tokio::runtime::Runtime::new().unwrap().block_on(async {
//! let json_rpc = JsonRpc::new()
//! .add("echo", echo);
//!
//! // Read from your transport
//! let message = r#"{"jsonrpc":"2.0","method":"echo","params":"hello","id":1}"#;
//!
//! // Process the message
//! if let Some(response) = json_rpc.call(message).await {
//! // Write to your transport
//! println!("{}", response);
//! }
//! # });
//! ```
//!
//! # Stdio Example
//!
//! Read newline-delimited JSON from stdin and write responses to stdout:
//!
//! ```no_run
//! use json_rpc::JsonRpc;
//! use serde_json::Value;
//! use tokio::io::AsyncBufReadExt;
//!
//! async fn echo(params: Value) -> Result<Value, json_rpc::Error> {
//! Ok(params)
//! }
//!
//! # tokio::runtime::Runtime::new().unwrap().block_on(async {
//! let json_rpc = JsonRpc::new().add("echo", echo);
//!
//! let stdin = tokio::io::stdin();
//! let mut reader = tokio::io::BufReader::new(stdin);
//! let mut line = String::new();
//!
//! while reader.read_line(&mut line).await.unwrap() > 0 {
//! let trimmed = line.trim();
//! if !trimmed.is_empty() {
//! if let Some(response) = json_rpc.call(trimmed).await {
//! println!("{}", response);
//! }
//! }
//! line.clear();
//! }
//! # });
//! ```
//!
//! # Struct Parameters
//!
//! Handlers can use struct parameters for complex APIs:
//!
//! ```no_run
//! use json_rpc::JsonRpc;
//! use serde::Deserialize;
//!
//! # tokio::runtime::Runtime::new().unwrap().block_on(async {
//! #[derive(Deserialize)]
//! struct InitializeParams {
//! name: String,
//! version: String,
//! }
//!
//! async fn initialize(params: InitializeParams) -> Result<String, json_rpc::Error> {
//! Ok(format!("Server {} v{} initialized", params.name, params.version))
//! }
//!
//! let json_rpc = JsonRpc::new()
//! .add("initialize", initialize);
//! # });
//! ```
//!
//! # Error Handling
//!
//! Methods return `Result<T, Error>`. Create JSON-RPC protocol errors with
//! specific codes:
//!
//! ```no_run
//! use json_rpc::{JsonRpc, Error};
//!
//! async fn divide(params: (i32, i32)) -> Result<i32, Error> {
//! if params.1 == 0 {
//! return Err(Error::rpc(-32000, "Division by zero"));
//! }
//! Ok(params.0 / params.1)
//! }
//!
//! let json_rpc = JsonRpc::new().add("divide", divide);
//! ```
//!
//! # Axum Integration
//!
//! The axum feature provides a handler for HTTP integration. Enable the feature
//! in Cargo.toml:
//!
//! ```toml
//! [dependencies]
//! json-rpc-rs = { version = "0.2", features = ["axum"] }
//! ```
//!
//! ```no_run
//! # #[cfg(feature = "axum")]
//! # {
//! use json_rpc::{JsonRpc, axum::handler};
//! use axum::Router;
//! use std::sync::Arc;
//!
//! async fn echo(params: serde_json::Value) -> Result<serde_json::Value, json_rpc::Error> {
//! Ok(params)
//! }
//!
//! let json_rpc = JsonRpc::new().add("echo", echo);
//! let app = Router::new()
//! .route("/jsonrpc", handler)
//! .with_state(Arc::new(json_rpc));
//! # }
//! ```
pub use Error;
pub use JsonRpc;
pub use ;