core_crypto_ffi/
client_id.rs

1#[cfg(target_family = "wasm")]
2use wasm_bindgen::prelude::*;
3
4#[derive(Debug, Clone, Eq, Hash, PartialEq, derive_more::From)]
5#[cfg_attr(
6    target_family = "wasm",
7    wasm_bindgen,
8    derive(serde::Serialize, serde::Deserialize),
9    serde(from = "ClientIdSerializationShim", into = "ClientIdSerializationShim")
10)]
11pub struct ClientId(pub(crate) core_crypto::prelude::ClientId);
12
13#[cfg(not(target_family = "wasm"))]
14uniffi::custom_type!(ClientId, Vec<u8>, {
15    lower: |id| id.0.to_vec(),
16    try_lift: |vec| Ok(Self(vec.into()))
17});
18
19#[cfg(target_family = "wasm")]
20#[wasm_bindgen]
21impl ClientId {
22    #[wasm_bindgen(constructor)]
23    pub fn new(bytes: Vec<u8>) -> Self {
24        Self(bytes.into())
25    }
26
27    pub fn as_bytes(&self) -> Vec<u8> {
28        self.0.to_vec()
29    }
30}
31
32pub type FfiClientId = Box<[u8]>;
33
34#[cfg(target_family = "wasm")]
35#[derive(serde::Serialize, serde::Deserialize)]
36struct ClientIdSerializationShim(Vec<u8>);
37
38#[cfg(target_family = "wasm")]
39impl From<ClientId> for ClientIdSerializationShim {
40    fn from(value: ClientId) -> Self {
41        Self(value.0.into())
42    }
43}
44
45#[cfg(target_family = "wasm")]
46impl From<ClientIdSerializationShim> for ClientId {
47    fn from(value: ClientIdSerializationShim) -> Self {
48        Self(value.0.into())
49    }
50}
51
52pub(crate) trait AsCoreCryptoClientId {
53    fn as_cc_client_id(&self) -> core_crypto::prelude::ClientId;
54}
55
56impl AsCoreCryptoClientId for ClientId {
57    fn as_cc_client_id(&self) -> core_crypto::prelude::ClientId {
58        self.0.clone()
59    }
60}
61
62impl AsCoreCryptoClientId for FfiClientId {
63    fn as_cc_client_id(&self) -> core_crypto::prelude::ClientId {
64        self.clone().into()
65    }
66}