Skip to content

Commit

Permalink
ffi: convert Contact to a Record
Browse files Browse the repository at this point in the history
This removes ~80kb from the compiled library

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Jan 8, 2025
1 parent f3bbd34 commit 809d75c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* pool: update `Error::WebSocket` variant inner type ([Yuki Kishimoto])
* lmdb: use `EventBorrow` instead of `DatabaseEvent` ([Yuki Kishimoto])
* ndb: refactor note-to-event conversion ([Yuki Kishimoto])
* ffi: convert `Contact` to a `Record` ([Yuki Kishimoto])
* bindings: build `std` library optimized for size ([Yuki Kishimoto])

### Added
Expand Down
11 changes: 7 additions & 4 deletions bindings/nostr-sdk-ffi/src/protocol/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,15 @@ impl EventBuilder {
///
/// <https://github.com/nostr-protocol/nips/blob/master/02.md>
#[uniffi::constructor]
pub fn contact_list(list: &[Arc<Contact>]) -> Self {
let list: Vec<ContactSdk> = list.iter().map(|c| c.as_ref().deref().clone()).collect();
pub fn contact_list(contacts: Vec<Contact>) -> Result<Self> {
let mut list: Vec<ContactSdk> = Vec::with_capacity(contacts.len());
for contact in contacts.into_iter() {
list.push(contact.try_into()?);
}

Self {
Ok(Self {
inner: nostr::EventBuilder::contact_list(list),
}
})
}

/// Repost
Expand Down
48 changes: 15 additions & 33 deletions bindings/nostr-sdk-ffi/src/protocol/types/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,33 @@
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

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

use nostr::RelayUrl;
use uniffi::Object;
use uniffi::Record;

use crate::error::Result;
use crate::error::{NostrSdkError, Result};
use crate::protocol::key::PublicKey;

#[derive(Debug, PartialEq, Eq, Hash, Object)]
#[uniffi::export(Debug, Eq, Hash)]
#[derive(Record)]
pub struct Contact {
inner: nostr::Contact,
pub public_key: Arc<PublicKey>,
pub relay_url: Option<String>,
pub alias: Option<String>,
}

impl Deref for Contact {
type Target = nostr::Contact;
impl TryFrom<Contact> for nostr::Contact {
type Error = NostrSdkError;

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

#[uniffi::export]
impl Contact {
#[uniffi::constructor(default(relay_url = None, alias = None))]
pub fn new(pk: &PublicKey, relay_url: Option<String>, alias: Option<String>) -> Result<Self> {
let relay_url = match relay_url {
fn try_from(contact: Contact) -> Result<Self, Self::Error> {
let relay_url = match contact.relay_url {
Some(url) => Some(RelayUrl::parse(&url)?),
None => None,
};
Ok(Self {
inner: nostr::Contact::new(**pk, relay_url, alias),
})
}

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

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

pub fn relay_url(&self) -> Option<String> {
self.inner.relay_url.clone().map(|u| u.to_string())
Ok(nostr::Contact::new(
**contact.public_key,
relay_url,
contact.alias,
))
}
}

0 comments on commit 809d75c

Please sign in to comment.