Skip to main content

bitwarden_ffi/
wire_macros.rs

1//! The `impl_wire_*` macros, which implement the wire traits.
2//!
3//! Defined outside that module so they exist without this crate's `wasm` feature. Like the
4//! attribute macros, each one gates what it generates on the calling crate's `wasm` feature, so a
5//! call site needs no `cfg` of its own.
6
7/// Implements the traits for a serde DTO, which crosses as `Ts<Self>`.
8///
9/// Used by `#[wasm_record]`; there is no reason to call it directly. `impl_wire_record!(T, input)`
10/// implements only [`FromWasm`](crate::FromWasm), for `#[wasm_record(input)]`.
11#[macro_export]
12macro_rules! impl_wire_record {
13    ($ty:ty, input) => {
14        #[cfg(feature = "wasm")]
15        impl $crate::FromWasm for $ty {
16            type Wire = $crate::Ts<$ty>;
17
18            fn from_wire(wire: Self::Wire) -> ::core::result::Result<Self, $crate::WireError> {
19                wire.to_rust().map_err($crate::WireError::from)
20            }
21        }
22    };
23    ($ty:ty) => {
24        $crate::impl_wire_record!($ty, input);
25
26        #[cfg(feature = "wasm")]
27        impl $crate::ToWasm for $ty {
28            type Wire = $crate::Ts<$ty>;
29
30            fn to_wire(self) -> ::core::result::Result<Self::Wire, $crate::WireError> {
31                $crate::Ts::from_rust(&self).map_err($crate::WireError::from)
32            }
33        }
34    };
35}
36
37/// Implements both traits for a type that crosses as itself: a `#[wasm_bindgen]` handle, an
38/// `extern "C"` type, or a primitive wasm_bindgen already understands.
39///
40/// `#[wasm_object]` and `#[wasm_import]` call this for the types they declare.
41#[macro_export]
42macro_rules! impl_wire_object {
43    ($($ty:ty),* $(,)?) => {
44        $(
45            #[cfg(feature = "wasm")]
46            impl $crate::FromWasm for $ty {
47                type Wire = $ty;
48
49                fn from_wire(wire: Self::Wire) -> ::core::result::Result<Self, $crate::WireError> {
50                    ::core::result::Result::Ok(wire)
51                }
52            }
53
54            #[cfg(feature = "wasm")]
55            impl $crate::ToWasm for $ty {
56                type Wire = $ty;
57
58                fn to_wire(self) -> ::core::result::Result<Self::Wire, $crate::WireError> {
59                    ::core::result::Result::Ok(self)
60                }
61            }
62        )*
63    };
64}
65
66/// Implements the traits for a type that crosses as a `String`, so a malformed string is a
67/// [`WireError::Parse`](crate::WireError::Parse) rather than a throw from inside `FromWasmAbi`.
68///
69/// - `impl_wire_string!(T)` parses with `FromStr` and formats with `Display`.
70/// - `impl_wire_string!(T, parse = f, format = g)` takes `f: FnOnce(String) -> Result<T, E>` and
71///   `g: FnOnce(T) -> String`.
72/// - `impl_wire_string!(T, parse = f)` implements only [`FromWasm`](crate::FromWasm), for a type
73///   JavaScript never receives.
74///
75/// Also implements `WasmDescribe` and, with a `format`, `IntoWasmAbi` through the same conversion,
76/// for `extern "C"` imports that pass the type to JavaScript. There is no `FromWasmAbi`: a value
77/// coming from JavaScript goes through [`FromWasm`](crate::FromWasm), so a malformed string is an
78/// `Err` rather than a throw.
79#[macro_export]
80macro_rules! impl_wire_string {
81    ($ty:ty $(,)?) => {
82        $crate::impl_wire_string!(
83            $ty,
84            parse = |wire: ::std::string::String| wire.parse::<$ty>(),
85            format = |value: $ty| ::std::string::ToString::to_string(&value),
86        );
87    };
88    ($ty:ty, parse = $parse:expr, format = $format:expr $(,)?) => {
89        $crate::impl_wire_string!($ty, parse = $parse);
90
91        #[cfg(feature = "wasm")]
92        impl $crate::ToWasm for $ty {
93            type Wire = ::std::string::String;
94
95            fn to_wire(self) -> ::core::result::Result<Self::Wire, $crate::WireError> {
96                ::core::result::Result::Ok($crate::format_wire_string(self, $format))
97            }
98        }
99
100        #[cfg(feature = "wasm")]
101        impl $crate::_macro::wasm_bindgen::convert::IntoWasmAbi for $ty {
102            type Abi =
103                <::std::string::String as $crate::_macro::wasm_bindgen::convert::IntoWasmAbi>::Abi;
104
105            fn into_abi(self) -> Self::Abi {
106                let wire = $crate::_macro::wasm_bindgen::UnwrapThrowExt::unwrap_throw(
107                    <$ty as $crate::ToWasm>::to_wire(self),
108                );
109                $crate::_macro::wasm_bindgen::convert::IntoWasmAbi::into_abi(wire)
110            }
111        }
112
113        #[cfg(feature = "wasm")]
114        impl $crate::_macro::wasm_bindgen::convert::OptionIntoWasmAbi for $ty {
115            fn none() -> Self::Abi {
116                <::std::string::String as $crate::_macro::wasm_bindgen::convert::OptionIntoWasmAbi>::none()
117            }
118        }
119    };
120    ($ty:ty, parse = $parse:expr $(,)?) => {
121        #[cfg(feature = "wasm")]
122        impl $crate::FromWasm for $ty {
123            type Wire = ::std::string::String;
124
125            fn from_wire(wire: Self::Wire) -> ::core::result::Result<Self, $crate::WireError> {
126                $crate::parse_wire_string(stringify!($ty), wire, $parse)
127            }
128        }
129
130        #[cfg(feature = "wasm")]
131        impl $crate::_macro::wasm_bindgen::describe::WasmDescribe for $ty {
132            fn describe() {
133                <::std::string::String as $crate::_macro::wasm_bindgen::describe::WasmDescribe>::describe();
134            }
135        }
136    };
137}