34 lines
815 B
Rust
34 lines
815 B
Rust
use gloo::storage::{LocalStorage, Storage, errors::StorageError};
|
|
use serde::{Deserialize, Serialize};
|
|
use werewolves_proto::{game::GameSettings, message::PublicIdentity, player::PlayerId};
|
|
|
|
type Result<T> = core::result::Result<T, StorageError>;
|
|
|
|
pub trait StorageKey: for<'a> Deserialize<'a> + Serialize {
|
|
const KEY: &str;
|
|
|
|
fn load_from_storage() -> Result<Self> {
|
|
LocalStorage::get(Self::KEY)
|
|
}
|
|
|
|
fn save_to_storage(&self) -> Result<()> {
|
|
LocalStorage::set(Self::KEY, self)
|
|
}
|
|
|
|
fn delete() {
|
|
LocalStorage::delete(Self::KEY);
|
|
}
|
|
}
|
|
|
|
impl StorageKey for PlayerId {
|
|
const KEY: &str = "ww_player_id";
|
|
}
|
|
|
|
impl StorageKey for PublicIdentity {
|
|
const KEY: &str = "ww_public_identity";
|
|
}
|
|
|
|
impl StorageKey for GameSettings {
|
|
const KEY: &str = "game_settings";
|
|
}
|