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 InviteKeyEnvelope. This means base64url for the
3//! InviteKeyData type, and  base64 for the InviteKey type. In order to minimize
4//! complexity, the actual  B64/B64Url encoding/decoding are limited to the
5//! `From<String>` and `FromStr`  implementations. All other serialization
6//! goes through String to simplify maintenance.
7use std::str::FromStr;
8
9use wasm_bindgen::convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi};
10
11use crate::{InviteKeyBundleError, InviteKeyData, InviteKeyEnvelope};
12
13#[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)]
14const TS_CUSTOM_TYPES: &'static str = r#"
15export type InviteKeyData = Tagged<string, "InviteKeyData">;
16"#;
17
18impl wasm_bindgen::describe::WasmDescribe for InviteKeyData {
19    fn describe() {
20        <String as wasm_bindgen::describe::WasmDescribe>::describe();
21    }
22}
23
24impl FromWasmAbi for InviteKeyData {
25    type Abi = <String as FromWasmAbi>::Abi;
26
27    unsafe fn from_abi(abi: Self::Abi) -> Self {
28        use wasm_bindgen::UnwrapThrowExt;
29        let string = unsafe { String::from_abi(abi) };
30        InviteKeyData::from_str(&string).unwrap_throw()
31    }
32}
33
34impl OptionFromWasmAbi for InviteKeyData {
35    fn is_none(abi: &Self::Abi) -> bool {
36        <String as OptionFromWasmAbi>::is_none(abi)
37    }
38}
39
40impl IntoWasmAbi for InviteKeyData {
41    type Abi = <String as IntoWasmAbi>::Abi;
42
43    fn into_abi(self) -> Self::Abi {
44        String::from(&self).into_abi()
45    }
46}
47
48impl TryFrom<wasm_bindgen::JsValue> for InviteKeyData {
49    type Error = InviteKeyBundleError;
50
51    fn try_from(value: wasm_bindgen::JsValue) -> Result<Self, Self::Error> {
52        let string = value
53            .as_string()
54            .ok_or(InviteKeyBundleError::DecodingFailed)?;
55        Self::from_str(&string)
56    }
57}
58
59#[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)]
60const TS_CUSTOM_TYPES: &'static str = r#"
61export type InviteKeyEnvelope = Tagged<string, "InviteKeyEnvelope">;
62"#;
63
64impl wasm_bindgen::describe::WasmDescribe for InviteKeyEnvelope {
65    fn describe() {
66        <String as wasm_bindgen::describe::WasmDescribe>::describe();
67    }
68}
69
70impl FromWasmAbi for InviteKeyEnvelope {
71    type Abi = <String as FromWasmAbi>::Abi;
72
73    unsafe fn from_abi(abi: Self::Abi) -> Self {
74        use wasm_bindgen::UnwrapThrowExt;
75        let string = unsafe { String::from_abi(abi) };
76        InviteKeyEnvelope::from_str(&string).unwrap_throw()
77    }
78}
79
80impl OptionFromWasmAbi for InviteKeyEnvelope {
81    fn is_none(abi: &Self::Abi) -> bool {
82        <String as OptionFromWasmAbi>::is_none(abi)
83    }
84}
85
86impl IntoWasmAbi for InviteKeyEnvelope {
87    type Abi = <String as IntoWasmAbi>::Abi;
88
89    fn into_abi(self) -> Self::Abi {
90        String::from(&self).into_abi()
91    }
92}
93
94impl TryFrom<wasm_bindgen::JsValue> for InviteKeyEnvelope {
95    type Error = InviteKeyBundleError;
96
97    fn try_from(value: wasm_bindgen::JsValue) -> Result<Self, Self::Error> {
98        let string = value
99            .as_string()
100            .ok_or(InviteKeyBundleError::DecodingFailed)?;
101        Self::from_str(&string)
102    }
103}