Skip to content
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
31 changes: 3 additions & 28 deletions whatsrust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{
#[macro_use]
mod callbacks;
mod registrations;
mod lifecycle;
mod abi;
mod caches;
mod events;
Expand All @@ -20,9 +21,10 @@ pub use abi::{LogoutStatus, ReceiptKind};
pub use events::set_event_handler;
pub use callbacks::CallbackTranslator;
pub use registrations::{
connect, set_log_handler, set_message_handler,
set_log_handler, set_message_handler,
set_optimistic_text_sent_handler, set_presence_handler,
};
pub use lifecycle::{connect, disconnect, logout, new_client, pair_phone};
pub(crate) use models::file_kind_discriminant;
pub use models::{
ChatSettings, CommunitiesError, CommunityInfo, Contact, DownloadFailed, Event, FileContent,
Expand Down Expand Up @@ -634,21 +636,6 @@ mod read_receipt_ffi_tests {
}
}

pub fn pair_phone(phone: &str) -> String {
let phone_c = CString::new(phone).unwrap();
let result = unsafe { C_PairPhone(phone_c.as_ptr()) };
let result_str = unsafe { CStr::from_ptr(result) }
.to_string_lossy()
.into_owned();
unsafe { C_FreePairPhoneResult(result) };
result_str
}

pub fn new_client(db_path: &str) {
let db_path_c = CString::new(db_path).unwrap();
unsafe { C_NewClient(db_path_c.as_ptr()) }
}

impl CallbackTranslator<CJID> for JID {
unsafe fn to_rust(from: CJID) -> Self {
(&from).into()
Expand All @@ -661,18 +648,6 @@ impl CallbackTranslator<i64> for i64 {
}
}

pub fn disconnect() {
unsafe { C_Disconnect() }
}

pub fn logout() {
// Performs a deterministic local sign-out on the Go side (disconnect +
// clear the persisted device). The result arrives via Event::LogoutResult
// so the app can remove the DB file and quit. No network round-trip, so
// the UI never blocks.
unsafe { C_Logout() };
}

unsafe fn take_owned_c_string(
value: *mut c_char,
free: unsafe extern "C" fn(*mut c_char),
Expand Down
37 changes: 37 additions & 0 deletions whatsrust/src/lifecycle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use super::*;

impl CallbackTranslator<*const c_char> for String {
unsafe fn to_rust(ptr: *const c_char) -> String {
let c_str = unsafe { CStr::from_ptr(ptr) };
c_str.to_string_lossy().into_owned()
}
}

setup_handler!(connect, C_Connect, qr: *const c_char => String);

pub fn new_client(db_path: &str) {
let db_path_c = CString::new(db_path).unwrap();
unsafe { C_NewClient(db_path_c.as_ptr()) }
}

pub fn disconnect() {
unsafe { C_Disconnect() }
}

pub fn logout() {
// Performs a deterministic local sign-out on the Go side (disconnect +
// clear the persisted device). The result arrives via Event::LogoutResult
// so the app can remove the DB file and quit. No network round-trip, so
// the UI never blocks.
unsafe { C_Logout() };
}

pub fn pair_phone(phone: &str) -> String {
let phone_c = CString::new(phone).unwrap();
let result = unsafe { C_PairPhone(phone_c.as_ptr()) };
let result_str = unsafe { CStr::from_ptr(result) }
.to_string_lossy()
.into_owned();
unsafe { C_FreePairPhoneResult(result) };
result_str
}
9 changes: 0 additions & 9 deletions whatsrust/src/registrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ impl CallbackTranslator<u64> for u64 {
}
}

impl CallbackTranslator<*const c_char> for String {
unsafe fn to_rust(ptr: *const c_char) -> String {
let c_str = unsafe { CStr::from_ptr(ptr) };
c_str.to_string_lossy().into_owned()
}
}

impl CallbackTranslator<u8> for u8 {
unsafe fn to_rust(value: u8) -> Self {
value
Expand Down Expand Up @@ -69,5 +62,3 @@ setup_handler!(
msg: *const c_char => String,
level: u8 => u8
);

setup_handler!(connect, C_Connect, qr: *const c_char => String);
Loading