bright_ln_models/
http.rs

1
2use serde::{de::DeserializeOwned, Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct LndResponse<T> {
6    pub result: T,
7}
8impl<T> LndResponse<T>
9where
10    T: Serialize + DeserializeOwned + Clone + 'static,
11{
12    pub fn inner(&self) -> T {
13        self.result.clone()
14    }
15}
16impl<T> std::str::FromStr for LndResponse<T>
17where
18    T: Serialize + DeserializeOwned + Clone + 'static,
19{
20    type Err = serde_json::Error;
21    fn from_str(s: &str) -> Result<Self, Self::Err> {
22        serde_json::from_str(s)
23    }
24}
25impl<T> TryInto<String> for LndResponse<T>
26where
27    T: Serialize + DeserializeOwned + Clone + 'static,
28{
29    type Error = serde_json::Error;
30    fn try_into(self) -> Result<String, Self::Error> {
31        serde_json::to_string(&self)
32    }
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct LndErrorDetail {
37    code: i32,
38    message: String,
39}
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct LndError {
42    error: LndErrorDetail,
43}
44impl Default for LndError {
45    fn default() -> Self {
46        Self {
47            error: LndErrorDetail {
48                code: 0,
49                message: "Unknown".to_string(),
50            },
51        }
52    }
53}
54impl LndError {
55    #[must_use]
56    pub fn timeout() -> Self {
57        Self {
58            error: LndErrorDetail {
59                code: 0,
60                message: "Timeout".to_string(),
61            },
62        }
63    }
64}
65impl std::str::FromStr for LndError {
66    type Err = serde_json::Error;
67    fn from_str(s: &str) -> Result<Self, Self::Err> {
68        serde_json::from_str(s)
69    }
70}
71
72impl TryFrom<&LndError> for String {
73    type Error = serde_json::Error;
74    fn try_from(value: &LndError) -> Result<Self, Self::Error> {
75        serde_json::to_string(value)
76    }
77}
78