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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use js_sys::Object;
use wasm_bindgen::{JsCast, JsValue};
use web_sys::IdbCursorWithValue;
use crate::{CursorDirection, Error, StoreRequest};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Cursor {
inner: IdbCursorWithValue,
}
impl Cursor {
pub fn source(&self) -> Object {
self.inner.source()
}
pub fn direction(&self) -> Result<CursorDirection, Error> {
self.inner.direction().try_into()
}
pub fn key(&self) -> Result<JsValue, Error> {
self.inner.key().map_err(Error::CursorKeyNotFound)
}
pub fn primary_key(&self) -> Result<JsValue, Error> {
self.inner
.primary_key()
.map_err(Error::CursorPrimaryKeyNotFound)
}
pub fn value(&self) -> Result<JsValue, Error> {
self.inner.value().map_err(Error::CursorValueNotFound)
}
pub fn request(&self) -> StoreRequest {
self.inner.request().into()
}
pub fn advance(&self, count: u32) -> Result<(), Error> {
self.inner
.advance(count)
.map_err(Error::CursorAdvanceFailed)
}
pub fn next(&self, key: Option<&JsValue>) -> Result<(), Error> {
match key {
None => self.inner.continue_().map_err(Error::CursorContinueFailed),
Some(key) => self
.inner
.continue_with_key(key)
.map_err(Error::CursorContinueFailed),
}
}
pub fn next_primary_key(&self, key: &JsValue, primary_key: &JsValue) -> Result<(), Error> {
self.inner
.continue_primary_key(key, primary_key)
.map_err(Error::CursorContinueFailed)
}
pub fn update(&self, value: &JsValue) -> Result<StoreRequest, Error> {
self.inner
.update(value)
.map(Into::into)
.map_err(Error::UpdateFailed)
}
pub fn delete(&self) -> Result<StoreRequest, Error> {
self.inner
.delete()
.map(Into::into)
.map_err(Error::DeleteFailed)
}
}
impl From<IdbCursorWithValue> for Cursor {
fn from(inner: IdbCursorWithValue) -> Self {
Self { inner }
}
}
impl From<Cursor> for IdbCursorWithValue {
fn from(cursor: Cursor) -> Self {
cursor.inner
}
}
impl TryFrom<JsValue> for Cursor {
type Error = Error;
fn try_from(value: JsValue) -> Result<Self, Self::Error> {
value
.dyn_into::<IdbCursorWithValue>()
.map(Into::into)
.map_err(|value| Error::UnexpectedJsType("IdbCursorWithValue", value))
}
}
impl From<Cursor> for JsValue {
fn from(cursor: Cursor) -> Self {
cursor.inner.into()
}
}