use std::fmt::Debug; use std::sync::Arc; use mls_rs::{ client_builder::{self, WithGroupStateStorage}, identity::basic, storage_provider::in_memory::InMemoryGroupStateStorage, }; use mls_rs_crypto_openssl::OpensslCryptoProvider; use self::group_state::{GroupStateStorage, GroupStateStorageAdapter}; use crate::Error; pub mod group_state; #[derive(Debug, Clone)] pub(crate) struct ClientGroupStorage(Arc); impl From> for ClientGroupStorage { fn from(value: Arc) -> Self { Self(value) } } #[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] #[cfg_attr(mls_build_async, maybe_async::must_be_async)] impl mls_rs_core::group::GroupStateStorage for ClientGroupStorage { type Error = Error; async fn state(&self, group_id: &[u8]) -> Result>, Self::Error> { self.0.state(group_id.to_vec()).await } async fn epoch(&self, group_id: &[u8], epoch_id: u64) -> Result>, Self::Error> { self.0.epoch(group_id.to_vec(), epoch_id).await } async fn write( &mut self, state: mls_rs_core::group::GroupState, inserts: Vec, updates: Vec, ) -> Result<(), Self::Error> { self.0 .write( state.id, state.data, inserts.into_iter().map(Into::into).collect(), updates.into_iter().map(Into::into).collect(), ) .await } async fn max_epoch_id(&self, group_id: &[u8]) -> Result, Self::Error> { self.0.max_epoch_id(group_id.to_vec()).await } } pub type UniFFIConfig = client_builder::WithIdentityProvider< basic::BasicIdentityProvider, client_builder::WithCryptoProvider< OpensslCryptoProvider, WithGroupStateStorage, >, >; #[derive(Debug, Clone, uniffi::Record)] pub struct ClientConfig { pub group_state_storage: Arc, /// Use the ratchet tree extension. If this is false, then you /// must supply `ratchet_tree` out of band to clients. pub use_ratchet_tree_extension: bool, } impl Default for ClientConfig { fn default() -> Self { Self { group_state_storage: Arc::new(GroupStateStorageAdapter::new( InMemoryGroupStateStorage::new(), )), use_ratchet_tree_extension: true, } } } // TODO(mgeisler): turn into an associated function when UniFFI // supports them: https://github.com/mozilla/uniffi-rs/issues/1074. /// Create a client config with an in-memory group state storage. #[uniffi::export] pub fn client_config_default() -> ClientConfig { ClientConfig::default() }