bitwarden_crypto/
uniffi_support.rs

1use std::{num::NonZeroU32, str::FromStr};
2
3use bitwarden_uniffi_error::convert_result;
4
5use crate::{
6    CryptoError, EncString, EncodingError, PublicKey, 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!(PublicKey, String, {
40    try_lift: |val| {
41        convert_result(PublicKey::from_str(&val)
42            .map_err(|_e| EncodingError::InvalidBase64Encoding))
43    },
44    lower: |obj| obj.to_string(),
45});
46
47uniffi::custom_type!(DataEnvelope, String, {
48    try_lift: |val| convert_result(DataEnvelope::from_str(val.as_str())),
49    lower: |obj| obj.to_string(),
50});
51
52uniffi::custom_type!(PasswordProtectedKeyEnvelope, String, {
53    remote,
54    try_lift: |val| convert_result(PasswordProtectedKeyEnvelope::from_str(&val)),
55    lower: |obj| obj.into(),
56});