use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use crate::nuts::nut00::KnownMethod;
use crate::nuts::nut04::{MintQuoteCustomRequest, MintQuoteCustomResponse};
use crate::nuts::nut23::{MintQuoteBolt11Request, MintQuoteBolt11Response};
use crate::nuts::nut25::{MintQuoteBolt12Request, MintQuoteBolt12Response};
use crate::{Amount, CurrencyUnit, PaymentMethod, PublicKey};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MintQuoteRequest {
Bolt11(MintQuoteBolt11Request),
Bolt12(MintQuoteBolt12Request),
Custom((PaymentMethod, MintQuoteCustomRequest)),
}
impl From<MintQuoteBolt11Request> for MintQuoteRequest {
fn from(request: MintQuoteBolt11Request) -> Self {
MintQuoteRequest::Bolt11(request)
}
}
impl From<MintQuoteBolt12Request> for MintQuoteRequest {
fn from(request: MintQuoteBolt12Request) -> Self {
MintQuoteRequest::Bolt12(request)
}
}
impl MintQuoteRequest {
pub fn method(&self) -> PaymentMethod {
match self {
Self::Bolt11(_) => PaymentMethod::Known(KnownMethod::Bolt11),
Self::Bolt12(_) => PaymentMethod::Known(KnownMethod::Bolt12),
Self::Custom((method, _)) => method.clone(),
}
}
pub fn amount(&self) -> Option<Amount> {
match self {
Self::Bolt11(request) => Some(request.amount),
Self::Bolt12(request) => request.amount,
Self::Custom((_, request)) => Some(request.amount),
}
}
pub fn unit(&self) -> CurrencyUnit {
match self {
Self::Bolt11(request) => request.unit.clone(),
Self::Bolt12(request) => request.unit.clone(),
Self::Custom((_, request)) => request.unit.clone(),
}
}
pub fn payment_method(&self) -> PaymentMethod {
self.method()
}
pub fn pubkey(&self) -> Option<PublicKey> {
match self {
Self::Bolt11(request) => request.pubkey,
Self::Bolt12(request) => Some(request.pubkey),
Self::Custom((_, request)) => request.pubkey,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(bound = "Q: Serialize + DeserializeOwned")]
pub enum MintQuoteResponse<Q> {
Bolt11(MintQuoteBolt11Response<Q>),
Bolt12(MintQuoteBolt12Response<Q>),
Custom((PaymentMethod, MintQuoteCustomResponse<Q>)),
}
impl<Q> MintQuoteResponse<Q> {
pub fn method(&self) -> PaymentMethod {
match self {
Self::Bolt11(_) => PaymentMethod::Known(KnownMethod::Bolt11),
Self::Bolt12(_) => PaymentMethod::Known(KnownMethod::Bolt12),
Self::Custom((method, _)) => method.clone(),
}
}
pub fn quote(&self) -> &Q {
match self {
Self::Bolt11(r) => &r.quote,
Self::Bolt12(r) => &r.quote,
Self::Custom((_, r)) => &r.quote,
}
}
pub fn request(&self) -> &str {
match self {
Self::Bolt11(r) => &r.request,
Self::Bolt12(r) => &r.request,
Self::Custom((_, r)) => &r.request,
}
}
pub fn state(&self) -> crate::nuts::nut23::QuoteState {
match self {
Self::Bolt11(r) => r.state,
Self::Bolt12(r) => {
if r.amount_issued > Amount::ZERO {
crate::nuts::nut23::QuoteState::Issued
} else if r.amount_paid >= r.amount.unwrap_or(Amount::ZERO) {
crate::nuts::nut23::QuoteState::Paid
} else {
crate::nuts::nut23::QuoteState::Unpaid
}
}
Self::Custom((_, r)) => r.state,
}
}
pub fn expiry(&self) -> Option<u64> {
match self {
Self::Bolt11(r) => r.expiry,
Self::Bolt12(r) => r.expiry,
Self::Custom((_, r)) => r.expiry,
}
}
}