diff --git a/whatsrust/src/lib.rs b/whatsrust/src/lib.rs index ba782d4..11d6662 100644 --- a/whatsrust/src/lib.rs +++ b/whatsrust/src/lib.rs @@ -10,6 +10,7 @@ use std::{ #[macro_use] mod callbacks; mod registrations; +mod lifecycle; mod abi; mod caches; mod events; @@ -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, @@ -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 for JID { unsafe fn to_rust(from: CJID) -> Self { (&from).into() @@ -661,18 +648,6 @@ impl CallbackTranslator 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), diff --git a/whatsrust/src/lifecycle.rs b/whatsrust/src/lifecycle.rs new file mode 100644 index 0000000..3fddfdc --- /dev/null +++ b/whatsrust/src/lifecycle.rs @@ -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 +} diff --git a/whatsrust/src/registrations.rs b/whatsrust/src/registrations.rs index aedb702..e706918 100644 --- a/whatsrust/src/registrations.rs +++ b/whatsrust/src/registrations.rs @@ -12,13 +12,6 @@ impl CallbackTranslator 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 for u8 { unsafe fn to_rust(value: u8) -> Self { value @@ -69,5 +62,3 @@ setup_handler!( msg: *const c_char => String, level: u8 => u8 ); - -setup_handler!(connect, C_Connect, qr: *const c_char => String);