Skip to content

Commit

Permalink
js(nostr): some adjustment to EventBuilder and related things
Browse files Browse the repository at this point in the history
- add missing `#[wasm_bindgen(constructor)]`
- add missing `new` constructor
- fix clippy
  • Loading branch information
rustedmoon committed Mar 13, 2024
1 parent d07c06e commit 89179d8
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 48 deletions.
4 changes: 2 additions & 2 deletions bindings/nostr-js/src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl JsEventBuilder {
badge_id,
name,
description,
image.map(|url| UncheckedUrl::from(url)),
image.map(UncheckedUrl::from),
image_dimensions.map(|i| i.into()),
thumbnails.into_iter().map(|t| t.into()).collect(),
),
Expand Down Expand Up @@ -385,7 +385,7 @@ impl JsEventBuilder {
}

#[wasm_bindgen(js_name = stallData)]
pub fn stall_data(data: JsStallData) -> Self {
pub fn stall_data(data: &JsStallData) -> Self {
Self {
builder: EventBuilder::stall_data(data.deref().clone()),
}
Expand Down
13 changes: 11 additions & 2 deletions bindings/nostr-js/src/event/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ impl From<JsHttpMethod> for HttpMethod {

#[wasm_bindgen(js_name = Thumbnails)]
pub struct JsThumbnails {
url: String,
dimensions: Option<JsImageDimensions>,
#[wasm_bindgen(getter_with_clone)]
pub url: String,
pub dimensions: Option<JsImageDimensions>,
}

impl From<JsThumbnails> for (UncheckedUrl, Option<ImageDimensions>) {
Expand All @@ -52,6 +53,14 @@ impl From<JsThumbnails> for (UncheckedUrl, Option<ImageDimensions>) {
}
}

#[wasm_bindgen(js_class = Thumbnails)]
impl JsThumbnails {
#[wasm_bindgen(constructor)]
pub fn new(url: String, dimensions: Option<JsImageDimensions>) -> Self {
Self { url, dimensions }
}
}

#[derive(Clone, Copy)]
#[wasm_bindgen(js_name = ImageDimensions)]
pub struct JsImageDimensions {
Expand Down
1 change: 1 addition & 0 deletions bindings/nostr-js/src/key/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use wasm_bindgen::prelude::*;

use crate::error::{into_err, Result};

#[derive(Clone, Copy)]
#[wasm_bindgen(js_name = PublicKey)]
pub struct JsPublicKey {
pub(crate) inner: PublicKey,
Expand Down
4 changes: 4 additions & 0 deletions bindings/nostr-js/src/nips/nip15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ impl Deref for JsShippingMethod {

#[wasm_bindgen(js_class = ShippingMethod)]
impl JsShippingMethod {
#[wasm_bindgen(constructor)]
pub fn new(id: &str, cost: f64) -> Self {
ShippingMethod::new(id, cost).into()
}

#[wasm_bindgen(js_name = getShuppingCost)]
pub fn get_shipping_cost(&self) -> JsShippingCost {
ShippingMethod::get_shipping_cost(self.deref()).into()
}
Expand Down Expand Up @@ -108,6 +110,7 @@ impl Deref for JsStallData {

#[wasm_bindgen(js_class = StallData)]
impl JsStallData {
#[wasm_bindgen(constructor)]
pub fn new(id: &str, name: &str, currency: &str) -> Self {
StallData::new(id, name, currency).into()
}
Expand Down Expand Up @@ -162,6 +165,7 @@ impl From<JsProductData> for ProductData {

#[wasm_bindgen(js_class = ProductData)]
impl JsProductData {
#[wasm_bindgen(constructor)]
pub fn new(id: &str, stall_id: &str, name: &str, currency: &str) -> Self {
ProductData::new(id, stall_id, name, currency).into()
}
Expand Down
57 changes: 30 additions & 27 deletions bindings/nostr-js/src/nips/nip53.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ use crate::event::tag::JsImageDimensions;
use crate::key::JsPublicKey;

#[wasm_bindgen(js_name = Image)]
pub struct Image {
url: String,
dimensions: Option<JsImageDimensions>,
pub struct JsImage {
#[wasm_bindgen(getter_with_clone)]
pub url: String,
pub dimensions: Option<JsImageDimensions>,
}

impl From<(UncheckedUrl, Option<ImageDimensions>)> for Image {
impl From<(UncheckedUrl, Option<ImageDimensions>)> for JsImage {
fn from(value: (UncheckedUrl, Option<ImageDimensions>)) -> Self {
Self {
url: value.0.to_string(),
Expand All @@ -25,23 +26,23 @@ impl From<(UncheckedUrl, Option<ImageDimensions>)> for Image {
}
}

impl Image {
pub fn url(&self) -> String {
self.url.to_string()
}

pub fn dimensions(&self) -> Option<JsImageDimensions> {
self.dimensions.clone().map(|d| d.into())
#[wasm_bindgen(js_class = Image)]
impl JsImage {
#[wasm_bindgen(constructor)]
pub fn new(url: String, dimensions: Option<JsImageDimensions>) -> Self {
Self { url, dimensions }
}
}

#[wasm_bindgen(js_name = User)]
pub struct User {
public_key: JsPublicKey,
url: Option<String>,
pub struct JsUser {
#[wasm_bindgen(js_name = publicKey)]
pub public_key: JsPublicKey,
#[wasm_bindgen(getter_with_clone)]
pub url: Option<String>,
}

impl From<(PublicKey, Option<UncheckedUrl>)> for User {
impl From<(PublicKey, Option<UncheckedUrl>)> for JsUser {
fn from(value: (PublicKey, Option<UncheckedUrl>)) -> Self {
Self {
public_key: value.0.into(),
Expand All @@ -50,13 +51,11 @@ impl From<(PublicKey, Option<UncheckedUrl>)> for User {
}
}

impl User {
pub fn public_key(&self) -> JsPublicKey {
self.public_key.clone().into()
}

pub fn url(&self) -> Option<String> {
self.url.clone()
#[wasm_bindgen(js_class = User)]
impl JsUser {
#[wasm_bindgen(constructor)]
pub fn new(public_key: JsPublicKey, url: Option<String>) -> Self {
Self { public_key, url }
}
}

Expand All @@ -81,24 +80,28 @@ impl Deref for JsLiveEventStatus {

#[wasm_bindgen(js_class = LiveEventStatus)]
impl JsLiveEventStatus {
#[wasm_bindgen(constructor)]
pub fn planned() -> Self {
Self {
inner: LiveEventStatus::Planned,
}
}

#[wasm_bindgen(constructor)]
pub fn live() -> Self {
Self {
inner: LiveEventStatus::Live,
}
}

#[wasm_bindgen(constructor)]
pub fn ended() -> Self {
Self {
inner: LiveEventStatus::Ended,
}
}

#[wasm_bindgen(constructor)]
pub fn custom(string: String) -> Self {
Self {
inner: LiveEventStatus::Custom(string),
Expand Down Expand Up @@ -178,7 +181,7 @@ impl JsLiveEvent {
}

#[wasm_bindgen(getter)]
pub fn image(&self) -> Option<Image> {
pub fn image(&self) -> Option<JsImage> {
self.inner.image.clone().map(|i| i.into())
}

Expand All @@ -199,12 +202,12 @@ impl JsLiveEvent {

#[wasm_bindgen(getter)]
pub fn starts(&self) -> Option<f64> {
self.inner.starts.clone().map(|t| t.as_i64() as f64)
self.inner.starts.map(|t| t.as_i64() as f64)
}

#[wasm_bindgen(getter)]
pub fn ends(&self) -> Option<f64> {
self.inner.ends.clone().map(|t| t.as_i64() as f64)
self.inner.ends.map(|t| t.as_i64() as f64)
}

#[wasm_bindgen(getter)]
Expand Down Expand Up @@ -238,7 +241,7 @@ impl JsLiveEvent {
}

#[wasm_bindgen(getter)]
pub fn speakers(&self) -> Vec<User> {
pub fn speakers(&self) -> Vec<JsUser> {
self.inner
.speakers
.clone()
Expand All @@ -248,7 +251,7 @@ impl JsLiveEvent {
}

#[wasm_bindgen(getter)]
pub fn participants(&self) -> Vec<User> {
pub fn participants(&self) -> Vec<JsUser> {
self.inner
.participants
.clone()
Expand Down
3 changes: 2 additions & 1 deletion bindings/nostr-js/src/nips/nip57.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl Deref for JsZapRequestData {

#[wasm_bindgen(js_class = ZapRequestData)]
impl JsZapRequestData {
#[wasm_bindgen(constructor)]
pub fn new(
public_key: &JsPublicKey,
relays: Vec<String>,
Expand All @@ -79,7 +80,7 @@ impl JsZapRequestData {
message,
amount: amount.map(|n| n as u64),
lnurl,
event_id: event_id.map(|e| e.deref().clone()),
event_id: event_id.map(|e| *e.deref()),
event_coordinate: event_coordinate.map(|e| e.deref().clone()),
},
}
Expand Down
16 changes: 4 additions & 12 deletions bindings/nostr-js/src/nips/nip65.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use crate::event::JsEvent;

#[wasm_bindgen(js_name = RelayListItem)]
pub struct JsRelayListItem {
url: String,
metadata: Option<JsRelayMetadata>,
#[wasm_bindgen(getter_with_clone)]
pub url: String,
pub metadata: Option<JsRelayMetadata>,
}

impl From<JsRelayListItem> for (UncheckedUrl, Option<RelayMetadata>) {
Expand All @@ -27,19 +28,10 @@ impl From<JsRelayListItem> for (UncheckedUrl, Option<RelayMetadata>) {

#[wasm_bindgen(js_class = RelayListItem)]
impl JsRelayListItem {
#[wasm_bindgen(constructor)]
pub fn new(url: String, metadata: Option<JsRelayMetadata>) -> Self {
Self { url, metadata }
}

#[wasm_bindgen(getter)]
pub fn url(&self) -> String {
self.url.clone()
}

#[wasm_bindgen(getter)]
pub fn metadata(&self) -> Option<JsRelayMetadata> {
self.metadata
}
}

#[wasm_bindgen(js_name = extractRelayList)]
Expand Down
13 changes: 9 additions & 4 deletions bindings/nostr-js/src/nips/nip94.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ use crate::event::tag::JsImageDimensions;

#[wasm_bindgen(js_name = Aes256Gcm)]
pub struct JsAes256Gcm {
key: String,
iv: String,
#[wasm_bindgen(getter_with_clone)]
pub key: String,
#[wasm_bindgen(getter_with_clone)]
pub iv: String,
}

impl From<JsAes256Gcm> for (String, String) {
Expand All @@ -33,7 +35,9 @@ impl From<(String, String)> for JsAes256Gcm {
}
}

#[wasm_bindgen(js_class = Aes256Gcm)]
impl JsAes256Gcm {
#[wasm_bindgen(constructor)]
pub fn new(key: String, iv: String) -> Self {
Self { key, iv }
}
Expand All @@ -60,12 +64,13 @@ impl Deref for JsFileMetadata {

#[wasm_bindgen(js_class = FileMetadata)]
impl JsFileMetadata {
#[wasm_bindgen(constructor)]
pub fn new(url: &str, mime_type: String, hash: &str) -> Result<JsFileMetadata> {
Ok(Self {
inner: FileMetadata::new(
Url::from_str(&url).map_err(into_err)?,
Url::from_str(url).map_err(into_err)?,
mime_type,
Sha256Hash::from_str(&hash).map_err(into_err)?,
Sha256Hash::from_str(hash).map_err(into_err)?,
),
})
}
Expand Down
1 change: 1 addition & 0 deletions bindings/nostr-js/src/nips/nip98.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl From<JsHttpData> for HttpData {

#[wasm_bindgen(js_class = HttpData)]
impl JsHttpData {
#[wasm_bindgen(constructor)]
pub fn new(url: &str, method: JsHttpMethod) -> Self {
HttpData::new(UncheckedUrl::from(url), method.into()).into()
}
Expand Down

0 comments on commit 89179d8

Please sign in to comment.