bitwarden_crypto/
uniffi_support.rs

1use std::{num::NonZeroU32, str::FromStr};
2
3use crate::{CryptoError, EncString, UniffiCustomTypeConverter, UnsignedSharedKey};
4
5uniffi::custom_type!(NonZeroU32, u32);
6
7impl UniffiCustomTypeConverter for NonZeroU32 {
8    type Builtin = u32;
9
10    fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
11        Self::new(val).ok_or(CryptoError::ZeroNumber.into())
12    }
13
14    fn from_custom(obj: Self) -> Self::Builtin {
15        obj.get()
16    }
17}
18
19uniffi::custom_type!(EncString, String);
20
21impl UniffiCustomTypeConverter for EncString {
22    type Builtin = String;
23
24    fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
25        val.parse().map_err(|e: CryptoError| e.into())
26    }
27
28    fn from_custom(obj: Self) -> Self::Builtin {
29        obj.to_string()
30    }
31}
32
33uniffi::custom_type!(UnsignedSharedKey, String);
34
35impl UniffiCustomTypeConverter for UnsignedSharedKey {
36    type Builtin = String;
37
38    fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
39        Self::from_str(&val).map_err(|e| e.into())
40    }
41
42    fn from_custom(obj: Self) -> Self::Builtin {
43        obj.to_string()
44    }
45}