Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add StallDataRecord in addition to StallData #335

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions bindings/nostr-ffi/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::{Event, EventId};
use crate::error::Result;
use crate::helper::unwrap_or_clone_arc;
use crate::key::Keys;
use crate::nips::nip15::{ProductData, StallData};
use crate::nips::nip15::{ProductData, StallDataRecord};
use crate::nips::nip53::LiveEvent;
use crate::nips::nip57::ZapRequestData;
use crate::nips::nip90::DataVendingMachineStatus;
Expand Down Expand Up @@ -464,10 +464,10 @@ impl EventBuilder {
}

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

#[uniffi::constructor]
Expand Down
81 changes: 81 additions & 0 deletions bindings/nostr-ffi/src/nips/nip15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,44 @@ use crate::error::Result;
use crate::helper::unwrap_or_clone_arc;

/// Payload for creating or updating stall
#[derive(Record)]
pub struct StallDataRecord {
/// 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<ShippingMethodRecord>,
}

impl From<StallDataRecord> for nip15::StallData {
fn from(value: StallDataRecord) -> Self {
Self {
id: value.id,
name: value.name,
description: value.description,
currency: value.currency,
shipping: value.shipping.into_iter().map(|s| s.into()).collect(),
}
}
}

impl From<nip15::StallData> for StallDataRecord {
fn from(value: nip15::StallData) -> Self {
Self {
id: value.id,
name: value.name,
description: value.description,
currency: value.currency,
shipping: value.shipping.into_iter().map(|s| s.into()).collect(),
}
}
}

#[derive(Object)]
pub struct StallData {
inner: nip15::StallData,
Expand Down Expand Up @@ -55,6 +93,11 @@ impl StallData {
}
}

#[uniffi::constructor]
pub fn from_record(r: StallDataRecord) -> Self {
Self { inner: r.into() }
}

#[uniffi::constructor]
pub fn from_json(json: String) -> Result<Self> {
Ok(nip15::StallData::from_json(json)?.into())
Expand Down Expand Up @@ -85,6 +128,10 @@ impl StallData {
.collect()
}

pub fn as_record(&self) -> StallDataRecord {
self.inner.clone().into()
}

pub fn as_json(&self) -> String {
self.inner.as_json()
}
Expand Down Expand Up @@ -135,6 +182,40 @@ impl From<ProductData> for nip15::ProductData {
}
}

#[derive(Record)]
pub struct ShippingMethodRecord {
/// Shipping method unique id by merchant
pub id: String,
/// Shipping method name
pub name: Option<String>,
/// Shipping method cost (currency is the same as the stall)
pub cost: f64,
/// Covered regions
pub regions: Vec<String>,
}

impl From<nip15::ShippingMethod> for ShippingMethodRecord {
fn from(value: nip15::ShippingMethod) -> Self {
Self {
id: value.id,
name: value.name,
cost: value.cost,
regions: value.regions,
}
}
}

impl From<ShippingMethodRecord> for nip15::ShippingMethod {
fn from(value: ShippingMethodRecord) -> Self {
Self {
id: value.id,
name: value.name,
cost: value.cost,
regions: value.regions,
}
}
}

#[derive(Clone, Object)]
pub struct ShippingMethod {
inner: nip15::ShippingMethod,
Expand Down
Loading