idb_sys/index/
index_params.rs

1use wasm_bindgen::{JsCast, JsValue};
2use web_sys::IdbIndexParameters;
3
4use crate::Error;
5
6/// Options when creating [`Index`](crate::Index).
7#[derive(Debug, Default, Clone, PartialEq, Eq)]
8pub struct IndexParams {
9    inner: IdbIndexParameters,
10}
11
12impl IndexParams {
13    /// Creates a new instance of [`IndexParams`]
14    pub fn new() -> Self {
15        Default::default()
16    }
17
18    /// Sets the `unique` flag.
19    pub fn unique(&mut self, unique: bool) -> &mut Self {
20        self.inner.unique(unique);
21        self
22    }
23
24    /// Sets the `multi_entry` flag.
25    pub fn multi_entry(&mut self, multi_entry: bool) -> &mut Self {
26        self.inner.multi_entry(multi_entry);
27        self
28    }
29}
30
31impl From<IdbIndexParameters> for IndexParams {
32    fn from(inner: IdbIndexParameters) -> Self {
33        Self { inner }
34    }
35}
36
37impl From<IndexParams> for IdbIndexParameters {
38    fn from(params: IndexParams) -> Self {
39        params.inner
40    }
41}
42
43impl TryFrom<JsValue> for IndexParams {
44    type Error = Error;
45
46    fn try_from(value: JsValue) -> Result<Self, Self::Error> {
47        value
48            .dyn_into::<IdbIndexParameters>()
49            .map(Into::into)
50            .map_err(|value| Error::UnexpectedJsType("IdbIndexParameters", value))
51    }
52}
53
54impl From<IndexParams> for JsValue {
55    fn from(value: IndexParams) -> Self {
56        value.inner.into()
57    }
58}