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
//! A set of types for representing JSON-RPC requests and responses as defined in //! the [JSON-RPC 1.0 spec](https://www.jsonrpc.org/specification_v1) and //! [JSON-RPC 2.0 spec](https://www.jsonrpc.org/specification). //! //! # Usage //! //! ## Creates JSON-RPC 1.0 request //! //! ```rust //! use jsonrpc_types::v1::{Call, MethodCall, Notification, Request}; //! //! // Creates a JSON-RPC 1.0 method call request //! let method_call = MethodCall::new("foo", vec![], 1.into()); //! let method_call_req = Request::Single(Call::MethodCall(method_call)); //! assert_eq!( //! serde_json::to_string(&method_call_req).unwrap(), //! r#"{"method":"foo","params":[],"id":1}"# //! ); //! //! // Creates a JSON-RPC 1.0 notification request //! let notification = Notification::new("foo", vec![]); //! let notification_req = Request::Single(Call::Notification(notification.clone())); //! assert_eq!( //! serde_json::to_string(¬ification_req).unwrap(), //! r#"{"method":"foo","params":[],"id":null}"# //! ); //! //! // Creates a JSON-RPC 1.0 batch request //! let batch_request = Request::Batch(vec![ //! Call::MethodCall(MethodCall::new("foo", vec![], 1.into())), //! Call::MethodCall(MethodCall::new("bar", vec![], 2.into())), //! ]); //! assert_eq!( //! serde_json::to_string(&batch_request).unwrap(), //! r#"[{"method":"foo","params":[],"id":1},{"method":"bar","params":[],"id":2}]"# //! ); //! ``` //! //! ## Creates JSON-RPC 1.0 response //! //! ```rust //! use jsonrpc_types::v1::{Value, Error, Output, Response}; //! //! // Creates a JSON-RPC 1.0 success response //! let success_response = Output::success(Value::Bool(true), 1.into()); //! let response1 = Response::Single(success_response.clone()); //! assert_eq!( //! serde_json::to_string(&response1).unwrap(), //! r#"{"result":true,"error":null,"id":1}"# //! ); //! //! // Creates a JSON-RPC 1.0 failure response //! let failure_response = Output::<Value>::failure(Error::invalid_request(), None); //! let response2 = Response::Single(failure_response.clone()); //! assert_eq!( //! serde_json::to_string(&response2).unwrap(), //! r#"{"result":null,"error":{"code":-32600,"message":"Invalid request"},"id":null}"# //! ); //! //! // Creates a JSON-RPC 1.0 batch response //! let success1 = Output::success(Value::Bool(true), 1.into()); //! let success2 = Output::success(Value::Bool(false), 2.into()); //! let batch_response = Response::Batch(vec![success1, success2]); //! assert_eq!( //! serde_json::to_string(&batch_response).unwrap(), //! r#"[{"result":true,"error":null,"id":1},{"result":false,"error":null,"id":2}]"# //! ); //! ``` //! //! ## Creates JSON-RPC 2.0 request //! //! ```rust //! use jsonrpc_types::{Params, MethodCall, Notification, Call, Request}; //! //! // Creates a JSON-RPC 2.0 method call request //! let method_call = MethodCall::new("foo", Some(Params::Array(vec![])), 1.into()); //! let method_call_req = Request::Single(Call::MethodCall(method_call)); //! assert_eq!( //! serde_json::to_string(&method_call_req).unwrap(), //! r#"{"jsonrpc":"2.0","method":"foo","params":[],"id":1}"# //! ); //! //! // Creates a JSON-RPC 2.0 notification request //! let notification = Notification::new("foo", Some(Params::Array(vec![]))); //! let notification_req = Request::Single(Call::Notification(notification.clone())); //! assert_eq!( //! serde_json::to_string(¬ification_req).unwrap(), //! r#"{"jsonrpc":"2.0","method":"foo","params":[]}"# //! ); //! //! // Creates a JSON-RPC 2.0 batch request //! let batch_request = Request::Batch(vec![ //! Call::MethodCall(MethodCall::new("foo", Some(Params::Array(vec![])), 1.into())), //! Call::MethodCall(MethodCall::new("bar", Some(Params::Array(vec![])), 2.into())), //! ]); //! assert_eq!( //! serde_json::to_string(&batch_request).unwrap(), //! r#"[{"jsonrpc":"2.0","method":"foo","params":[],"id":1},{"jsonrpc":"2.0","method":"bar","params":[],"id":2}]"# //! ); //! ``` //! //! ## Creates JSON-RPC 2.0 response //! //! ```rust //! use jsonrpc_types::{Value, Error, Success, Failure, Output, Response}; //! //! // Creates a JSON-RPC 2.0 success response //! let success = Success::new(Value::Bool(true), 1.into()); //! let response1 = Response::Single(Output::Success(success.clone())); //! assert_eq!( //! serde_json::to_string(&response1).unwrap(), //! r#"{"jsonrpc":"2.0","result":true,"id":1}"# //! ); //! //! // Creates a JSON-RPC 2.0 failure response //! let failure = Failure::new(Error::invalid_request(), None); //! let response2 = Response::<Value>::Single(Output::Failure(failure.clone())); //! assert_eq!( //! serde_json::to_string(&response2).unwrap(), //! r#"{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request"},"id":null}"# //! ); //! //! // Creates a JSON-RPC 2.0 batch response //! let success1 = Output::success(Value::Bool(true), 1.into()); //! let success2 = Output::success(Value::Bool(false), 2.into()); //! let batch_response = Response::Batch(vec![success1, success2]); //! assert_eq!( //! serde_json::to_string(&batch_response).unwrap(), //! r#"[{"jsonrpc":"2.0","result":true,"id":1},{"jsonrpc":"2.0","result":false,"id":2}]"# //! ); //! ``` //! #![deny(unused_imports)] #![deny(missing_docs)] // Export JSON-RPC 2.0 types by default pub use self::v2::*; /// JSON-RPC 1.0 types. pub mod v1; /// JSON-RPC 2.0 types. pub mod v2; mod error; mod id;