Skip to content

Commit

Permalink
Merge #226: ffi(nostr): complete types module
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Dec 23, 2023
2 parents f195c75 + f9ee699 commit 6fc8928
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 50 deletions.
2 changes: 2 additions & 0 deletions bindings/nostr-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

//! Nostr FFI
#![allow(clippy::new_without_default)]

use std::sync::Arc;

use uniffi::Object;
Expand Down
22 changes: 11 additions & 11 deletions bindings/nostr-ffi/src/types/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,42 @@
use std::ops::Deref;
use std::sync::Arc;

use nostr::{Contact as ContactSdk, UncheckedUrl};
use nostr::UncheckedUrl;
use uniffi::Object;

use crate::PublicKey;

#[derive(Object)]
pub struct Contact {
contact: ContactSdk,
inner: nostr::Contact,
}

impl Deref for Contact {
type Target = ContactSdk;
type Target = nostr::Contact;
fn deref(&self) -> &Self::Target {
&self.contact
&self.inner
}
}

#[uniffi::export]
impl Contact {
#[uniffi::constructor]
pub fn new(pk: Arc<PublicKey>, relay_url: Option<String>, alias: Option<String>) -> Arc<Self> {
pub fn new(pk: Arc<PublicKey>, relay_url: Option<String>, alias: Option<String>) -> Self {
let relay_url = relay_url.map(|relay_url| UncheckedUrl::from(&relay_url));
Arc::new(Self {
contact: ContactSdk::new(**pk, relay_url, alias),
})
Self {
inner: nostr::Contact::new(**pk, relay_url, alias),
}
}

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

pub fn public_key(&self) -> Arc<PublicKey> {
Arc::new(self.contact.pk.into())
Arc::new(self.inner.pk.into())
}

pub fn relay_url(&self) -> Option<String> {
self.contact.relay_url.clone().map(|u| u.to_string())
self.inner.relay_url.clone().map(|u| u.to_string())
}
}
8 changes: 3 additions & 5 deletions bindings/nostr-ffi/src/types/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use std::sync::Arc;

use uniffi::Object;

#[derive(Object)]
Expand Down Expand Up @@ -32,10 +30,10 @@ impl From<&ImageDimensions> for nostr::ImageDimensions {
#[uniffi::export]
impl ImageDimensions {
#[uniffi::constructor]
pub fn new(width: u64, height: u64) -> Arc<Self> {
Arc::new(Self {
pub fn new(width: u64, height: u64) -> Self {
Self {
inner: nostr::ImageDimensions { width, height },
})
}
}

pub fn width(&self) -> u64 {
Expand Down
52 changes: 26 additions & 26 deletions bindings/nostr-ffi/src/types/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,120 +32,120 @@ impl From<nostr::Metadata> for Metadata {
#[uniffi::export]
impl Metadata {
#[uniffi::constructor]
pub fn new() -> Arc<Self> {
Arc::new(Self {
pub fn new() -> Self {
Self {
inner: nostr::Metadata::new(),
})
}
}

#[uniffi::constructor]
pub fn from_json(json: String) -> Result<Arc<Self>> {
Ok(Arc::new(Self {
pub fn from_json(json: String) -> Result<Self> {
Ok(Self {
inner: nostr::Metadata::from_json(json)?,
}))
})
}

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

pub fn set_name(self: Arc<Self>, name: String) -> Arc<Self> {
pub fn set_name(self: Arc<Self>, name: String) -> Self {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.name(name);
Arc::new(builder)
builder
}

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

pub fn set_display_name(self: Arc<Self>, display_name: String) -> Arc<Self> {
pub fn set_display_name(self: Arc<Self>, display_name: String) -> Self {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.display_name(display_name);
Arc::new(builder)
builder
}

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

pub fn set_about(self: Arc<Self>, about: String) -> Arc<Self> {
pub fn set_about(self: Arc<Self>, about: String) -> Self {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.about(about);
Arc::new(builder)
builder
}

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

pub fn set_website(self: Arc<Self>, website: String) -> Result<Arc<Self>> {
pub fn set_website(self: Arc<Self>, website: String) -> Result<Self> {
let website = Url::parse(&website)?;
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.website(website);
Ok(Arc::new(builder))
Ok(builder)
}

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

pub fn set_picture(self: Arc<Self>, picture: String) -> Result<Arc<Self>> {
pub fn set_picture(self: Arc<Self>, picture: String) -> Result<Self> {
let picture = Url::parse(&picture)?;
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.picture(picture);
Ok(Arc::new(builder))
Ok(builder)
}

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

pub fn set_banner(self: Arc<Self>, banner: String) -> Result<Arc<Self>> {
pub fn set_banner(self: Arc<Self>, banner: String) -> Result<Self> {
let banner = Url::parse(&banner)?;
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.banner(banner);
Ok(Arc::new(builder))
Ok(builder)
}

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

pub fn set_nip05(self: Arc<Self>, nip05: String) -> Arc<Self> {
pub fn set_nip05(self: Arc<Self>, nip05: String) -> Self {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.nip05(nip05);
Arc::new(builder)
builder
}

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

pub fn set_lud06(self: Arc<Self>, lud06: String) -> Arc<Self> {
pub fn set_lud06(self: Arc<Self>, lud06: String) -> Self {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.lud06(lud06);
Arc::new(builder)
builder
}

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

pub fn set_lud16(self: Arc<Self>, lud16: String) -> Arc<Self> {
pub fn set_lud16(self: Arc<Self>, lud16: String) -> Self {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.lud16(lud16);
Arc::new(builder)
builder
}

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

pub fn set_custom_field(self: Arc<Self>, key: String, value: String) -> Arc<Self> {
pub fn set_custom_field(self: Arc<Self>, key: String, value: String) -> Self {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.custom_field(key, value);
Arc::new(builder)
builder
}

pub fn get_custom_field(&self, key: String) -> Option<String> {
Expand Down
20 changes: 13 additions & 7 deletions bindings/nostr-ffi/src/types/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Distributed under the MIT software license

use std::ops::Deref;
use std::sync::Arc;

use uniffi::Object;

Expand All @@ -29,17 +28,24 @@ impl Deref for Timestamp {
impl Timestamp {
/// Get UNIX timestamp
#[uniffi::constructor]
pub fn now() -> Arc<Self> {
Arc::new(Self {
pub fn now() -> Self {
Self {
inner: nostr::Timestamp::now(),
})
}
}

#[uniffi::constructor]
pub fn from_secs(secs: u64) -> Arc<Self> {
Arc::new(Self {
pub fn tweaked() -> Self {
Self {
inner: nostr::Timestamp::tweaked(),
}
}

#[uniffi::constructor]
pub fn from_secs(secs: u64) -> Self {
Self {
inner: nostr::Timestamp::from(secs),
})
}
}

/// Get timestamp as [`u64`]
Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-ffi/src/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl RelayConnectionStats {
self.inner.uptime()
}

pub fn connected_at(&self) -> Arc<Timestamp> {
pub fn connected_at(&self) -> Timestamp {
let secs = self.inner.connected_at().as_u64();
Timestamp::from_secs(secs)
}
Expand Down

0 comments on commit 6fc8928

Please sign in to comment.