core_crypto_ffi/
lib.rs

1// Wire
2// Copyright (C) 2022 Wire Swiss GmbH
3
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see http://www.gnu.org/licenses/.
16
17#[macro_export]
18macro_rules! proteus_impl {
19    ($errcode_dest:expr => $body:block or throw $err_type:ty) => {{
20        cfg_if::cfg_if! {
21            if #[cfg(feature = "proteus")] {
22                #[allow(clippy::redundant_closure_call)]
23                let result = (async move { $body }).await;
24
25                cfg_if::cfg_if! {
26                    if #[cfg(target_family = "wasm")] {
27                        if let Err(CoreCryptoError($crate::InternalError::ProteusError(err))) = &result {
28                            let err_code = err.error_code();
29                            let mut ec = $errcode_dest.write().await;
30                            *ec = err_code;
31                        }
32
33                        result
34                    } else {
35                        if let Err(CoreCryptoError::Proteus(err)) = &result {
36                            let err_code = err.error_code();
37                            $errcode_dest.store(err_code, std::sync::atomic::Ordering::SeqCst);
38                        }
39
40                        result
41                    }
42                }
43            } else {
44                return <$err_type>::Err(core_crypto::CryptoError::ProteusSupportNotEnabled("proteus".into()).into());
45            }
46        }
47    }};
48    ($body:block or throw $err_type:ty) => {{
49        cfg_if::cfg_if! {
50            if #[cfg(feature = "proteus")] {
51                $body
52            } else {
53                return <$err_type>::Err(core_crypto::CryptoError::ProteusSupportNotEnabled("proteus".into()).into());
54            }
55        }
56    }};
57
58    ($body:block) => {
59        proteus_impl!($body or throw ::std::result::Result<_, _>)
60    };
61
62    ($errcode_dest:expr => $body:block) => {
63        proteus_impl!($errcode_dest => $body or throw ::std::result::Result<_, _>)
64    };
65}
66
67cfg_if::cfg_if! {
68    if #[cfg(target_family = "wasm")] {
69        mod wasm;
70        pub use self::wasm::*;
71    } else {
72        uniffi::setup_scaffolding!("core_crypto_ffi");
73
74        mod generic;
75        pub use self::generic::*;
76    }
77}
78
79#[cfg(doc)]
80pub mod bindings;