Skip to main content

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.
34mod method_response;
35/// JSON-RPC "modules" group sets of methods that belong together and handles method/subscription
36/// registration.
37mod rpc_module;
38/// Subscription related types.
39mod subscription;
40
41pub use error::*;
42pub use helpers::*;
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 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
118    }
119}
120
121impl IntoResponse for ErrorObjectOwned {
122    type Output = ();
123
124    fn into_response(self) -> ResponsePayload<'static, Self::Output> {
125        ResponsePayload::error(self)
126    }
127}
128
129macro_rules! impl_into_response {
130    ($($n:ty),*) => {
131        $(
132            impl IntoResponse for $n {
133                type Output = $n;
134
135                fn into_response(self) -> ResponsePayload<'static, Self::Output> {
136                    ResponsePayload::success(self)
137                }
138            }
139        )+
140    }
141}
142
143impl_into_response!(
144    u8,
145    u16,
146    u32,
147    u64,
148    u128,
149    usize,
150    i8,
151    i16,
152    i32,
153    i64,
154    i128,
155    isize,
156    String,
157    &'static str,
158    bool,
159    serde_json::Value,
160    ()
161);