jsonrpsee_core/server/
mod.rs

1// Copyright 2019-2021 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any
4// person obtaining a copy of this software and associated
5// documentation files (the "Software"), to deal in the
6// Software without restriction, including without
7// limitation the rights to use, copy, modify, merge,
8// publish, distribute, sublicense, and/or sell copies of
9// the Software, and to permit persons to whom the Software
10// is furnished to do so, subject to the following
11// conditions:
12//
13// The above copyright notice and this permission notice
14// shall be included in all copies or substantial portions
15// of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
24// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25// DEALINGS IN THE SOFTWARE.
26
27//! Shared modules for the JSON-RPC servers.
28
29/// Error types.
30mod error;
31/// Helpers.
32pub mod helpers;
33/// Method response related types.
34mod method_response;
35/// JSON-RPC "modules" group sets of methods that belong together and handles method/subscription registration.
36mod rpc_module;
37/// Subscription related types.
38mod subscription;
39
40pub use error::*;
41pub use helpers::*;
42pub use http::Extensions;
43pub use method_response::*;
44pub use rpc_module::*;
45pub use subscription::*;
46
47use jsonrpsee_types::ErrorObjectOwned;
48
49const LOG_TARGET: &str = "jsonrpsee-server";
50
51/// Something that can be converted into a JSON-RPC method call response.
52///
53/// If the value couldn't be serialized/encoded, jsonrpsee will sent out an error
54/// to the client `response could not be serialized`.
55pub trait IntoResponse {
56	/// Output.
57	type Output: serde::Serialize + Clone;
58
59	/// Something that can be converted into a JSON-RPC method call response.
60	fn into_response(self) -> ResponsePayload<'static, Self::Output>;
61}
62
63impl<T, E: Into<ErrorObjectOwned>> IntoResponse for Result<T, E>
64where
65	T: serde::Serialize + Clone,
66{
67	type Output = T;
68
69	fn into_response(self) -> ResponsePayload<'static, Self::Output> {
70		match self {
71			Ok(val) => ResponsePayload::success(val),
72			Err(e) => ResponsePayload::error(e),
73		}
74	}
75}
76
77impl<T> IntoResponse for Option<T>
78where
79	T: serde::Serialize + Clone,
80{
81	type Output = Option<T>;
82
83	fn into_response(self) -> ResponsePayload<'static, Self::Output> {
84		ResponsePayload::success(self)
85	}
86}
87
88impl<T> IntoResponse for Vec<T>
89where
90	T: serde::Serialize + Clone,
91{
92	type Output = Vec<T>;
93
94	fn into_response(self) -> ResponsePayload<'static, Self::Output> {
95		ResponsePayload::success(self)
96	}
97}
98
99impl<T, const N: usize> IntoResponse for [T; N]
100where
101	[T; N]: serde::Serialize + Clone,
102{
103	type Output = [T; N];
104
105	fn into_response(self) -> ResponsePayload<'static, Self::Output> {
106		ResponsePayload::success(self)
107	}
108}
109
110impl<T> IntoResponse for jsonrpsee_types::ResponsePayload<'static, T>
111where
112	T: serde::Serialize + Clone,
113{
114	type Output = T;
115
116	fn into_response(self) -> ResponsePayload<'static, Self::Output> {
117		self.into()
118	}
119}
120
121impl<T> IntoResponse for ResponsePayload<'static, T>
122where
123	T: serde::Serialize + Clone,
124{
125	type Output = T;
126
127	fn into_response(self) -> ResponsePayload<'static, Self::Output> {
128		self
129	}
130}
131
132impl IntoResponse for ErrorObjectOwned {
133	type Output = ();
134
135	fn into_response(self) -> ResponsePayload<'static, Self::Output> {
136		ResponsePayload::error(self)
137	}
138}
139
140macro_rules! impl_into_response {
141	($($n:ty),*) => {
142		$(
143			impl IntoResponse for $n {
144				type Output = $n;
145
146				fn into_response(self) -> ResponsePayload<'static, Self::Output> {
147					ResponsePayload::success(self)
148				}
149			}
150		)+
151	}
152}
153
154impl_into_response!(
155	u8,
156	u16,
157	u32,
158	u64,
159	u128,
160	usize,
161	i8,
162	i16,
163	i32,
164	i64,
165	i128,
166	isize,
167	String,
168	&'static str,
169	bool,
170	serde_json::Value,
171	()
172);