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
//! Errors that can occur when working with IndexedDB.

use std::fmt;
use wasm_bindgen::JsValue;

/// An error that occurred when working with IndexedDB.
#[derive(Clone, PartialEq, Debug)]
pub enum Error {
    /// Accessing a Window has failed.
    WindowNotAvailable,
    /// IndexedDB is not supported by your browser.
    NotSupported(String),
    /// The database returned a generic error.
    Generic(String),
    /// The operation was canceled
    Canceled,
}

impl From<std::io::Error> for Error {
    fn from(other: std::io::Error) -> Self {
        Self::Generic(other.to_string())
    }
}

impl std::error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::WindowNotAvailable => "Accessing a Window has failed",
            Error::NotSupported(_) => "IndexedDB is not supported by your browser",
            Error::Generic(_) => "The database returned a generic error",
            Error::Canceled => "The operation was canceled",
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Error::WindowNotAvailable => write!(f, "Accessing a Window has failed"),
            Error::NotSupported(ref err) => {
                write!(f, "IndexedDB is not supported by your browser: {}", err,)
            }
            Error::Generic(ref err) => {
                write!(f, "Generic error: {}", err,)
            }
            Error::Canceled => write!(f, "The operation was canceled"),
        }
    }
}

pub(crate) fn io_err_string<T: ToString>(e: T) -> std::io::Error {
    std::io::Error::new(std::io::ErrorKind::Other, e.to_string())
}

pub(crate) fn io_err_jsvalue(e: JsValue) -> std::io::Error {
    std::io::Error::new(std::io::ErrorKind::Other, e.as_string().unwrap_or_default())
}

pub(crate) fn bad_cast_io_err(context: &str, v: JsValue) -> std::io::Error {
    io_err_string(format!("{} is a {:?}", context, v))
}
pub(crate) fn bad_cast_generic(context: &str, v: JsValue) -> Error {
    Error::Generic(format!("{} is a {:?}", context, v))
}