1//! Result of fallible computation that translates to/from JavaScript.
23use serde::{Deserialize, Serialize};
4use tsify_next::Tsify;
56/** A `Result`-like type that translates to/from JavaScript.
78In `wasm-bindgen`, returning a [`Result`] raises an exception in JavaScript when
9the `Err` variant is given:
1011<https://rustwasm.github.io/docs/wasm-bindgen/reference/types/result.html>
1213When an error should be handled in the UI, it is often more convenient to return
14an error value than to raise an exception. That's what this enum does.
15*/
16#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Tsify)]
17#[serde(tag = "tag", content = "content")]
18#[tsify(into_wasm_abi, from_wasm_abi)]
19pub enum JsResult<T, E> {
20/// Contains the success value
21Ok(T),
2223/// Contains the error value
24Err(E),
25}
2627impl<T, E> From<Result<T, E>> for JsResult<T, E> {
28fn from(value: Result<T, E>) -> Self {
29match value {
30 Result::Ok(x) => JsResult::Ok(x),
31 Result::Err(x) => JsResult::Err(x),
32 }
33 }
34}
3536impl<T> From<Option<T>> for JsResult<T, ()> {
37fn from(value: Option<T>) -> Self {
38match value {
39 Option::Some(x) => JsResult::Ok(x),
40 Option::None => JsResult::Err(()),
41 }
42 }
43}