1use std::sync::Arc;
2
3use crate::{CurrencyCode, LwkError, WolletDescriptor};
4
5#[derive(uniffi::Object, PartialEq, Eq, Debug, Clone)]
7#[uniffi::export(Display, Eq)]
8pub struct PosConfig {
9 pub(crate) inner: lwk_wollet::PosConfig,
10}
11
12impl std::fmt::Display for PosConfig {
13 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14 write!(
15 f,
16 "PosConfig(descriptor: {}, currency: {})",
17 self.descriptor(),
18 self.currency()
19 )
20 }
21}
22
23impl From<lwk_wollet::PosConfig> for PosConfig {
24 fn from(inner: lwk_wollet::PosConfig) -> Self {
25 Self { inner }
26 }
27}
28
29impl From<PosConfig> for lwk_wollet::PosConfig {
30 fn from(value: PosConfig) -> Self {
31 value.inner
32 }
33}
34
35impl From<&PosConfig> for lwk_wollet::PosConfig {
36 fn from(value: &PosConfig) -> Self {
37 value.inner.clone()
38 }
39}
40
41#[uniffi::export]
42impl PosConfig {
43 #[uniffi::constructor]
45 pub fn new(descriptor: &WolletDescriptor, currency: &CurrencyCode) -> Arc<PosConfig> {
46 let inner =
47 lwk_wollet::PosConfig::new(descriptor.as_ref().clone(), currency.as_ref().clone());
48 Arc::new(PosConfig { inner })
49 }
50
51 #[uniffi::constructor]
53 pub fn with_options(
54 descriptor: &WolletDescriptor,
55 currency: &CurrencyCode,
56 show_gear: Option<bool>,
57 show_description: Option<bool>,
58 ) -> Arc<PosConfig> {
59 let mut inner =
60 lwk_wollet::PosConfig::new(descriptor.as_ref().clone(), currency.as_ref().clone());
61
62 if let Some(show_gear) = show_gear {
63 inner = inner.with_show_gear(show_gear);
64 }
65 if let Some(show_description) = show_description {
66 inner = inner.with_show_description(show_description);
67 }
68
69 Arc::new(PosConfig { inner })
70 }
71
72 #[uniffi::constructor]
74 pub fn decode(encoded: &str) -> Result<Arc<PosConfig>, LwkError> {
75 let inner = lwk_wollet::PosConfig::decode(encoded).ok_or_else(|| LwkError::Generic {
76 msg: "Invalid POS configuration encoding".to_string(),
77 })?;
78 Ok(Arc::new(PosConfig { inner }))
79 }
80
81 pub fn encode(&self) -> Result<String, LwkError> {
83 self.inner.encode().map_err(|e| LwkError::Generic {
84 msg: format!("Failed to encode POS configuration: {}", e),
85 })
86 }
87
88 pub fn descriptor(&self) -> Arc<WolletDescriptor> {
90 Arc::new(self.inner.descriptor.clone().into())
91 }
92
93 pub fn currency(&self) -> Arc<CurrencyCode> {
95 Arc::new(self.inner.currency.clone().into())
96 }
97
98 pub fn show_gear(&self) -> Option<bool> {
100 self.inner.show_gear
101 }
102
103 pub fn show_description(&self) -> Option<bool> {
105 self.inner.show_description
106 }
107}