bitwarden_core/
uniffi_support.rs

1//! This module contains custom type converters for Uniffi.
2
3use std::num::NonZeroU32;
4
5use bitwarden_crypto::{EncString, UnsignedSharedKey};
6use uuid::Uuid;
7
8use crate::UniffiCustomTypeConverter;
9
10uniffi::ffi_converter_forward!(NonZeroU32, bitwarden_crypto::UniFfiTag, crate::UniFfiTag);
11uniffi::ffi_converter_forward!(EncString, bitwarden_crypto::UniFfiTag, crate::UniFfiTag);
12uniffi::ffi_converter_forward!(
13    UnsignedSharedKey,
14    bitwarden_crypto::UniFfiTag,
15    crate::UniFfiTag
16);
17
18type DateTime = chrono::DateTime<chrono::Utc>;
19uniffi::custom_type!(DateTime, std::time::SystemTime);
20
21impl UniffiCustomTypeConverter for chrono::DateTime<chrono::Utc> {
22    type Builtin = std::time::SystemTime;
23
24    fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
25        Ok(Self::from(val))
26    }
27
28    fn from_custom(obj: Self) -> Self::Builtin {
29        obj.into()
30    }
31}
32
33uniffi::custom_type!(Uuid, String);
34
35impl UniffiCustomTypeConverter for Uuid {
36    type Builtin = String;
37
38    fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
39        Uuid::parse_str(val.as_str()).map_err(|e| e.into())
40    }
41
42    fn from_custom(obj: Self) -> Self::Builtin {
43        obj.to_string()
44    }
45}
46
47// Uniffi doesn't emit unused types, this is a dummy record to ensure that the custom type
48// converters are emitted
49#[allow(dead_code)]
50#[derive(uniffi::Record)]
51struct UniffiConverterDummyRecord {
52    uuid: Uuid,
53    date: DateTime,
54}