Skip to main content

bitwarden_core/
uniffi_support.rs

1//! This module contains custom type converters for Uniffi.
2
3use std::{num::NonZeroU32, str::FromStr};
4
5use bitwarden_crypto::{SymmetricCryptoKey, safe};
6use bitwarden_uniffi_error::convert_result;
7use uuid::Uuid;
8
9use crate::key_management::SignedSecurityState;
10
11uniffi::use_remote_type!(bitwarden_crypto::NonZeroU32);
12uniffi::use_remote_type!(bitwarden_crypto::safe::PasswordProtectedKeyEnvelope);
13uniffi::use_remote_type!(bitwarden_crypto::SymmetricCryptoKey);
14
15type DateTime = chrono::DateTime<chrono::Utc>;
16uniffi::custom_type!(DateTime, std::time::SystemTime, { remote });
17
18uniffi::custom_type!(Uuid, String, {
19    remote,
20    try_lift: |val| convert_result(Uuid::parse_str(&val)),
21    lower: |obj| obj.to_string(),
22});
23
24type NaiveDate = chrono::NaiveDate;
25uniffi::custom_type!(NaiveDate, String, {
26    remote,
27    try_lift: |val| convert_result(NaiveDate::from_str(&val)),
28    lower: |obj| obj.to_string(),
29});
30
31// Uniffi doesn't emit unused types, this is a dummy record to ensure that the custom type
32// converters are emitted
33#[allow(dead_code)]
34#[derive(uniffi::Record)]
35struct UniffiConverterDummyRecord {
36    uuid: Uuid,
37    date: DateTime,
38    naive_date: NaiveDate,
39}
40
41uniffi::custom_type!(SignedSecurityState, String, {
42    try_lift: |val| {
43        convert_result(SignedSecurityState::from_str(&val))
44    },
45    lower: |obj| obj.into(),
46});