1use serde::{Deserialize, Serialize};
4
5use crate::error::FfiError;
6
7#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, uniffi::Enum)]
9pub enum PaymentType {
10 Bolt11,
12 Bolt12,
14}
15
16impl From<cdk::invoice::PaymentType> for PaymentType {
17 fn from(payment_type: cdk::invoice::PaymentType) -> Self {
18 match payment_type {
19 cdk::invoice::PaymentType::Bolt11 => Self::Bolt11,
20 cdk::invoice::PaymentType::Bolt12 => Self::Bolt12,
21 }
22 }
23}
24
25impl From<PaymentType> for cdk::invoice::PaymentType {
26 fn from(payment_type: PaymentType) -> Self {
27 match payment_type {
28 PaymentType::Bolt11 => Self::Bolt11,
29 PaymentType::Bolt12 => Self::Bolt12,
30 }
31 }
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
36pub struct DecodedInvoice {
37 pub payment_type: PaymentType,
39 pub amount_msat: Option<u64>,
41 pub expiry: Option<u64>,
43 pub description: Option<String>,
45}
46
47impl From<cdk::invoice::DecodedInvoice> for DecodedInvoice {
48 fn from(decoded: cdk::invoice::DecodedInvoice) -> Self {
49 Self {
50 payment_type: decoded.payment_type.into(),
51 amount_msat: decoded.amount_msat,
52 expiry: decoded.expiry,
53 description: decoded.description,
54 }
55 }
56}
57
58impl From<DecodedInvoice> for cdk::invoice::DecodedInvoice {
59 fn from(decoded: DecodedInvoice) -> Self {
60 Self {
61 payment_type: decoded.payment_type.into(),
62 amount_msat: decoded.amount_msat,
63 expiry: decoded.expiry,
64 description: decoded.description,
65 }
66 }
67}
68
69#[uniffi::export]
93pub fn decode_invoice(invoice_str: String) -> Result<DecodedInvoice, FfiError> {
94 let decoded = cdk::invoice::decode_invoice(&invoice_str)?;
95 Ok(decoded.into())
96}