use serde::{Deserialize, Serialize};
use crate::error::FfiError;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, uniffi::Enum)]
pub enum PaymentType {
Bolt11,
Bolt12,
}
impl From<cdk::invoice::PaymentType> for PaymentType {
fn from(payment_type: cdk::invoice::PaymentType) -> Self {
match payment_type {
cdk::invoice::PaymentType::Bolt11 => Self::Bolt11,
cdk::invoice::PaymentType::Bolt12 => Self::Bolt12,
}
}
}
impl From<PaymentType> for cdk::invoice::PaymentType {
fn from(payment_type: PaymentType) -> Self {
match payment_type {
PaymentType::Bolt11 => Self::Bolt11,
PaymentType::Bolt12 => Self::Bolt12,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct DecodedInvoice {
pub payment_type: PaymentType,
pub amount_msat: Option<u64>,
pub expiry: Option<u64>,
pub description: Option<String>,
}
impl From<cdk::invoice::DecodedInvoice> for DecodedInvoice {
fn from(decoded: cdk::invoice::DecodedInvoice) -> Self {
Self {
payment_type: decoded.payment_type.into(),
amount_msat: decoded.amount_msat,
expiry: decoded.expiry,
description: decoded.description,
}
}
}
impl From<DecodedInvoice> for cdk::invoice::DecodedInvoice {
fn from(decoded: DecodedInvoice) -> Self {
Self {
payment_type: decoded.payment_type.into(),
amount_msat: decoded.amount_msat,
expiry: decoded.expiry,
description: decoded.description,
}
}
}
#[uniffi::export]
pub fn decode_invoice(invoice_str: String) -> Result<DecodedInvoice, FfiError> {
let decoded = cdk::invoice::decode_invoice(&invoice_str)?;
Ok(decoded.into())
}