clightningrpc_common/
types.rs1use serde::{Deserialize, Serialize};
6
7use crate::errors::{Error, RpcError};
8
9#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
10#[serde(untagged)]
11pub enum Id {
12 Str(String),
13 Int(u16),
14}
15
16impl From<&str> for Id {
17 fn from(value: &str) -> Self {
18 Id::Str(value.to_owned())
19 }
20}
21
22impl From<u64> for Id {
23 fn from(value: u64) -> Self {
24 Id::Str(format!("{value}"))
25 }
26}
27
28#[allow(clippy::derive_partial_eq_without_eq)]
29#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
30pub struct Request<T: Serialize> {
32 pub method: String,
34 pub params: T,
36 #[serde(skip_serializing_if = "Option::is_none")]
38 pub id: Option<Id>,
39 pub jsonrpc: String,
41}
42
43#[allow(clippy::derive_partial_eq_without_eq)]
44#[derive(Debug, Clone, PartialEq, Deserialize)]
45pub struct Response<T> {
47 pub result: Option<T>,
49 pub error: Option<RpcError>,
51 pub id: Id,
53 pub jsonrpc: Option<String>,
55}
56
57impl<T> Response<T> {
58 pub fn into_result(self) -> Result<T, Error> {
60 if let Some(e) = self.error {
61 return Err(Error::Rpc(e));
62 }
63
64 self.result.ok_or(Error::NoErrorOrResult)
65 }
66
67 pub fn is_none(&self) -> bool {
69 self.result.is_none()
70 }
71}