use serde::{de::DeserializeOwned, Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LndResponse<T> {
pub result: T,
}
impl<T> LndResponse<T>
where
T: Serialize + DeserializeOwned + Clone + 'static,
{
pub fn inner(&self) -> T {
self.result.clone()
}
}
impl<T> std::str::FromStr for LndResponse<T>
where
T: Serialize + DeserializeOwned + Clone + 'static,
{
type Err = serde_json::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_str(s)
}
}
impl<T> TryInto<String> for LndResponse<T>
where
T: Serialize + DeserializeOwned + Clone + 'static,
{
type Error = serde_json::Error;
fn try_into(self) -> Result<String, Self::Error> {
serde_json::to_string(&self)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LndErrorDetail {
code: i32,
message: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LndError {
error: LndErrorDetail,
}
impl Default for LndError {
fn default() -> Self {
Self {
error: LndErrorDetail {
code: 0,
message: "Unknown".to_string(),
},
}
}
}
impl LndError {
#[must_use]
pub fn timeout() -> Self {
Self {
error: LndErrorDetail {
code: 0,
message: "Timeout".to_string(),
},
}
}
}
impl std::str::FromStr for LndError {
type Err = serde_json::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_str(s)
}
}
impl TryFrom<&LndError> for String {
type Error = serde_json::Error;
fn try_from(value: &LndError) -> Result<Self, Self::Error> {
serde_json::to_string(value)
}
}