use std::sync::Arc;
use crate::{CurrencyCode, LwkError, WolletDescriptor};
#[derive(uniffi::Object, PartialEq, Eq, Debug, Clone)]
#[uniffi::export(Display, Eq)]
pub struct PosConfig {
pub(crate) inner: lwk_wollet::PosConfig,
}
impl std::fmt::Display for PosConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"PosConfig(descriptor: {}, currency: {})",
self.descriptor(),
self.currency()
)
}
}
impl From<lwk_wollet::PosConfig> for PosConfig {
fn from(inner: lwk_wollet::PosConfig) -> Self {
Self { inner }
}
}
impl From<PosConfig> for lwk_wollet::PosConfig {
fn from(value: PosConfig) -> Self {
value.inner
}
}
impl From<&PosConfig> for lwk_wollet::PosConfig {
fn from(value: &PosConfig) -> Self {
value.inner.clone()
}
}
#[uniffi::export]
impl PosConfig {
#[uniffi::constructor]
pub fn new(descriptor: &WolletDescriptor, currency: &CurrencyCode) -> Arc<PosConfig> {
let inner =
lwk_wollet::PosConfig::new(descriptor.as_ref().clone(), currency.as_ref().clone());
Arc::new(PosConfig { inner })
}
#[uniffi::constructor]
pub fn with_options(
descriptor: &WolletDescriptor,
currency: &CurrencyCode,
show_gear: Option<bool>,
show_description: Option<bool>,
) -> Arc<PosConfig> {
let mut inner =
lwk_wollet::PosConfig::new(descriptor.as_ref().clone(), currency.as_ref().clone());
if let Some(show_gear) = show_gear {
inner = inner.with_show_gear(show_gear);
}
if let Some(show_description) = show_description {
inner = inner.with_show_description(show_description);
}
Arc::new(PosConfig { inner })
}
#[uniffi::constructor]
pub fn decode(encoded: &str) -> Result<Arc<PosConfig>, LwkError> {
let inner = lwk_wollet::PosConfig::decode(encoded).ok_or_else(|| LwkError::Generic {
msg: "Invalid POS configuration encoding".to_string(),
})?;
Ok(Arc::new(PosConfig { inner }))
}
pub fn encode(&self) -> Result<String, LwkError> {
self.inner.encode().map_err(|e| LwkError::Generic {
msg: format!("Failed to encode POS configuration: {}", e),
})
}
pub fn descriptor(&self) -> Arc<WolletDescriptor> {
Arc::new(self.inner.descriptor.clone().into())
}
pub fn currency(&self) -> Arc<CurrencyCode> {
Arc::new(self.inner.currency.clone().into())
}
pub fn show_gear(&self) -> Option<bool> {
self.inner.show_gear
}
pub fn show_description(&self) -> Option<bool> {
self.inner.show_description
}
}