jsonrpsee_core/server/
mod.rs1mod error;
31pub mod helpers;
33mod method_response;
35mod rpc_module;
38mod 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
51pub trait IntoResponse {
56 type Output: serde::Serialize + Clone;
58
59 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);