-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathwasm.rs
More file actions
94 lines (76 loc) · 2.78 KB
/
Copy pathwasm.rs
File metadata and controls
94 lines (76 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//! The wasm module holds serialization/encoding needed wasm bindings for
//! any types related to InviteKeyEnvelope. This means base64url for the
//! InviteKeyData type, and Bitwarden EncString text format (`"2.iv|data|mac"`)
//! for the InviteKeyEnvelope type. In order to minimize complexity, the actual
//! encoding/decoding is limited to the `From<String>` and `FromStr`
//! implementations. All other serialization goes through String to simplify
//! maintenance.
use std::str::FromStr;
use wasm_bindgen::convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi};
use crate::{InviteKeyBundleError, InviteKeyData, InviteKeyEnvelope};
impl wasm_bindgen::describe::WasmDescribe for InviteKeyData {
fn describe() {
<String as wasm_bindgen::describe::WasmDescribe>::describe();
}
}
impl FromWasmAbi for InviteKeyData {
type Abi = <String as FromWasmAbi>::Abi;
unsafe fn from_abi(abi: Self::Abi) -> Self {
use wasm_bindgen::UnwrapThrowExt;
let string = unsafe { String::from_abi(abi) };
InviteKeyData::from_str(&string).unwrap_throw()
}
}
impl OptionFromWasmAbi for InviteKeyData {
fn is_none(abi: &Self::Abi) -> bool {
<String as OptionFromWasmAbi>::is_none(abi)
}
}
impl IntoWasmAbi for InviteKeyData {
type Abi = <String as IntoWasmAbi>::Abi;
fn into_abi(self) -> Self::Abi {
String::from(&self).into_abi()
}
}
impl TryFrom<wasm_bindgen::JsValue> for InviteKeyData {
type Error = InviteKeyBundleError;
fn try_from(value: wasm_bindgen::JsValue) -> Result<Self, Self::Error> {
let string = value
.as_string()
.ok_or(InviteKeyBundleError::DecodingFailed)?;
Self::from_str(&string)
}
}
impl wasm_bindgen::describe::WasmDescribe for InviteKeyEnvelope {
fn describe() {
<String as wasm_bindgen::describe::WasmDescribe>::describe();
}
}
impl FromWasmAbi for InviteKeyEnvelope {
type Abi = <String as FromWasmAbi>::Abi;
unsafe fn from_abi(abi: Self::Abi) -> Self {
use wasm_bindgen::UnwrapThrowExt;
let string = unsafe { String::from_abi(abi) };
InviteKeyEnvelope::from_str(&string).unwrap_throw()
}
}
impl OptionFromWasmAbi for InviteKeyEnvelope {
fn is_none(abi: &Self::Abi) -> bool {
<String as OptionFromWasmAbi>::is_none(abi)
}
}
impl IntoWasmAbi for InviteKeyEnvelope {
type Abi = <String as IntoWasmAbi>::Abi;
fn into_abi(self) -> Self::Abi {
String::from(&self).into_abi()
}
}
impl TryFrom<wasm_bindgen::JsValue> for InviteKeyEnvelope {
type Error = InviteKeyBundleError;
fn try_from(value: wasm_bindgen::JsValue) -> Result<Self, Self::Error> {
let string = value
.as_string()
.ok_or(InviteKeyBundleError::DecodingFailed)?;
Self::from_str(&string)
}
}