From 5a8566a78fbf188f0196d4f671a149ae2c6fe5c5 Mon Sep 17 00:00:00 2001 From: Edouard Paris Date: Sun, 9 Aug 2020 17:14:03 +0200 Subject: [PATCH] add event to lnurl::Response::Ok --- README.md | 2 +- src/lib.rs | 69 +++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index fd6bc26..3ba79b3 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ _Readings about **lnurl**_ -* [The spec](https://github.com/btcontract/lnurl-rfc/blob/master/spec.md) – **lnurl** spec, by [Anton Kumaigorodski](https://twitter.com/akumaigorodski). +* [The spec](https://github.com/btcontract/lnurl-rfc) – **lnurl** spec, by [Anton Kumaigorodski](https://twitter.com/akumaigorodski). * [An introduction to lnurl](https://telegra.ph/lnurl-a-protocol-for-seamless-interaction-between-services-and-Lightning-wallets-08-19) – An article introducing the various types of **lnurl**'s, by [fiatjaf](https://twitter.com/fiatjaf). ## Progress diff --git a/src/lib.rs b/src/lib.rs index 81ef665..9d388cd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,18 @@ use serde::{Deserialize, Serialize}; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub enum Event { + #[serde(rename = "REGISTERED")] + Registered, + #[serde(rename = "LOGGEDIN")] + LoggedIn, + #[serde(rename = "LINKED")] + Linked, + #[serde(rename = "AUTHED")] + Authed, +} + +#[derive(Debug, PartialEq, Serialize, Deserialize)] pub enum Tag { #[serde(rename = "withdrawRequest")] WithdrawRequest, @@ -36,30 +48,55 @@ pub enum Response { #[serde(rename = "ERROR")] Error { reason: String }, #[serde(rename = "OK")] - Ok, + Ok { event: Option }, } #[cfg(test)] mod tests { - use crate::lnurl::Response; + use super::*; #[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() - } - ); + let tests = vec![ + ( + r#"{"status":"ERROR","reason":"error detail..."}"#, + Response::Error { + reason: "error detail...".to_string(), + }, + ), + ( + r#"{"status":"OK","event":"LOGGEDIN"}"#, + Response::Ok { + event: Some(Event::LoggedIn), + }, + ), + ]; + + for test in tests { + let resp: Response = serde_json::from_str(test.0).unwrap(); + assert_eq!(resp, test.1); + } } #[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..."}"#); + let tests = vec![ + ( + r#"{"status":"ERROR","reason":"error detail..."}"#, + Response::Error { + reason: "error detail...".to_string(), + }, + ), + ( + r#"{"status":"OK","event":"LOGGEDIN"}"#, + Response::Ok { + event: Some(Event::LoggedIn), + }, + ), + ]; + + for test in tests { + let json = serde_json::to_string(&test.1).unwrap(); + assert_eq!(json, test.0); + } } }