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
use wasm_bindgen::JsValue;
use web_sys::IdbTransactionMode;

use crate::Error;

/// Specifies the transaction mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransactionMode {
    /// The transaction is only allowed to read data.
    ReadOnly,
    /// The transaction is allowed to read, modify and delete data from existing object stores.
    ReadWrite,
    /// The transaction is allowed to read, modify and delete data from existing object stores, and can also create and
    /// remove object stores and indexes. This type of transaction can’t be manually created, but instead is created
    /// automatically when an `upgradeneeded` event is fired.
    VersionChange,
}

impl TryFrom<IdbTransactionMode> for TransactionMode {
    type Error = Error;

    fn try_from(value: IdbTransactionMode) -> Result<Self, Self::Error> {
        match value {
            IdbTransactionMode::Readonly => Ok(TransactionMode::ReadOnly),
            IdbTransactionMode::Readwrite => Ok(TransactionMode::ReadWrite),
            IdbTransactionMode::Versionchange => Ok(TransactionMode::VersionChange),
            _ => Err(Error::InvalidTransactionMode),
        }
    }
}

impl From<TransactionMode> for IdbTransactionMode {
    fn from(value: TransactionMode) -> Self {
        match value {
            TransactionMode::ReadOnly => IdbTransactionMode::Readonly,
            TransactionMode::ReadWrite => IdbTransactionMode::Readwrite,
            TransactionMode::VersionChange => IdbTransactionMode::Versionchange,
        }
    }
}

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

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

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