1#[macro_export]
2macro_rules! impl_from_core_type {
3 ($core_type:ident, $ffi_type:ident) => {
4 impl From<$core_type> for $ffi_type {
5 fn from(core_type: $core_type) -> Self {
6 $ffi_type(core_type)
7 }
8 }
9 };
10}
11
12#[macro_export]
13macro_rules! impl_into_core_type {
14 ($ffi_type:ident, $core_type:ident) => {
15 impl From<$ffi_type> for $core_type {
16 fn from(ffi_type: $ffi_type) -> Self {
17 ffi_type.0
18 }
19 }
20 };
21}
22
23#[macro_export]
24macro_rules! impl_hash_like {
25 ($ffi_type:ident, $core_type:ident) => {
26 #[uniffi::export]
27 impl $ffi_type {
28 #[uniffi::constructor]
30 pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, HashParseError> {
31 let hash_like: $core_type = deserialize(&bytes).map_err(|_| {
32 let len = bytes.len() as u32;
33 HashParseError::InvalidHash { len }
34 })?;
35 Ok(Self(hash_like))
36 }
37
38 #[uniffi::constructor]
40 pub fn from_string(hex: String) -> Result<Self, HashParseError> {
41 hex.parse::<$core_type>()
42 .map(Self)
43 .map_err(|_| HashParseError::InvalidHexString { hex })
44 }
45
46 pub fn serialize(&self) -> Vec<u8> {
48 serialize(&self.0)
49 }
50 }
51
52 impl std::fmt::Display for $ffi_type {
53 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54 self.0.fmt(f)
55 }
56 }
57 };
58}