diff --git a/Cargo.lock b/Cargo.lock index 437f13e5e78..087b91ef18d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5529,7 +5529,7 @@ checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" [[package]] name = "uniffi" version = "0.23.0" -source = "git+https://github.com/mozilla/uniffi-rs?rev=aa91307b6ac27aae6d5c7ad971b762df952d2745#aa91307b6ac27aae6d5c7ad971b762df952d2745" +source = "git+https://github.com/mozilla/uniffi-rs?rev=9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb#9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb" dependencies = [ "anyhow", "camino", @@ -5550,7 +5550,7 @@ dependencies = [ [[package]] name = "uniffi_bindgen" version = "0.23.0" -source = "git+https://github.com/mozilla/uniffi-rs?rev=aa91307b6ac27aae6d5c7ad971b762df952d2745#aa91307b6ac27aae6d5c7ad971b762df952d2745" +source = "git+https://github.com/mozilla/uniffi-rs?rev=9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb#9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb" dependencies = [ "anyhow", "askama", @@ -5572,7 +5572,7 @@ dependencies = [ [[package]] name = "uniffi_build" version = "0.23.0" -source = "git+https://github.com/mozilla/uniffi-rs?rev=aa91307b6ac27aae6d5c7ad971b762df952d2745#aa91307b6ac27aae6d5c7ad971b762df952d2745" +source = "git+https://github.com/mozilla/uniffi-rs?rev=9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb#9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb" dependencies = [ "anyhow", "camino", @@ -5582,7 +5582,7 @@ dependencies = [ [[package]] name = "uniffi_checksum_derive" version = "0.23.0" -source = "git+https://github.com/mozilla/uniffi-rs?rev=aa91307b6ac27aae6d5c7ad971b762df952d2745#aa91307b6ac27aae6d5c7ad971b762df952d2745" +source = "git+https://github.com/mozilla/uniffi-rs?rev=9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb#9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb" dependencies = [ "quote", "syn 1.0.109", @@ -5591,7 +5591,7 @@ dependencies = [ [[package]] name = "uniffi_core" version = "0.23.0" -source = "git+https://github.com/mozilla/uniffi-rs?rev=aa91307b6ac27aae6d5c7ad971b762df952d2745#aa91307b6ac27aae6d5c7ad971b762df952d2745" +source = "git+https://github.com/mozilla/uniffi-rs?rev=9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb#9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb" dependencies = [ "anyhow", "bytes", @@ -5606,7 +5606,7 @@ dependencies = [ [[package]] name = "uniffi_macros" version = "0.23.0" -source = "git+https://github.com/mozilla/uniffi-rs?rev=aa91307b6ac27aae6d5c7ad971b762df952d2745#aa91307b6ac27aae6d5c7ad971b762df952d2745" +source = "git+https://github.com/mozilla/uniffi-rs?rev=9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb#9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb" dependencies = [ "bincode", "camino", @@ -5624,7 +5624,7 @@ dependencies = [ [[package]] name = "uniffi_meta" version = "0.23.0" -source = "git+https://github.com/mozilla/uniffi-rs?rev=aa91307b6ac27aae6d5c7ad971b762df952d2745#aa91307b6ac27aae6d5c7ad971b762df952d2745" +source = "git+https://github.com/mozilla/uniffi-rs?rev=9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb#9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb" dependencies = [ "anyhow", "bytes", @@ -5637,7 +5637,7 @@ dependencies = [ [[package]] name = "uniffi_testing" version = "0.23.0" -source = "git+https://github.com/mozilla/uniffi-rs?rev=aa91307b6ac27aae6d5c7ad971b762df952d2745#aa91307b6ac27aae6d5c7ad971b762df952d2745" +source = "git+https://github.com/mozilla/uniffi-rs?rev=9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb#9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb" dependencies = [ "anyhow", "camino", @@ -5936,7 +5936,7 @@ dependencies = [ [[package]] name = "weedle2" version = "4.0.0" -source = "git+https://github.com/mozilla/uniffi-rs?rev=aa91307b6ac27aae6d5c7ad971b762df952d2745#aa91307b6ac27aae6d5c7ad971b762df952d2745" +source = "git+https://github.com/mozilla/uniffi-rs?rev=9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb#9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb" dependencies = [ "nom", ] diff --git a/Cargo.toml b/Cargo.toml index d9d522acb22..d38f351e8f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,8 +42,8 @@ thiserror = "1.0.38" tokio = { version = "1.24", default-features = false, features = ["sync"] } tracing = { version = "0.1.36", default-features = false, features = ["std"] } tracing-core = "0.1.30" -uniffi = { git = "https://github.com/mozilla/uniffi-rs", rev = "aa91307b6ac27aae6d5c7ad971b762df952d2745" } -uniffi_bindgen = { git = "https://github.com/mozilla/uniffi-rs", rev = "aa91307b6ac27aae6d5c7ad971b762df952d2745" } +uniffi = { git = "https://github.com/mozilla/uniffi-rs", rev = "9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb" } +uniffi_bindgen = { git = "https://github.com/mozilla/uniffi-rs", rev = "9e01d2281bb4a603fc9ed6409a02ad1854cdc8fb" } vodozemac = { git = "https://github.com/matrix-org/vodozemac", rev = "fb609ca1e4df5a7a818490ae86ac694119e41e71" } zeroize = "1.3.0" diff --git a/bindings/matrix-sdk-crypto-ffi/src/backup_recovery_key.rs b/bindings/matrix-sdk-crypto-ffi/src/backup_recovery_key.rs index 5cf6e3974f4..2de8c2b2049 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/backup_recovery_key.rs +++ b/bindings/matrix-sdk-crypto-ffi/src/backup_recovery_key.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, iter, ops::DerefMut}; +use std::{collections::HashMap, iter, ops::DerefMut, sync::Arc}; use hmac::Hmac; use matrix_sdk_crypto::{ @@ -12,6 +12,7 @@ use thiserror::Error; use zeroize::Zeroize; /// The private part of the backup key, the one used for recovery. +#[derive(uniffi::Object)] pub struct BackupRecoveryKey { pub(crate) inner: RecoveryKey, pub(crate) passphrase_info: Option, @@ -62,46 +63,40 @@ pub struct MegolmV1BackupKey { pub backup_algorithm: String, } -#[uniffi::export] -impl BackupRecoveryKey { - /// Convert the recovery key to a base 58 encoded string. - pub fn to_base58(&self) -> String { - self.inner.to_base58() - } - - /// Convert the recovery key to a base 64 encoded string. - pub fn to_base64(&self) -> String { - self.inner.to_base64() - } -} - impl BackupRecoveryKey { const KEY_SIZE: usize = 32; const SALT_SIZE: usize = 32; const PBKDF_ROUNDS: i32 = 500_000; +} +#[uniffi::export] +impl BackupRecoveryKey { /// Create a new random [`BackupRecoveryKey`]. #[allow(clippy::new_without_default)] - pub fn new() -> Self { - Self { + #[uniffi::constructor] + pub fn new() -> Arc { + Arc::new(Self { inner: RecoveryKey::new() .expect("Can't gather enough randomness to create a recovery key"), passphrase_info: None, - } + }) } /// Try to create a [`BackupRecoveryKey`] from a base 64 encoded string. - pub fn from_base64(key: String) -> Result { - Ok(Self { inner: RecoveryKey::from_base64(&key)?, passphrase_info: None }) + #[uniffi::constructor] + pub fn from_base64(key: String) -> Result, DecodeError> { + Ok(Arc::new(Self { inner: RecoveryKey::from_base64(&key)?, passphrase_info: None })) } /// Try to create a [`BackupRecoveryKey`] from a base 58 encoded string. - pub fn from_base58(key: String) -> Result { - Ok(Self { inner: RecoveryKey::from_base58(&key)?, passphrase_info: None }) + #[uniffi::constructor] + pub fn from_base58(key: String) -> Result, DecodeError> { + Ok(Arc::new(Self { inner: RecoveryKey::from_base58(&key)?, passphrase_info: None })) } /// Create a new [`BackupRecoveryKey`] from the given passphrase. - pub fn new_from_passphrase(passphrase: String) -> Self { + #[uniffi::constructor] + pub fn new_from_passphrase(passphrase: String) -> Arc { let mut rng = thread_rng(); let salt: String = iter::repeat(()) .map(|()| rng.sample(Alphanumeric)) @@ -113,7 +108,8 @@ impl BackupRecoveryKey { } /// Restore a [`BackupRecoveryKey`] from the given passphrase. - pub fn from_passphrase(passphrase: String, salt: String, rounds: i32) -> Self { + #[uniffi::constructor] + pub fn from_passphrase(passphrase: String, salt: String, rounds: i32) -> Arc { let mut key = Box::new([0u8; Self::KEY_SIZE]); let rounds = rounds as u32; @@ -123,18 +119,25 @@ impl BackupRecoveryKey { key.zeroize(); - Self { + Arc::new(Self { inner: recovery_key, passphrase_info: Some(PassphraseInfo { private_key_salt: salt, private_key_iterations: rounds as i32, }), - } + }) + } + + /// Convert the recovery key to a base 58 encoded string. + pub fn to_base58(&self) -> String { + self.inner.to_base58() + } + + /// Convert the recovery key to a base 64 encoded string. + pub fn to_base64(&self) -> String { + self.inner.to_base64() } -} -#[uniffi::export] -impl BackupRecoveryKey { /// Get the public part of the backup key. pub fn megolm_v1_public_key(&self) -> MegolmV1BackupKey { let public_key = self.inner.megolm_v1_public_key(); diff --git a/bindings/matrix-sdk-crypto-ffi/src/error.rs b/bindings/matrix-sdk-crypto-ffi/src/error.rs index 04bcc9e5570..3cb6d30fe9a 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/error.rs +++ b/bindings/matrix-sdk-crypto-ffi/src/error.rs @@ -42,7 +42,8 @@ pub enum SignatureError { UnknownUserIdentity(String), } -#[derive(Debug, thiserror::Error)] +#[derive(Debug, thiserror::Error, uniffi::Error)] +#[uniffi(flat_error)] pub enum CryptoStoreError { #[error("Failed to open the store")] OpenStore(#[from] OpenStoreError), diff --git a/bindings/matrix-sdk-crypto-ffi/src/lib.rs b/bindings/matrix-sdk-crypto-ffi/src/lib.rs index d26ea8d04fa..9674954b566 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/lib.rs +++ b/bindings/matrix-sdk-crypto-ffi/src/lib.rs @@ -972,7 +972,12 @@ mod test { migrate(migration_data, path.clone(), None, Box::new(|_, _| {}))?; - let machine = OlmMachine::new("@ganfra146:matrix.org", "DEWRCMENGS", &path, None)?; + let machine = OlmMachine::new( + "@ganfra146:matrix.org".to_owned(), + "DEWRCMENGS".to_owned(), + path, + None, + )?; assert_eq!( machine.identity_keys()["ed25519"], diff --git a/bindings/matrix-sdk-crypto-ffi/src/machine.rs b/bindings/matrix-sdk-crypto-ffi/src/machine.rs index e9f267601ab..7e156baa1ed 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/machine.rs +++ b/bindings/matrix-sdk-crypto-ffi/src/machine.rs @@ -59,6 +59,7 @@ use crate::{ }; /// A high level state machine that handles E2EE for Matrix. +#[derive(uniffi::Object)] pub struct OlmMachine { pub(crate) inner: ManuallyDrop, pub(crate) runtime: Runtime, @@ -128,6 +129,7 @@ impl From for SignatureVerification { } } +#[uniffi::export] impl OlmMachine { /// Create a new `OlmMachine` /// @@ -142,14 +144,15 @@ impl OlmMachine { /// * `passphrase` - The passphrase that should be used to encrypt the data /// at rest in the Sled store. **Warning**, if no passphrase is given, the /// store and all its data will remain unencrypted. + #[uniffi::constructor] pub fn new( - user_id: &str, - device_id: &str, - path: &str, + user_id: String, + device_id: String, + path: String, mut passphrase: Option, - ) -> Result { - let user_id = parse_user_id(user_id)?; - let device_id = device_id.into(); + ) -> Result, CryptoStoreError> { + let user_id = parse_user_id(&user_id)?; + let device_id = device_id.as_str().into(); let runtime = Runtime::new().expect("Couldn't create a tokio runtime"); let store = runtime @@ -160,41 +163,9 @@ impl OlmMachine { let inner = runtime.block_on(InnerMachine::with_store(&user_id, device_id, Arc::new(store)))?; - Ok(OlmMachine { inner: ManuallyDrop::new(inner), runtime }) - } - - fn import_room_keys_helper( - &self, - keys: Vec, - from_backup: bool, - progress_listener: Box, - ) -> Result { - let listener = |progress: usize, total: usize| { - progress_listener.on_progress(progress as i32, total as i32) - }; - - let result = - self.runtime.block_on(self.inner.import_room_keys(keys, from_backup, listener))?; - - Ok(KeysImportResult { - imported: result.imported_count as i64, - total: result.total_count as i64, - keys: result - .keys - .into_iter() - .map(|(r, m)| { - ( - r.to_string(), - m.into_iter().map(|(s, k)| (s, k.into_iter().collect())).collect(), - ) - }) - .collect(), - }) + Ok(Arc::new(OlmMachine { inner: ManuallyDrop::new(inner), runtime })) } -} -#[uniffi::export] -impl OlmMachine { /// Get the user ID of the owner of this `OlmMachine`. pub fn user_id(&self) -> String { self.inner.user_id().to_string() @@ -1402,3 +1373,34 @@ impl OlmMachine { .into()) } } + +impl OlmMachine { + fn import_room_keys_helper( + &self, + keys: Vec, + from_backup: bool, + progress_listener: Box, + ) -> Result { + let listener = |progress: usize, total: usize| { + progress_listener.on_progress(progress as i32, total as i32) + }; + + let result = + self.runtime.block_on(self.inner.import_room_keys(keys, from_backup, listener))?; + + Ok(KeysImportResult { + imported: result.imported_count as i64, + total: result.total_count as i64, + keys: result + .keys + .into_iter() + .map(|(r, m)| { + ( + r.to_string(), + m.into_iter().map(|(s, k)| (s, k.into_iter().collect())).collect(), + ) + }) + .collect(), + }) + } +} diff --git a/bindings/matrix-sdk-crypto-ffi/src/olm.udl b/bindings/matrix-sdk-crypto-ffi/src/olm.udl index b98565243e1..71adf3d83a0 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/olm.udl +++ b/bindings/matrix-sdk-crypto-ffi/src/olm.udl @@ -8,16 +8,6 @@ callback interface ProgressListener { void on_progress(i32 progress, i32 total); }; -[Error] -enum CryptoStoreError { - "OpenStore", - "CryptoStore", - "OlmError", - "Serialization", - "InvalidUserId", - "Identifier", -}; - dictionary CancelInfo { string cancel_code; string reason; @@ -71,31 +61,9 @@ enum LocalTrust { "Unset", }; -interface OlmMachine { - [Throws=CryptoStoreError] - constructor( - [ByRef] string user_id, - [ByRef] string device_id, - [ByRef] string path, - string? passphrase - ); -}; - enum SignatureState { "Missing", "Invalid", "ValidButNotTrusted", "ValidAndTrusted", }; - -interface BackupRecoveryKey { - constructor(); - [Name=from_passphrase] - constructor(string passphrase, string salt, i32 rounds); - [Name=new_from_passphrase] - constructor(string passphrase); - [Name=from_base64, Throws=DecodeError] - constructor(string key); - [Name=from_base58, Throws=DecodeError] - constructor(string key); -}; diff --git a/bindings/matrix-sdk-ffi/src/api.udl b/bindings/matrix-sdk-ffi/src/api.udl index f84e818de0a..dabf4c60d01 100644 --- a/bindings/matrix-sdk-ffi/src/api.udl +++ b/bindings/matrix-sdk-ffi/src/api.udl @@ -1,10 +1,5 @@ namespace matrix_sdk_ffi {}; -[Error] -interface ClientError { - Generic(string msg); -}; - callback interface ClientDelegate { void did_receive_auth_error(boolean is_soft_logout); }; @@ -96,14 +91,6 @@ callback interface SlidingSyncListRoomItemsObserver { void did_receive_update(); }; -interface SlidingSyncListBuilder { - constructor(); -}; - -interface ClientBuilder { - constructor(); -}; - dictionary CreateRoomParameters { string? name; string? topic = null; @@ -164,14 +151,6 @@ interface MediaSource { string url(); }; -interface AuthenticationService { - constructor(string base_path, string? passphrase, string? custom_sliding_sync_proxy); -}; - -interface NotificationService { - constructor(string base_path, string user_id); -}; - interface SessionVerificationEmoji {}; callback interface SessionVerificationControllerDelegate { @@ -182,17 +161,3 @@ callback interface SessionVerificationControllerDelegate { void did_cancel(); void did_finish(); }; - -interface Span { - constructor(string file, u32 line, u32 column, LogLevel level, string target, string name); - [Name=current] - constructor(); -}; - -enum LogLevel { - "Error", - "Warn", - "Info", - "Debug", - "Trace", -}; diff --git a/bindings/matrix-sdk-ffi/src/authentication_service.rs b/bindings/matrix-sdk-ffi/src/authentication_service.rs index 58df9ef2a72..fae7387d8af 100644 --- a/bindings/matrix-sdk-ffi/src/authentication_service.rs +++ b/bindings/matrix-sdk-ffi/src/authentication_service.rs @@ -11,6 +11,7 @@ use zeroize::Zeroize; use super::{client::Client, client_builder::ClientBuilder, RUNTIME}; use crate::error::ClientError; +#[derive(uniffi::Object)] pub struct AuthenticationService { base_path: String, passphrase: Option, @@ -78,44 +79,24 @@ impl HomeserverLoginDetails { } } +#[uniffi::export] impl AuthenticationService { /// Creates a new service to authenticate a user with. + #[uniffi::constructor] pub fn new( base_path: String, passphrase: Option, custom_sliding_sync_proxy: Option, - ) -> Self { - AuthenticationService { + ) -> Arc { + Arc::new(AuthenticationService { base_path, passphrase, client: RwLock::new(None), homeserver_details: RwLock::new(None), custom_sliding_sync_proxy: RwLock::new(custom_sliding_sync_proxy), - } + }) } - /// Get the homeserver login details from a client. - async fn details_from_client( - &self, - client: &Arc, - ) -> Result { - let login_details = join3( - client.async_homeserver(), - client.authentication_issuer(), - client.supports_password_login(), - ) - .await; - - let url = login_details.0; - let authentication_issuer = login_details.1; - let supports_password_login = login_details.2?; - - Ok(HomeserverLoginDetails { url, authentication_issuer, supports_password_login }) - } -} - -#[uniffi::export] -impl AuthenticationService { pub fn homeserver_details(&self) -> Option> { self.homeserver_details.read().unwrap().clone() } @@ -126,7 +107,7 @@ impl AuthenticationService { &self, server_name_or_homeserver_url: String, ) -> Result<(), AuthenticationError> { - let mut builder = Arc::new(ClientBuilder::new()).base_path(self.base_path.clone()); + let mut builder = ClientBuilder::new().base_path(self.base_path.clone()); // Attempt discovery as a server name first. let result = matrix_sdk::sanitize_server_name(&server_name_or_homeserver_url); @@ -152,7 +133,7 @@ impl AuthenticationService { return Err(e); } // When discovery fails, fallback to the homeserver URL if supplied. - let mut builder = Arc::new(ClientBuilder::new()).base_path(self.base_path.clone()); + let mut builder = ClientBuilder::new().base_path(self.base_path.clone()); builder = builder.homeserver_url(server_name_or_homeserver_url); builder.build_inner() })?; @@ -205,7 +186,7 @@ impl AuthenticationService { sliding_sync_proxy = None; } - let client = Arc::new(ClientBuilder::new()) + let client = ClientBuilder::new() .base_path(self.base_path.clone()) .passphrase(self.passphrase.clone()) .homeserver_url(homeserver_url) @@ -258,7 +239,7 @@ impl AuthenticationService { user_id: whoami.user_id.clone(), device_id, }; - let client = Arc::new(ClientBuilder::new()) + let client = ClientBuilder::new() .base_path(self.base_path.clone()) .passphrase(self.passphrase.clone()) .homeserver_url(homeserver_url) @@ -270,3 +251,24 @@ impl AuthenticationService { Ok(client) } } + +impl AuthenticationService { + /// Get the homeserver login details from a client. + async fn details_from_client( + &self, + client: &Arc, + ) -> Result { + let login_details = join3( + client.async_homeserver(), + client.authentication_issuer(), + client.supports_password_login(), + ) + .await; + + let url = login_details.0; + let authentication_issuer = login_details.1; + let supports_password_login = login_details.2?; + + Ok(HomeserverLoginDetails { url, authentication_issuer, supports_password_login }) + } +} diff --git a/bindings/matrix-sdk-ffi/src/client_builder.rs b/bindings/matrix-sdk-ffi/src/client_builder.rs index 539b4e6b8e8..118640262f9 100644 --- a/bindings/matrix-sdk-ffi/src/client_builder.rs +++ b/bindings/matrix-sdk-ffi/src/client_builder.rs @@ -15,7 +15,7 @@ use zeroize::Zeroizing; use super::{client::Client, RUNTIME}; use crate::{error::ClientError, helpers::unwrap_or_clone_arc}; -#[derive(Clone)] +#[derive(Clone, uniffi::Object)] pub struct ClientBuilder { base_path: Option, username: Option, @@ -28,24 +28,13 @@ pub struct ClientBuilder { inner: MatrixClientBuilder, } +#[uniffi::export] impl ClientBuilder { - pub fn new() -> Self { - Self { - base_path: None, - username: None, - server_name: None, - homeserver_url: None, - server_versions: None, - passphrase: Zeroizing::new(None), - user_agent: None, - sliding_sync_proxy: None, - inner: MatrixClient::builder(), - } + #[uniffi::constructor] + pub fn new() -> Arc { + Arc::new(Self::default()) } -} -#[uniffi::export] -impl ClientBuilder { pub fn base_path(self: Arc, path: String) -> Arc { let mut builder = unwrap_or_clone_arc(self); builder.base_path = Some(path); @@ -168,6 +157,16 @@ impl ClientBuilder { impl Default for ClientBuilder { fn default() -> Self { - Self::new() + Self { + base_path: None, + username: None, + server_name: None, + homeserver_url: None, + server_versions: None, + passphrase: Zeroizing::new(None), + user_agent: None, + sliding_sync_proxy: None, + inner: MatrixClient::builder(), + } } } diff --git a/bindings/matrix-sdk-ffi/src/error.rs b/bindings/matrix-sdk-ffi/src/error.rs index fe809fa71f0..ee9f01bdce0 100644 --- a/bindings/matrix-sdk-ffi/src/error.rs +++ b/bindings/matrix-sdk-ffi/src/error.rs @@ -1,6 +1,6 @@ use matrix_sdk::{self, encryption::CryptoStoreError, HttpError, IdParseError, StoreError}; -#[derive(thiserror::Error, Debug)] +#[derive(Debug, thiserror::Error, uniffi::Error)] pub enum ClientError { #[error("client error: {msg}")] Generic { msg: String }, diff --git a/bindings/matrix-sdk-ffi/src/lib.rs b/bindings/matrix-sdk-ffi/src/lib.rs index 152c3e62ceb..546f5f62543 100644 --- a/bindings/matrix-sdk-ffi/src/lib.rs +++ b/bindings/matrix-sdk-ffi/src/lib.rs @@ -40,7 +40,7 @@ use once_cell::sync::Lazy; use tokio::runtime::Runtime; // Re-exports for more convenient use inside other submodules -use self::{client::Client, client_builder::ClientBuilder, error::ClientError}; +use self::{client::Client, error::ClientError}; pub static RUNTIME: Lazy = Lazy::new(|| Runtime::new().expect("Can't start Tokio runtime")); diff --git a/bindings/matrix-sdk-ffi/src/notification_service.rs b/bindings/matrix-sdk-ffi/src/notification_service.rs index 6d2029305a9..64f3d897784 100644 --- a/bindings/matrix-sdk-ffi/src/notification_service.rs +++ b/bindings/matrix-sdk-ffi/src/notification_service.rs @@ -51,24 +51,24 @@ impl NotificationItem { } #[allow(dead_code)] +#[derive(uniffi::Object)] pub struct NotificationService { base_path: String, user_id: String, } +#[uniffi::export] impl NotificationService { /// Creates a new notification service. /// /// Will be used to fetch an event after receiving a notification. /// Please note that this will be called on a new process than the /// application context. - pub fn new(base_path: String, user_id: String) -> Self { - Self { base_path, user_id } + #[uniffi::constructor] + pub fn new(base_path: String, user_id: String) -> Arc { + Arc::new(Self { base_path, user_id }) } -} -#[uniffi::export] -impl NotificationService { /// Get notification item for a given `room_id `and `event_id`. /// /// Returns `None` if this notification should not be displayed to the user. diff --git a/bindings/matrix-sdk-ffi/src/sliding_sync.rs b/bindings/matrix-sdk-ffi/src/sliding_sync.rs index 1ae7a0cbccf..b712b91b36e 100644 --- a/bindings/matrix-sdk-ffi/src/sliding_sync.rs +++ b/bindings/matrix-sdk-ffi/src/sliding_sync.rs @@ -430,7 +430,7 @@ pub trait SlidingSyncListStateObserver: Sync + Send { fn did_receive_update(&self, new_state: SlidingSyncState); } -#[derive(Clone)] +#[derive(Clone, uniffi::Object)] pub struct SlidingSyncListBuilder { inner: matrix_sdk::SlidingSyncListBuilder, } @@ -471,14 +471,13 @@ impl From for SyncRequestListFilters { } } +#[uniffi::export] impl SlidingSyncListBuilder { - pub fn new() -> Self { - Self { inner: matrix_sdk::SlidingSyncList::builder() } + #[uniffi::constructor] + pub fn new() -> Arc { + Arc::new(Self { inner: matrix_sdk::SlidingSyncList::builder() }) } -} -#[uniffi::export] -impl SlidingSyncListBuilder { pub fn sync_mode(self: Arc, mode: SlidingSyncMode) -> Arc { let mut builder = unwrap_or_clone_arc(self); builder.inner = builder.inner.sync_mode(mode); diff --git a/bindings/matrix-sdk-ffi/src/tracing.rs b/bindings/matrix-sdk-ffi/src/tracing.rs index cd266658e31..c530f33f371 100644 --- a/bindings/matrix-sdk-ffi/src/tracing.rs +++ b/bindings/matrix-sdk-ffi/src/tracing.rs @@ -1,4 +1,7 @@ -use std::{collections::BTreeMap, sync::Mutex}; +use std::{ + collections::BTreeMap, + sync::{Arc, Mutex}, +}; use once_cell::sync::OnceCell; use tracing::{callsite::DefaultCallsite, field::FieldSet, Callsite}; @@ -92,8 +95,10 @@ fn span_or_event_enabled(callsite: &'static DefaultCallsite) -> bool { } } +#[derive(uniffi::Object)] pub struct Span(tracing::Span); +#[uniffi::export] impl Span { /// Create a span originating at the given callsite (file, line and column). /// @@ -119,6 +124,7 @@ impl Span { /// case it should also be exited on all of them individually; that is, /// unless you *want* the span to be attached to all further events created /// on that thread. + #[uniffi::constructor] pub fn new( file: String, line: u32, @@ -126,7 +132,7 @@ impl Span { level: LogLevel, target: String, name: String, - ) -> Self { + ) -> Arc { static CALLSITES: Mutex> = Mutex::new(BTreeMap::new()); let loc = Location::new(file, line, column); @@ -143,16 +149,14 @@ impl Span { tracing::Span::none() }; - Span(span) + Arc::new(Self(span)) } - pub fn current() -> Self { - Self(tracing::Span::current()) + #[uniffi::constructor] + pub fn current() -> Arc { + Arc::new(Self(tracing::Span::current())) } -} -#[uniffi::export] -impl Span { fn enter(&self) { self.0.with_subscriber(|(id, dispatch)| dispatch.enter(id)); } @@ -166,6 +170,7 @@ impl Span { } } +#[derive(uniffi::Enum)] pub enum LogLevel { Error, Warn,