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
61
62
63
64
65
66
67
68
69
use wasm_bindgen::JsValue;
use web_sys::IdbCursorDirection;

use crate::Error;

/// Specifies the cursor direction.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CursorDirection {
    /// `Next` causes the cursor to be opened at the start of the source. When iterated, the cursor yields all records,
    /// including duplicates, in monotonically increasing order of keys.
    Next,
    /// `NextUnique` causes the cursor to be opened at the start of the source. When iterated, the cursor does not yield
    /// records with the same key, but otherwise yields all records, in monotonically increasing order of keys.
    NextUnique,
    /// `Prev` causes the cursor to be opened at the end of the source. When iterated, the cursor yields all records,
    /// including duplicates, in monotonically decreasing order of keys.
    Prev,
    /// `PrevUnique` causes the cursor to be opened at the end of the source. When iterated, the cursor does not yield
    /// records with the same key, but otherwise yields all records, in monotonically decreasing order of keys.
    PrevUnique,
}

impl Default for CursorDirection {
    fn default() -> Self {
        CursorDirection::Next
    }
}

impl TryFrom<IdbCursorDirection> for CursorDirection {
    type Error = Error;

    fn try_from(direction: IdbCursorDirection) -> Result<Self, Self::Error> {
        match direction {
            IdbCursorDirection::Next => Ok(CursorDirection::Next),
            IdbCursorDirection::Nextunique => Ok(CursorDirection::NextUnique),
            IdbCursorDirection::Prev => Ok(CursorDirection::Prev),
            IdbCursorDirection::Prevunique => Ok(CursorDirection::PrevUnique),
            _ => Err(Error::InvalidCursorDirection),
        }
    }
}

impl From<CursorDirection> for IdbCursorDirection {
    fn from(direction: CursorDirection) -> Self {
        match direction {
            CursorDirection::Next => IdbCursorDirection::Next,
            CursorDirection::NextUnique => IdbCursorDirection::Nextunique,
            CursorDirection::Prev => IdbCursorDirection::Prev,
            CursorDirection::PrevUnique => IdbCursorDirection::Prevunique,
        }
    }
}

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

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

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