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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#![deny(missing_docs)]
#![warn(noop_method_call)]
#![deny(unreachable_pub)]
#![deny(clippy::await_holding_lock)]
#![deny(clippy::cargo_common_metadata)]
#![deny(clippy::cast_lossless)]
#![deny(clippy::checked_conversions)]
#![warn(clippy::clone_on_ref_ptr)]
#![warn(clippy::cognitive_complexity)]
#![deny(clippy::debug_assert_with_mut_call)]
#![deny(clippy::exhaustive_enums)]
#![deny(clippy::exhaustive_structs)]
#![deny(clippy::expl_impl_clone_on_copy)]
#![deny(clippy::fallible_impl_from)]
#![deny(clippy::implicit_clone)]
#![deny(clippy::large_stack_arrays)]
#![warn(clippy::manual_ok_or)]
#![deny(clippy::missing_docs_in_private_items)]
#![deny(clippy::missing_panics_doc)]
#![warn(clippy::needless_borrow)]
#![warn(clippy::needless_pass_by_value)]
#![warn(clippy::option_option)]
#![warn(clippy::rc_buffer)]
#![deny(clippy::ref_option_ref)]
#![warn(clippy::semicolon_if_nothing_returned)]
#![warn(clippy::trait_duplication_in_bounds)]
#![deny(clippy::unnecessary_wraps)]
#![warn(clippy::unseparated_literal_suffix)]
#![deny(clippy::unwrap_used)]
#[cfg(not(target_arch = "wasm32"))]
mod fs;
mod handle;
#[cfg(feature = "testing")]
mod testing;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::sync::Arc;
type Result<T> = std::result::Result<T, crate::Error>;
#[cfg(not(target_arch = "wasm32"))]
pub use fs::FsStateMgr;
pub use handle::{DynStorageHandle, StorageHandle};
pub use serde_json::Value as JsonValue;
#[cfg(feature = "testing")]
pub use testing::TestingStateMgr;
pub trait StateMgr: Clone {
fn load<D>(&self, key: &str) -> Result<Option<D>>
where
D: DeserializeOwned;
fn store<S>(&self, key: &str, val: &S) -> Result<()>
where
S: Serialize;
fn can_store(&self) -> bool;
fn try_lock(&self) -> Result<LockStatus>;
fn create_handle<T>(self, key: impl Into<String>) -> DynStorageHandle<T>
where
Self: Send + Sync + Sized + 'static,
T: Serialize + DeserializeOwned + 'static,
{
Arc::new(handle::StorageHandleImpl::new(self, key.into()))
}
}
#[allow(clippy::exhaustive_enums)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum LockStatus {
NoLock,
AlreadyHeld,
NewlyAcquired,
}
impl LockStatus {
pub fn held(&self) -> bool {
!matches!(self, LockStatus::NoLock)
}
}
#[derive(thiserror::Error, Debug, Clone)]
#[non_exhaustive]
pub enum Error {
#[error("IO error")]
IoError(#[source] Arc<std::io::Error>),
#[error("Storage not locked")]
NoLock,
#[error("JSON serialization error")]
JsonError(#[source] Arc<serde_json::Error>),
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Error {
Error::IoError(Arc::new(e))
}
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Error {
Error::JsonError(Arc::new(e))
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(untagged)]
#[allow(clippy::exhaustive_enums)]
pub enum Futureproof<T> {
Understandable(T),
Unknown(JsonValue),
}
impl<T> Futureproof<T> {
pub fn into_option(self) -> Option<T> {
match self {
Futureproof::Understandable(x) => Some(x),
Futureproof::Unknown(_) => None,
}
}
}
impl<T> From<T> for Futureproof<T> {
fn from(inner: T) -> Self {
Self::Understandable(inner)
}
}