bitwarden_crypto/
uniffi_support.rs1use std::{num::NonZeroU32, str::FromStr};
2
3use bitwarden_uniffi_error::convert_result;
4
5use crate::{
6 CryptoError, EncString, SignedPublicKey, UnsignedSharedKey,
7 safe::{DataEnvelope, PasswordProtectedKeyEnvelope},
8};
9
10uniffi::custom_type!(NonZeroU32, u32, {
11 remote,
12 try_lift: |val| {
13 convert_result(NonZeroU32::new(val).ok_or(CryptoError::ZeroNumber))
14 },
15 lower: |obj| obj.get(),
16});
17
18uniffi::custom_type!(EncString, String, {
19 try_lift: |val| {
20 convert_result(EncString::from_str(&val))
21 },
22 lower: |obj| obj.to_string(),
23});
24
25uniffi::custom_type!(UnsignedSharedKey, String, {
26 try_lift: |val| {
27 convert_result(UnsignedSharedKey::from_str(&val))
28 },
29 lower: |obj| obj.to_string(),
30});
31
32uniffi::custom_type!(SignedPublicKey, String, {
33 try_lift: |val| {
34 convert_result(SignedPublicKey::from_str(&val))
35 },
36 lower: |obj| obj.into(),
37});
38
39uniffi::custom_type!(DataEnvelope, String, {
40 try_lift: |val| DataEnvelope::from_str(val.as_str())
41 .map_err(|e| e.into()),
42 lower: |obj| obj.to_string(),
43});
44
45uniffi::custom_type!(PasswordProtectedKeyEnvelope, String, {
46 remote,
47 try_lift: |val| convert_result(PasswordProtectedKeyEnvelope::from_str(&val)),
48 lower: |obj| obj.into(),
49});