bdkffi/
macros.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#[macro_export]
macro_rules! impl_from_core_type {
    ($core_type:ident, $ffi_type:ident) => {
        impl From<$core_type> for $ffi_type {
            fn from(core_type: $core_type) -> Self {
                $ffi_type(core_type)
            }
        }
    };
}

#[macro_export]
macro_rules! impl_into_core_type {
    ($ffi_type:ident, $core_type:ident) => {
        impl From<$ffi_type> for $core_type {
            fn from(ffi_type: $ffi_type) -> Self {
                ffi_type.0
            }
        }
    };
}

#[macro_export]
macro_rules! impl_hash_like {
    ($ffi_type:ident, $core_type:ident) => {
        #[uniffi::export]
        impl $ffi_type {
            /// Construct a hash-like type from 32 bytes.
            #[uniffi::constructor]
            pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, HashParseError> {
                let hash_like: $core_type = deserialize(&bytes).map_err(|_| {
                    let len = bytes.len() as u32;
                    HashParseError::InvalidHash { len }
                })?;
                Ok(Self(hash_like))
            }

            /// Construct a hash-like type from a hex string.
            #[uniffi::constructor]
            pub fn from_string(hex: String) -> Result<Self, HashParseError> {
                hex.parse::<$core_type>()
                    .map(Self)
                    .map_err(|_| HashParseError::InvalidHexString { hex })
            }

            /// Serialize this type into a 32 byte array.
            pub fn serialize(&self) -> Vec<u8> {
                serialize(&self.0)
            }
        }

        impl std::fmt::Display for $ffi_type {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                self.0.fmt(f)
            }
        }
    };
}