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
use wasm_bindgen::{JsCast, JsValue};
use web_sys::IdbIndexParameters;

use crate::Error;

/// Options when creating [`Index`](crate::Index).
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct IndexParams {
    inner: IdbIndexParameters,
}

impl IndexParams {
    /// Creates a new instance of [`IndexParams`]
    pub fn new() -> Self {
        Default::default()
    }

    /// Sets the `unique` flag.
    pub fn unique(&mut self, unique: bool) -> &mut Self {
        self.inner.unique(unique);
        self
    }

    /// Sets the `multi_entry` flag.
    pub fn multi_entry(&mut self, multi_entry: bool) -> &mut Self {
        self.inner.multi_entry(multi_entry);
        self
    }
}

impl From<IdbIndexParameters> for IndexParams {
    fn from(inner: IdbIndexParameters) -> Self {
        Self { inner }
    }
}

impl From<IndexParams> for IdbIndexParameters {
    fn from(params: IndexParams) -> Self {
        params.inner
    }
}

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

    fn try_from(value: JsValue) -> Result<Self, Self::Error> {
        value
            .dyn_into::<IdbIndexParameters>()
            .map(Into::into)
            .map_err(|value| Error::UnexpectedJsType("IdbIndexParameters", value))
    }
}

impl From<IndexParams> for JsValue {
    fn from(value: IndexParams) -> Self {
        value.inner.into()
    }
}