Skip to main content

bitwarden_organization_crypto/
wasm.rs

1//! The wasm module holds serialization/encoding needed wasm bindings for
2//! any types related to Invite. In order to minimize complexity, the actual
3//! encoding/decoding is limited to the `From<String>` and `FromStr`
4//! implementations. All other serialization goes through String to simplify
5//! maintenance.
6use std::str::FromStr;
7
8use wasm_bindgen::convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi};
9
10use crate::invite::{Invite, InviteKeyBundleError, InviteSecret};
11
12impl wasm_bindgen::describe::WasmDescribe for Invite {
13    fn describe() {
14        <String as wasm_bindgen::describe::WasmDescribe>::describe();
15    }
16}
17
18impl FromWasmAbi for Invite {
19    type Abi = <String as FromWasmAbi>::Abi;
20
21    unsafe fn from_abi(abi: Self::Abi) -> Self {
22        use wasm_bindgen::UnwrapThrowExt;
23        let string = unsafe { String::from_abi(abi) };
24        Invite::from_str(&string).unwrap_throw()
25    }
26}
27
28impl OptionFromWasmAbi for Invite {
29    fn is_none(abi: &Self::Abi) -> bool {
30        <String as OptionFromWasmAbi>::is_none(abi)
31    }
32}
33
34impl IntoWasmAbi for Invite {
35    type Abi = <String as IntoWasmAbi>::Abi;
36
37    fn into_abi(self) -> Self::Abi {
38        String::from(&self).into_abi()
39    }
40}
41
42impl TryFrom<wasm_bindgen::JsValue> for Invite {
43    type Error = InviteKeyBundleError;
44
45    fn try_from(value: wasm_bindgen::JsValue) -> Result<Self, Self::Error> {
46        let string = value
47            .as_string()
48            .ok_or(InviteKeyBundleError::DecodingFailed)?;
49        Self::from_str(&string)
50    }
51}
52
53impl wasm_bindgen::describe::WasmDescribe for InviteSecret {
54    fn describe() {
55        <String as wasm_bindgen::describe::WasmDescribe>::describe();
56    }
57}
58
59impl FromWasmAbi for InviteSecret {
60    type Abi = <String as FromWasmAbi>::Abi;
61
62    unsafe fn from_abi(abi: Self::Abi) -> Self {
63        use wasm_bindgen::UnwrapThrowExt;
64        let string = unsafe { String::from_abi(abi) };
65        InviteSecret::from_str(&string).unwrap_throw()
66    }
67}
68
69impl OptionFromWasmAbi for InviteSecret {
70    fn is_none(abi: &Self::Abi) -> bool {
71        <String as OptionFromWasmAbi>::is_none(abi)
72    }
73}
74
75impl IntoWasmAbi for InviteSecret {
76    type Abi = <String as IntoWasmAbi>::Abi;
77
78    fn into_abi(self) -> Self::Abi {
79        String::from(&self).into_abi()
80    }
81}
82
83impl TryFrom<wasm_bindgen::JsValue> for InviteSecret {
84    type Error = InviteKeyBundleError;
85
86    fn try_from(value: wasm_bindgen::JsValue) -> Result<Self, Self::Error> {
87        let string = value
88            .as_string()
89            .ok_or(InviteKeyBundleError::DecodingFailed)?;
90        Self::from_str(&string)
91    }
92}
93
94// Returning these types from `async` `#[wasm_bindgen]` methods (which resolve to JS Promises)
95// requires `Into<JsValue>`, not just `IntoWasmAbi`. Both delegate through their base64 string form.
96impl From<Invite> for wasm_bindgen::JsValue {
97    fn from(value: Invite) -> Self {
98        wasm_bindgen::JsValue::from_str(&String::from(&value))
99    }
100}
101
102impl From<InviteSecret> for wasm_bindgen::JsValue {
103    fn from(value: InviteSecret) -> Self {
104        wasm_bindgen::JsValue::from_str(&String::from(&value))
105    }
106}