MCPcopy Create free account
hub / github.com/ElementsProject/lightning / deserialize

Method deserialize

plugins/lsps-plugin/src/proto/jsonrpc.rs:201–238  ·  view source on GitHub ↗
(deserializer: D)

Source from the content-addressed store, hash-verified

199// Custom Deserialize from JSON-RPC 2.0 wire format
200impl<'de, R: DeserializeOwned> Deserialize<'de> for JsonRpcResponse<R> {
201 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
202 where
203 D: serde::Deserializer<'de>,
204 {
205 #[derive(Deserialize)]
206 struct RawResponse<R> {
207 jsonrpc: String,
208 result: Option<R>,
209 error: Option<RpcError>,
210 id: String,
211 }
212
213 let raw = RawResponse::deserialize(deserializer)?;
214
215 if raw.jsonrpc != "2.0" {
216 return Err(serde::de::Error::custom(format!(
217 "Invalid JSON-RPC version: {}",
218 raw.jsonrpc
219 )));
220 }
221
222 let body = match (raw.result, raw.error) {
223 (Some(result), None) => JsonRpcResponseBody::Success { result },
224 (None, Some(error)) => JsonRpcResponseBody::Error { error },
225 (Some(_), Some(_)) => {
226 return Err(serde::de::Error::custom(
227 "Response cannot have both result and error",
228 ));
229 }
230 (None, None) => {
231 return Err(serde::de::Error::custom(
232 "Response must have either result or error",
233 ));
234 }
235 };
236
237 Ok(JsonRpcResponse { id: raw.id, body })
238 }
239}
240
241#[derive(Clone, Debug, PartialEq)]

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected