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
//! A persistence error.

use crate::prelude::*;

/// A persistence error.
#[derive(Debug, Error)]
pub enum PersistenceError {
    #[cfg(not(target_family = "wasm"))]
    #[error("{0}")]
    Filesystem(
        #[from]
        #[source]
        std::io::Error,
    ),

    #[cfg(target_family = "wasm")]
    #[error("{0}")]
    Browser(
        #[from]
        #[source]
        gloo_storage::errors::StorageError,
    ),

    #[cfg(any(
        feature = "ini",
        feature = "json",
        feature = "ron",
        feature = "toml",
        feature = "yaml"
    ))]
    #[error("{0}")]
    Encoding(
        #[from]
        #[source]
        std::str::Utf8Error,
    ),

    #[cfg(feature = "bincode")]
    #[error("{0}")]
    BincodeDeserialization(#[source] bincode::Error),
    #[cfg(feature = "bincode")]
    #[error("{0}")]
    BincodeSerialization(#[source] bincode::Error),

    #[cfg(feature = "ini")]
    #[error("{0}")]
    IniDeserialization(#[source] serde_ini::de::Error),
    #[cfg(feature = "ini")]
    #[error("{0}")]
    IniSerialization(#[source] serde_ini::ser::Error),

    #[cfg(feature = "json")]
    #[error("{0}")]
    JsonDeserialization(#[source] serde_json::Error),
    #[cfg(feature = "json")]
    #[error("{0}")]
    JsonSerialization(#[source] serde_json::Error),

    #[cfg(feature = "ron")]
    #[error("{0}")]
    RonDeserialization(#[source] ron::Error),
    #[cfg(feature = "ron")]
    #[error("{0}")]
    RonSerialization(#[source] ron::Error),

    #[cfg(feature = "toml")]
    #[error("{0}")]
    TomlDeserialization(#[source] toml::de::Error),
    #[cfg(feature = "toml")]
    #[error("{0}")]
    TomlSerialization(#[source] toml::ser::Error),

    #[cfg(feature = "yaml")]
    #[error("{0}")]
    YamlDeserialization(#[source] serde_yaml::Error),
    #[cfg(feature = "yaml")]
    #[error("{0}")]
    YamlSerialization(#[source] serde_yaml::Error),
}

impl PersistenceError {
    pub fn is_serde(&self) -> bool {
        match self {
            #[cfg(not(target_family = "wasm"))]
            PersistenceError::Filesystem(_) => false,
            #[cfg(target_family = "wasm")]
            PersistenceError::Browser(error) => {
                matches!(error, gloo_storage::errors::StorageError::SerdeError(_))
            },

            #[cfg(any(
                feature = "bincode",
                feature = "ini",
                feature = "json",
                feature = "ron",
                feature = "toml",
                feature = "yaml",
            ))]
            _ => true,
        }
    }
}