You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add wrappers for browser's storage interface. These will wrap around localStorage and sessionStorage and provide an idiomatic Rust API.
Motivation
It's hard to deal with web-sys directly and using storage in browser is a common case.
Detailed Explanation
We provide a trait with default implementations for dealing with web_sys::Storage. The web_sys::Storage instance to be used is provided by the raw method which must be implemented by the implementers of the trait. We use serde to serialize and deserialize data to and from JSON which is stored in the browser-provided storage.
pubstructJsError{pubname:String,pubmessage:String,/// Holds the value returned by calling `toString` on the error. This is used by `Display` impl.js_to_string:String,}#[derive(Debug, thiserror::Error)]pubenumStorageError{#[error("{0}")]SerdeError(#[from] serde_json::Error),#[error("key {0} not found")]KeyNotFound(String),#[error("{0}")]JsError(JsError),}pubtypeResult<T> = std::result::Result<T,StorageError>;pubtraitStorage{/// Get the raw [`web_sys::Storage`] instancefnraw() -> web_sys::Storage;/// Get the value for the specified keyfnget<T>(key:implAsRef<str>) -> Result<T>whereT:for<'de>Deserialize<'de>,{ ...}/// Get all the stored keys and their valuesfnget_all<T>() -> Result<T>whereT:for<'a>Deserialize<'a>,{ ...}/// Insert a value for the specified keyfnset<T>(key:implAsRef<str>,value:T) -> Result<()>whereT:Serialize,{ ...}/// Remove a key and it's stored valuefndelete(key:implAsRef<str>){ ...}/// Remove all the stored datafnclear(){ ...}/// Get the number of items storedfnlength() -> u32{ ...}}pubstructLocalStorage;implStorageforLocalStorage{fnraw() -> web_sys::Storage{ ...}}pubstructSessionStorage;implStorageforSessionStorage{fnraw() -> web_sys::Storage{ ...}}
Drawbacks, Rationale, and Alternatives
Alternative Approach
Provide an API with a single struct that's holds on to a web_sys::Storage instance and performs operations with that.
I just learnt that Web Storage API only covers localStorage and sessionStorage so this crate should cover only those too. I've removed mentions of other storage from the issue
Summary
Add wrappers for browser's storage interface. These will wrap around
localStorage
andsessionStorage
and provide an idiomatic Rust API.Motivation
It's hard to deal with
web-sys
directly and using storage in browser is a common case.Detailed Explanation
We provide a trait with default implementations for dealing with
web_sys::Storage
. Theweb_sys::Storage
instance to be used is provided by theraw
method which must be implemented by the implementers of the trait. We useserde
to serialize and deserialize data to and from JSON which is stored in the browser-provided storage.Drawbacks, Rationale, and Alternatives
Alternative Approach
Provide an API with a single struct that's holds on to a
web_sys::Storage
instance and performs operations with that.Unresolved Questions
None
The text was updated successfully, but these errors were encountered: