bright_lightning/lnd/models/
mod.rs

1mod hodl_invoice;
2mod info;
3mod invoice;
4mod invoice_request;
5mod lnd_payment;
6mod onchain;
7pub use hodl_invoice::*;
8pub use info::*;
9pub use invoice::*;
10pub use invoice_request::*;
11pub use lnd_payment::*;
12pub use onchain::*;
13
14use std::fmt::Display;
15
16use serde::{de::DeserializeOwned, Deserialize, Serialize};
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct LndResponse<T> {
20    pub result: T,
21}
22impl<T> LndResponse<T>
23where
24    T: Serialize + DeserializeOwned + Clone + 'static,
25{
26    pub fn inner(&self) -> T {
27        self.result.clone()
28    }
29}
30impl<T> TryFrom<&String> for LndResponse<T>
31where
32    T: Serialize + DeserializeOwned + Clone + 'static,
33{
34    type Error = anyhow::Error;
35    fn try_from(value: &String) -> Result<Self, Self::Error> {
36        Ok(serde_json::from_str(value)?)
37    }
38}
39impl<T> TryFrom<String> for LndResponse<T>
40where
41    T: Serialize + DeserializeOwned + Clone + 'static,
42{
43    type Error = anyhow::Error;
44    fn try_from(value: String) -> Result<Self, Self::Error> {
45        Ok(serde_json::from_str(&value)?)
46    }
47}
48impl<T> TryInto<String> for LndResponse<T>
49where
50    T: Serialize + DeserializeOwned + Clone + 'static,
51{
52    type Error = anyhow::Error;
53    fn try_into(self) -> Result<String, Self::Error> {
54        Ok(serde_json::to_string(&self)?)
55    }
56}
57impl<T> Display for LndResponse<T>
58where
59    T: Serialize + DeserializeOwned + Clone + 'static,
60{
61    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62        write!(f, "{}", serde_json::to_string_pretty(self).unwrap())
63    }
64}
65
66#[derive(Debug, Clone, Serialize, Deserialize)]
67pub struct LndErrorDetail {
68    code: i32,
69    message: String,
70}
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct LndError {
73    error: LndErrorDetail,
74}
75impl LndError {
76    #[must_use]
77    pub fn timeout() -> Self {
78        Self {
79            error: LndErrorDetail {
80                code: 0,
81                message: "Timeout".to_string(),
82            },
83        }
84    }
85}
86impl TryFrom<&String> for LndError {
87    type Error = anyhow::Error;
88    fn try_from(value: &String) -> Result<Self, Self::Error> {
89        Ok(serde_json::from_str(value)?)
90    }
91}
92impl TryFrom<String> for LndError {
93    type Error = anyhow::Error;
94    fn try_from(value: String) -> Result<Self, Self::Error> {
95        Ok(serde_json::from_str(&value)?)
96    }
97}
98impl Display for LndError {
99    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100        write!(f, "{}", serde_json::to_string_pretty(self).unwrap())
101    }
102}