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
use crate::error::DriverAdapterError;
use napi::{bindgen_prelude::FromNapiValue, Env, JsUnknown, NapiValue};

impl FromNapiValue for DriverAdapterError {
    unsafe fn from_napi_value(napi_env: napi::sys::napi_env, napi_val: napi::sys::napi_value) -> napi::Result<Self> {
        let env = Env::from_raw(napi_env);
        let value = JsUnknown::from_raw(napi_env, napi_val)?;
        env.from_js_value(value)
    }
}

/// Wrapper for JS-side result type.
/// This Napi-specific implementation has the same shape and API as the Wasm implementation,
/// but it asks for a `FromNapiValue` bound on the generic type.
/// The duplication is needed as it's currently impossible to have target-specific generic bounds in Rust.
pub(crate) enum AdapterResult<T>
where
    T: FromNapiValue,
{
    Ok(T),
    Err(DriverAdapterError),
}

impl<T> AdapterResult<T>
where
    T: FromNapiValue,
{
    fn from_js_unknown(unknown: JsUnknown) -> napi::Result<Self> {
        let object = unknown.coerce_to_object()?;
        let ok: bool = object.get_named_property("ok")?;
        if ok {
            let value: JsUnknown = object.get_named_property("value")?;
            return Ok(Self::Ok(T::from_unknown(value)?));
        }

        let error = object.get_named_property("error")?;
        Ok(Self::Err(error))
    }
}

impl<T> FromNapiValue for AdapterResult<T>
where
    T: FromNapiValue,
{
    unsafe fn from_napi_value(napi_env: napi::sys::napi_env, napi_val: napi::sys::napi_value) -> napi::Result<Self> {
        Self::from_js_unknown(JsUnknown::from_raw(napi_env, napi_val)?)
    }
}

impl<T> From<AdapterResult<T>> for quaint::Result<T>
where
    T: FromNapiValue,
{
    fn from(value: AdapterResult<T>) -> Self {
        match value {
            AdapterResult::Ok(result) => Ok(result),
            AdapterResult::Err(error) => Err(error.into()),
        }
    }
}