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)
}
}
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()),
}
}
}