From fc14e5f6fb42f2bec0c6a70114f01d022da297f5 Mon Sep 17 00:00:00 2001 From: Edouard Paris Date: Sun, 10 Nov 2019 02:14:00 +0100 Subject: [PATCH] fix withdrawRequest tag --- src/lib.rs | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 46e5a50..81ef665 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,8 +2,8 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] pub enum Tag { - #[serde(rename = "withdrawalRequest")] - WithdrawalRequest, + #[serde(rename = "withdrawRequest")] + WithdrawRequest, } /// Withdrawal is a withdrawal resource. @@ -28,16 +28,38 @@ pub struct Withdrawal { pub tag: Tag, } -#[derive(Debug, Serialize, Deserialize)] -pub enum ResponseStatus { - ERROR, - OK, -} - /// Response is the response format returned by Service. /// Example: `{\"status\":\"ERROR\",\"reason\":\"error detail...\"}"` -#[derive(Debug, Serialize, Deserialize)] -pub struct Response { - pub status: ResponseStatus, - pub reason: Option, +#[derive(Debug, PartialEq, Serialize, Deserialize)] +#[serde(tag = "status")] +pub enum Response { + #[serde(rename = "ERROR")] + Error { reason: String }, + #[serde(rename = "OK")] + Ok, +} + +#[cfg(test)] +mod tests { + use crate::lnurl::Response; + + #[test] + fn response_from_str() { + let json = r#"{"status":"ERROR","reason":"error detail..."}"#; + let resp: Response = serde_json::from_str(json).unwrap(); + assert_eq!( + resp, + Response::Error { + reason: "error detail...".to_string() + } + ); + } + #[test] + fn response_to_str() { + let resp = Response::Error { + reason: "error detail...".to_string(), + }; + let json = serde_json::to_string(&resp).unwrap(); + assert_eq!(json, r#"{"status":"ERROR","reason":"error detail..."}"#); + } }