1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3#![allow(renamed_and_removed_lints)] #![allow(unknown_lints)] #![warn(missing_docs)]
7#![warn(noop_method_call)]
8#![warn(unreachable_pub)]
9#![warn(clippy::all)]
10#![deny(clippy::await_holding_lock)]
11#![deny(clippy::cargo_common_metadata)]
12#![deny(clippy::cast_lossless)]
13#![deny(clippy::checked_conversions)]
14#![warn(clippy::cognitive_complexity)]
15#![deny(clippy::debug_assert_with_mut_call)]
16#![deny(clippy::exhaustive_enums)]
17#![deny(clippy::exhaustive_structs)]
18#![deny(clippy::expl_impl_clone_on_copy)]
19#![deny(clippy::fallible_impl_from)]
20#![deny(clippy::implicit_clone)]
21#![deny(clippy::large_stack_arrays)]
22#![warn(clippy::manual_ok_or)]
23#![deny(clippy::missing_docs_in_private_items)]
24#![warn(clippy::needless_borrow)]
25#![warn(clippy::needless_pass_by_value)]
26#![warn(clippy::option_option)]
27#![deny(clippy::print_stderr)]
28#![deny(clippy::print_stdout)]
29#![warn(clippy::rc_buffer)]
30#![deny(clippy::ref_option_ref)]
31#![warn(clippy::semicolon_if_nothing_returned)]
32#![warn(clippy::trait_duplication_in_bounds)]
33#![deny(clippy::unchecked_time_subtraction)]
34#![deny(clippy::unnecessary_wraps)]
35#![warn(clippy::unseparated_literal_suffix)]
36#![deny(clippy::unwrap_used)]
37#![deny(clippy::mod_module_files)]
38#![allow(clippy::let_unit_value)] #![allow(clippy::uninlined_format_args)]
40#![allow(clippy::significant_drop_in_scrutinee)] #![allow(clippy::result_large_err)] #![allow(clippy::needless_raw_string_hashes)] #![allow(clippy::needless_lifetimes)] #![allow(mismatched_lifetime_syntaxes)] #![allow(clippy::collapsible_if)] #![deny(clippy::unused_async)]
47#![cfg_attr(not(all(feature = "experimental", feature = "full")), allow(unused))]
51
52mod err;
53#[cfg(not(target_arch = "wasm32"))]
54mod fs;
55mod fs_mistrust_error_ext;
56mod handle;
57pub mod hsnickname;
58mod load_store;
59pub mod slug;
60#[cfg(feature = "testing")]
61mod testing;
62
63#[cfg(feature = "state-dir")]
64pub mod state_dir;
65
66use serde::{Deserialize, Serialize, de::DeserializeOwned};
67use std::sync::Arc;
68
69type Result<T> = std::result::Result<T, crate::Error>;
71
72pub use err::{Error, ErrorSource};
73#[cfg(not(target_arch = "wasm32"))]
74pub use fs::FsStateMgr;
75pub use fs_mistrust_error_ext::FsMistrustErrorExt;
76pub use handle::{DynStorageHandle, StorageHandle};
77pub use serde_json::Value as JsonValue;
78#[cfg(feature = "testing")]
79pub use testing::TestingStateMgr;
80
81pub trait StateMgr: Clone {
92 fn load<D>(&self, key: &str) -> Result<Option<D>>
96 where
97 D: DeserializeOwned;
98 fn store<S>(&self, key: &str, val: &S) -> Result<()>
102 where
103 S: Serialize;
104 fn can_store(&self) -> bool;
109
110 fn try_lock(&self) -> Result<LockStatus>;
118
119 fn unlock(&self) -> Result<()>;
122
123 fn create_handle<T>(self, key: impl Into<String>) -> DynStorageHandle<T>
126 where
127 Self: Send + Sync + Sized + 'static,
128 T: Serialize + DeserializeOwned + 'static,
129 {
130 Arc::new(handle::StorageHandleImpl::new(self, key.into()))
131 }
132}
133
134#[allow(clippy::exhaustive_enums)]
136#[derive(Debug, Copy, Clone, Eq, PartialEq)]
137#[must_use]
138pub enum LockStatus {
139 NoLock,
141 AlreadyHeld,
143 NewlyAcquired,
145}
146
147impl LockStatus {
148 pub fn held(&self) -> bool {
150 !matches!(self, LockStatus::NoLock)
151 }
152}
153
154#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
159#[serde(untagged)]
160#[allow(clippy::exhaustive_enums)]
161pub enum Futureproof<T> {
162 Understandable(T),
164 Unknown(JsonValue),
166}
167
168impl<T> Futureproof<T> {
169 pub fn into_option(self) -> Option<T> {
171 match self {
172 Futureproof::Understandable(x) => Some(x),
173 Futureproof::Unknown(_) => None,
174 }
175 }
176}
177
178impl<T> From<T> for Futureproof<T> {
179 fn from(inner: T) -> Self {
180 Self::Understandable(inner)
181 }
182}