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
use wasm_bindgen::JsValue;
use web_sys::IdbRequestReadyState;

use crate::Error;

/// State of a request.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RequestReadyState {
    /// Request is still ongoing.
    Pending,
    /// Request has already completed.
    Done,
}

impl From<RequestReadyState> for IdbRequestReadyState {
    fn from(state: RequestReadyState) -> Self {
        match state {
            RequestReadyState::Pending => IdbRequestReadyState::Pending,
            RequestReadyState::Done => IdbRequestReadyState::Done,
        }
    }
}

impl TryFrom<IdbRequestReadyState> for RequestReadyState {
    type Error = Error;

    fn try_from(value: IdbRequestReadyState) -> Result<Self, Self::Error> {
        match value {
            IdbRequestReadyState::Pending => Ok(RequestReadyState::Pending),
            IdbRequestReadyState::Done => Ok(RequestReadyState::Done),
            _ => Err(Error::InvalidReqeustReadyState),
        }
    }
}

impl TryFrom<JsValue> for RequestReadyState {
    type Error = Error;

    fn try_from(value: JsValue) -> Result<Self, Self::Error> {
        IdbRequestReadyState::from_js_value(&value)
            .ok_or(Error::InvalidCursorDirection)?
            .try_into()
    }
}

impl From<RequestReadyState> for JsValue {
    fn from(direction: RequestReadyState) -> Self {
        let inner: IdbRequestReadyState = direction.into();
        inner.into()
    }
}