From e4bf38fdb8dbfc876d1513fc836a63e8beb9953b Mon Sep 17 00:00:00 2001 From: benthecarman Date: Sun, 29 Oct 2023 16:36:42 -0500 Subject: [PATCH] Impl Display and FromStr for Method --- crates/nostr/src/nips/nip47.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/crates/nostr/src/nips/nip47.rs b/crates/nostr/src/nips/nip47.rs index 67a4bca02..555fd4a87 100644 --- a/crates/nostr/src/nips/nip47.rs +++ b/crates/nostr/src/nips/nip47.rs @@ -113,6 +113,36 @@ pub enum ErrorCode { Other, } +impl fmt::Display for Method { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Method::PayInvoice => write!(f, "pay_invoice"), + Method::PayKeysend => write!(f, "pay_keysend"), + Method::MakeInvoice => write!(f, "make_invoice"), + Method::LookupInvoice => write!(f, "lookup_invoice"), + Method::ListInvoices => write!(f, "list_invoices"), + Method::ListPayments => write!(f, "list_payments"), + Method::GetBalance => write!(f, "get_balance"), + } + } +} + +impl FromStr for Method { + type Err = Error; + fn from_str(s: &str) -> Result { + match s { + "pay_invoice" => Ok(Method::PayInvoice), + "pay_keysend" => Ok(Method::PayKeysend), + "make_invoice" => Ok(Method::MakeInvoice), + "lookup_invoice" => Ok(Method::LookupInvoice), + "list_invoices" => Ok(Method::ListInvoices), + "list_payments" => Ok(Method::ListPayments), + "get_balance" => Ok(Method::GetBalance), + _ => Err(Error::InvalidURI), + } + } +} + /// NIP47 Error message #[derive(Debug, Clone, Serialize, Deserialize)] pub struct NIP47Error {