Skip to content

Commit

Permalink
[uniffi] wip: use with_foreign
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeisler committed Feb 26, 2024
1 parent d068d61 commit 35b19d5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion mls-rs-uniffi/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use mls_rs_crypto_openssl::OpensslCryptoProvider;

use self::group_state::GroupStateStorageWrapper;

mod group_state;
pub mod group_state;

#[derive(Debug, thiserror::Error, uniffi::Error)]
#[uniffi(flat_error)]
Expand Down
14 changes: 7 additions & 7 deletions mls-rs-uniffi/src/config/group_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ impl mls_rs_core::group::EpochRecord for EpochRecord {

#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)]
#[cfg_attr(mls_build_async, maybe_async::must_be_async)]
#[uniffi::export]
#[uniffi::export(with_foreign)]
pub trait GroupStateStorage: Send + Sync + Debug {
async fn state(&self, group_id: &[u8]) -> Result<Option<Vec<u8>>, FFICallbackError>;
async fn state(&self, group_id: Vec<u8>) -> Result<Option<Vec<u8>>, FFICallbackError>;
async fn epoch(
&self,
group_id: &[u8],
group_id: Vec<u8>,
epoch_id: u64,
) -> Result<Option<Vec<u8>>, FFICallbackError>;

Expand All @@ -46,7 +46,7 @@ pub trait GroupStateStorage: Send + Sync + Debug {
epoch_updates: Vec<Arc<EpochRecord>>,
) -> Result<(), FFICallbackError>;

async fn max_epoch_id(&self, group_id: &[u8]) -> Result<Option<u64>, FFICallbackError>;
async fn max_epoch_id(&self, group_id: Vec<u8>) -> Result<Option<u64>, FFICallbackError>;
}

#[derive(Debug, Clone)]
Expand All @@ -67,7 +67,7 @@ impl mls_rs_core::group::GroupStateStorage for GroupStateStorageWrapper {
where
T: mls_rs_core::group::GroupState + MlsEncode + MlsDecode,
{
let state_data = self.0.state(group_id)?;
let state_data = self.0.state(group_id.to_vec())?;

state_data
.as_deref()
Expand All @@ -80,7 +80,7 @@ impl mls_rs_core::group::GroupStateStorage for GroupStateStorageWrapper {
where
T: mls_rs_core::group::EpochRecord + MlsEncode + MlsDecode,
{
let epoch_data = self.0.epoch(group_id, epoch_id)?;
let epoch_data = self.0.epoch(group_id.to_vec(), epoch_id)?;

epoch_data
.as_deref()
Expand Down Expand Up @@ -118,6 +118,6 @@ impl mls_rs_core::group::GroupStateStorage for GroupStateStorageWrapper {
}

async fn max_epoch_id(&self, group_id: &[u8]) -> Result<Option<u64>, Self::Error> {
self.0.max_epoch_id(group_id)
self.0.max_epoch_id(group_id.to_vec())
}
}
27 changes: 26 additions & 1 deletion mls-rs-uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#[cfg(test)]
pub mod test_utils;

mod config;
pub mod config;

use std::sync::Arc;

Expand Down Expand Up @@ -592,6 +592,31 @@ from mls_rs_uniffi import CipherSuite, generate_signature_keypair
signature_keypair = generate_signature_keypair(CipherSuite.CURVE25519_AES128)
assert signature_keypair.cipher_suite == CipherSuite.CURVE25519_AES128
"#,
)
}

#[test]
fn test_client_new() -> Result<(), Box<dyn std::error::Error>> {
run_python(
r#"
from mls_rs_uniffi import *
signature_keypair = generate_signature_keypair(CipherSuite.CURVE25519_AES128)
class PythonGroupStateStorage(GroupStateStorage):
def state(self, group_id: "bytes"):
raise NotImplementedError
def epoch(self, group_id: "bytes",epoch_id: "int"):
raise NotImplementedError
def write(self, state: "GroupState",epoch_inserts: "typing.List[EpochRecord]",epoch_updates: "typing.List[EpochRecord]"):
raise NotImplementedError
def max_epoch_id(self, group_id: "bytes"):
raise NotImplementedError
group_state_storage = PythonGroupStateStorage()
client_config = ClientConfig(group_state_storage)
client = Client(b"alice", signature_keypair, client_config)
"#,
)
}
Expand Down

0 comments on commit 35b19d5

Please sign in to comment.