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, InviteKeyBundleError, InviteKeyData};
11
12impl wasm_bindgen::describe::WasmDescribe for InviteKeyData {
13    fn describe() {
14        <String as wasm_bindgen::describe::WasmDescribe>::describe();
15    }
16}
17
18impl FromWasmAbi for InviteKeyData {
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        InviteKeyData::from_str(&string).unwrap_throw()
25    }
26}
27
28impl OptionFromWasmAbi for InviteKeyData {
29    fn is_none(abi: &Self::Abi) -> bool {
30        <String as OptionFromWasmAbi>::is_none(abi)
31    }
32}
33
34impl IntoWasmAbi for InviteKeyData {
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 InviteKeyData {
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 Invite {
54    fn describe() {
55        <String as wasm_bindgen::describe::WasmDescribe>::describe();
56    }
57}
58
59impl FromWasmAbi for Invite {
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        Invite::from_str(&string).unwrap_throw()
66    }
67}
68
69impl OptionFromWasmAbi for Invite {
70    fn is_none(abi: &Self::Abi) -> bool {
71        <String as OptionFromWasmAbi>::is_none(abi)
72    }
73}
74
75impl IntoWasmAbi for Invite {
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 Invite {
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}