idb_sys/cursor/
cursor_direction.rs

1use wasm_bindgen::JsValue;
2use web_sys::IdbCursorDirection;
3
4use crate::Error;
5
6/// Specifies the cursor direction.
7#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
8pub enum CursorDirection {
9    /// `Next` causes the cursor to be opened at the start of the source. When iterated, the cursor yields all records,
10    /// including duplicates, in monotonically increasing order of keys.
11    #[default]
12    Next,
13    /// `NextUnique` causes the cursor to be opened at the start of the source. When iterated, the cursor does not yield
14    /// records with the same key, but otherwise yields all records, in monotonically increasing order of keys.
15    NextUnique,
16    /// `Prev` causes the cursor to be opened at the end of the source. When iterated, the cursor yields all records,
17    /// including duplicates, in monotonically decreasing order of keys.
18    Prev,
19    /// `PrevUnique` causes the cursor to be opened at the end of the source. When iterated, the cursor does not yield
20    /// records with the same key, but otherwise yields all records, in monotonically decreasing order of keys.
21    PrevUnique,
22}
23
24impl TryFrom<IdbCursorDirection> for CursorDirection {
25    type Error = Error;
26
27    fn try_from(direction: IdbCursorDirection) -> Result<Self, Self::Error> {
28        match direction {
29            IdbCursorDirection::Next => Ok(CursorDirection::Next),
30            IdbCursorDirection::Nextunique => Ok(CursorDirection::NextUnique),
31            IdbCursorDirection::Prev => Ok(CursorDirection::Prev),
32            IdbCursorDirection::Prevunique => Ok(CursorDirection::PrevUnique),
33            _ => Err(Error::InvalidCursorDirection),
34        }
35    }
36}
37
38impl From<CursorDirection> for IdbCursorDirection {
39    fn from(direction: CursorDirection) -> Self {
40        match direction {
41            CursorDirection::Next => IdbCursorDirection::Next,
42            CursorDirection::NextUnique => IdbCursorDirection::Nextunique,
43            CursorDirection::Prev => IdbCursorDirection::Prev,
44            CursorDirection::PrevUnique => IdbCursorDirection::Prevunique,
45        }
46    }
47}
48
49impl TryFrom<JsValue> for CursorDirection {
50    type Error = Error;
51
52    fn try_from(value: JsValue) -> Result<Self, Self::Error> {
53        IdbCursorDirection::from_js_value(&value)
54            .ok_or(Error::InvalidCursorDirection)?
55            .try_into()
56    }
57}
58
59impl From<CursorDirection> for JsValue {
60    fn from(direction: CursorDirection) -> Self {
61        let inner: IdbCursorDirection = direction.into();
62        inner.into()
63    }
64}