Skip to content

Commit

Permalink
Merge #309: add JSON string deserialization for nip15 StallData in no…
Browse files Browse the repository at this point in the history
…str-ffi
  • Loading branch information
yukibtc committed Mar 3, 2024
2 parents e4612c5 + c0e1483 commit 82e4237
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 26 deletions.
8 changes: 4 additions & 4 deletions bindings/nostr-ffi/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,10 @@ impl EventBuilder {
}

#[uniffi::constructor]
pub fn stall_data(data: StallData) -> Self {
Self {
inner: nostr::EventBuilder::stall_data(data.into()),
}
pub fn stall_data(data: Arc<StallData>) -> Arc<Self> {
Arc::new(Self {
inner: nostr::EventBuilder::stall_data(data.as_ref().deref().clone()),
})
}

#[uniffi::constructor]
Expand Down
100 changes: 78 additions & 22 deletions bindings/nostr-ffi/src/nips/nip15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,89 @@ use std::ops::Deref;
use std::sync::Arc;

use nostr::nips::nip15;
use nostr::JsonUtil;
use uniffi::{Object, Record};

use crate::error::Result;
use crate::helper::unwrap_or_clone_arc;

/// Payload for creating or updating stall
#[derive(Record)]
#[derive(Object)]
pub struct StallData {
/// UUID of the stall generated by merchant
pub id: String,
/// Stall name
pub name: String,
/// Stall description
pub description: Option<String>,
/// Currency used
pub currency: String,
/// Available shipping methods
pub shipping: Vec<Arc<ShippingMethod>>,
inner: nip15::StallData,
}

impl Deref for StallData {
type Target = nip15::StallData;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl From<nip15::StallData> for StallData {
fn from(inner: nip15::StallData) -> Self {
Self { inner }
}
}

impl From<StallData> for nip15::StallData {
fn from(value: StallData) -> Self {
#[uniffi::export]
impl StallData {
#[uniffi::constructor]
pub fn new(
id: String,
name: String,
description: Option<String>,
currency: String,
shipping: Vec<Arc<ShippingMethod>>,
) -> Self {
Self {
id: value.id,
name: value.name,
description: value.description,
currency: value.currency,
shipping: value
.shipping
.into_iter()
.map(|s| s.as_ref().deref().clone())
.collect(),
inner: nip15::StallData {
id,
name,
description,
currency,
shipping: shipping
.into_iter()
.map(|s| s.as_ref().deref().clone())
.collect(),
},
}
}

#[uniffi::constructor]
pub fn from_json(json: String) -> Result<Self> {
Ok(nip15::StallData::from_json(json)?.into())
}

pub fn id(&self) -> String {
self.inner.id.clone()
}

pub fn name(&self) -> String {
self.inner.name.clone()
}

pub fn description(&self) -> Option<String> {
self.inner.description.clone()
}

pub fn currency(&self) -> String {
self.inner.currency.clone()
}

pub fn shipping(&self) -> Vec<Arc<ShippingMethod>> {
self.inner
.shipping
.iter()
.cloned()
.map(|s| Arc::new(s.into()))
.collect()
}

pub fn as_json(&self) -> String {
self.inner.as_json()
}
}

/// Payload for creating or updating product
Expand Down Expand Up @@ -98,6 +148,12 @@ impl Deref for ShippingMethod {
}
}

impl From<nip15::ShippingMethod> for ShippingMethod {
fn from(value: nip15::ShippingMethod) -> Self {
Self { inner: value }
}
}

#[uniffi::export]
impl ShippingMethod {
/// Create a new shipping method
Expand Down

0 comments on commit 82e4237

Please sign in to comment.