jsonrpc_types/
lib.rs

1//! A set of types for representing JSON-RPC requests and responses as defined in
2//! the [JSON-RPC 1.0 spec](https://www.jsonrpc.org/specification_v1) and
3//! [JSON-RPC 2.0 spec](https://www.jsonrpc.org/specification).
4//!
5//! # Usage
6//!
7//! ## Creates JSON-RPC 1.0 request
8//!
9//! ```rust
10//! use jsonrpc_types::v1::{Call, MethodCall, Notification, Request};
11//!
12//! // Creates a JSON-RPC 1.0 method call request
13//! let method_call = MethodCall::new("foo", vec![], 1.into());
14//! let method_call_req = Request::Single(Call::MethodCall(method_call));
15//! assert_eq!(
16//!     serde_json::to_string(&method_call_req).unwrap(),
17//!     r#"{"method":"foo","params":[],"id":1}"#
18//! );
19//!
20//! // Creates a JSON-RPC 1.0 notification request
21//! let notification = Notification::new("foo", vec![]);
22//! let notification_req = Request::Single(Call::Notification(notification.clone()));
23//! assert_eq!(
24//!     serde_json::to_string(&notification_req).unwrap(),
25//!     r#"{"method":"foo","params":[],"id":null}"#
26//! );
27//!
28//! // Creates a JSON-RPC 1.0 batch request
29//! let batch_request = Request::Batch(vec![
30//!     Call::MethodCall(MethodCall::new("foo", vec![], 1.into())),
31//!     Call::MethodCall(MethodCall::new("bar", vec![], 2.into())),
32//! ]);
33//! assert_eq!(
34//!     serde_json::to_string(&batch_request).unwrap(),
35//!     r#"[{"method":"foo","params":[],"id":1},{"method":"bar","params":[],"id":2}]"#
36//! );
37//! ```
38//!
39//! ## Creates JSON-RPC 1.0 response
40//!
41//! ```rust
42//! use jsonrpc_types::v1::{Value, Error, Output, Response};
43//!
44//! // Creates a JSON-RPC 1.0 success response
45//! let success_response = Output::success(Value::Bool(true), 1.into());
46//! let response1 = Response::Single(success_response.clone());
47//! assert_eq!(
48//!     serde_json::to_string(&response1).unwrap(),
49//!     r#"{"result":true,"error":null,"id":1}"#
50//! );
51//!
52//! // Creates a JSON-RPC 1.0 failure response
53//! let failure_response = Output::<Value>::failure(Error::invalid_request(), None);
54//! let response2 = Response::Single(failure_response.clone());
55//! assert_eq!(
56//!     serde_json::to_string(&response2).unwrap(),
57//!     r#"{"result":null,"error":{"code":-32600,"message":"Invalid request"},"id":null}"#
58//! );
59//!
60//! // Creates a JSON-RPC 1.0 batch response
61//! let success1 = Output::success(Value::Bool(true), 1.into());
62//! let success2 = Output::success(Value::Bool(false), 2.into());
63//! let batch_response = Response::Batch(vec![success1, success2]);
64//! assert_eq!(
65//!     serde_json::to_string(&batch_response).unwrap(),
66//!     r#"[{"result":true,"error":null,"id":1},{"result":false,"error":null,"id":2}]"#
67//! );
68//! ```
69//!
70//! ## Creates JSON-RPC 2.0 request
71//!
72//! ```rust
73//! use jsonrpc_types::{Params, MethodCall, Notification, Call, Request};
74//!
75//! // Creates a JSON-RPC 2.0 method call request
76//! let method_call = MethodCall::new("foo", Some(Params::Array(vec![])), 1.into());
77//! let method_call_req = Request::Single(Call::MethodCall(method_call));
78//! assert_eq!(
79//!     serde_json::to_string(&method_call_req).unwrap(),
80//!     r#"{"jsonrpc":"2.0","method":"foo","params":[],"id":1}"#
81//! );
82//!
83//! // Creates a JSON-RPC 2.0 notification request
84//! let notification = Notification::new("foo", Some(Params::Array(vec![])));
85//! let notification_req = Request::Single(Call::Notification(notification.clone()));
86//! assert_eq!(
87//!     serde_json::to_string(&notification_req).unwrap(),
88//!     r#"{"jsonrpc":"2.0","method":"foo","params":[]}"#
89//! );
90//!
91//! // Creates a JSON-RPC 2.0 batch request
92//! let batch_request = Request::Batch(vec![
93//!     Call::MethodCall(MethodCall::new("foo", Some(Params::Array(vec![])), 1.into())),
94//!     Call::MethodCall(MethodCall::new("bar", Some(Params::Array(vec![])), 2.into())),
95//! ]);
96//! assert_eq!(
97//!     serde_json::to_string(&batch_request).unwrap(),
98//!     r#"[{"jsonrpc":"2.0","method":"foo","params":[],"id":1},{"jsonrpc":"2.0","method":"bar","params":[],"id":2}]"#
99//! );
100//! ```
101//!
102//! ## Creates JSON-RPC 2.0 response
103//!
104//! ```rust
105//! use jsonrpc_types::{Value, Error, Success, Failure, Output, Response};
106//!
107//! // Creates a JSON-RPC 2.0 success response
108//! let success = Success::new(Value::Bool(true), 1.into());
109//! let response1 = Response::Single(Output::Success(success.clone()));
110//! assert_eq!(
111//!     serde_json::to_string(&response1).unwrap(),
112//!     r#"{"jsonrpc":"2.0","result":true,"id":1}"#
113//! );
114//!
115//! // Creates a JSON-RPC 2.0 failure response
116//! let failure = Failure::new(Error::invalid_request(), None);
117//! let response2 = Response::<Value>::Single(Output::Failure(failure.clone()));
118//! assert_eq!(
119//!     serde_json::to_string(&response2).unwrap(),
120//!     r#"{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request"},"id":null}"#
121//! );
122//!
123//! // Creates a JSON-RPC 2.0 batch response
124//! let success1 = Output::success(Value::Bool(true), 1.into());
125//! let success2 = Output::success(Value::Bool(false), 2.into());
126//! let batch_response = Response::Batch(vec![success1, success2]);
127//! assert_eq!(
128//!     serde_json::to_string(&batch_response).unwrap(),
129//!     r#"[{"jsonrpc":"2.0","result":true,"id":1},{"jsonrpc":"2.0","result":false,"id":2}]"#
130//! );
131//! ```
132//!
133
134#![deny(unused_imports)]
135#![deny(missing_docs)]
136
137// Export JSON-RPC 2.0 types by default
138pub use self::v2::*;
139
140/// JSON-RPC 1.0 types.
141pub mod v1;
142/// JSON-RPC 2.0 types.
143pub mod v2;
144
145mod error;
146mod id;