floating_ui/floating_ui/core/
promisable.rs

1use js_sys::Promise;
2use wasm_bindgen::JsValue;
3
4use crate::{Error, Result};
5
6#[derive(Clone)]
7pub enum Promisable<T> {
8    T(T),
9    Promise(Promise),
10}
11
12impl<T> Promisable<T> {
13    pub fn from_promise(promise: Promise) -> Self {
14        Self::Promise(promise)
15    }
16}
17
18impl<T> From<T> for Promisable<T> {
19    fn from(t: T) -> Self {
20        Self::T(t)
21    }
22}
23
24impl<T> Into<JsValue> for Promisable<T>
25where
26    T: Into<JsValue>,
27{
28    fn into(self) -> JsValue {
29        match self {
30            Promisable::T(t) => t.into(),
31            Promisable::Promise(promise) => promise.into(),
32        }
33    }
34}
35
36impl<T> Promisable<T> {
37    pub fn try_into_js_value(self) -> Result<JsValue>
38    where
39        T: TryInto<JsValue, Error = Error>,
40    {
41        match self {
42            Promisable::T(t) => t.try_into(),
43            Promisable::Promise(promise) => Ok(promise.into()),
44        }
45    }
46}