feat: make the web version fully working (#67)
Reviewed-on: #67
This commit was merged in pull request #67.
This commit is contained in:
@@ -16,3 +16,7 @@ futures.workspace = true
|
||||
|
||||
dirs = "5.0"
|
||||
regex = "1.10"
|
||||
anyhow.workspace = true
|
||||
|
||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||
web-sys = { version = "0.3", features = ["Window", "Storage"] }
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
pub use debounced_delay::*;
|
||||
pub use display::*;
|
||||
pub use event::*;
|
||||
pub use media_extractor::*;
|
||||
pub use parser::*;
|
||||
pub use paths::*;
|
||||
|
||||
mod debounced_delay;
|
||||
mod display;
|
||||
mod event;
|
||||
mod media_extractor;
|
||||
mod parser;
|
||||
mod paths;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub mod web_store;
|
||||
|
||||
pub use debounced_delay::*;
|
||||
pub use display::*;
|
||||
pub use event::*;
|
||||
pub use media_extractor::*;
|
||||
pub use parser::*;
|
||||
pub use paths::*;
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
use anyhow::{Error, anyhow};
|
||||
|
||||
const LOCAL_STORAGE: &str = "localStorage";
|
||||
|
||||
fn storage() -> Result<web_sys::Storage, Error> {
|
||||
web_sys::window()
|
||||
.ok_or_else(|| anyhow!("no browser window"))?
|
||||
.local_storage()
|
||||
.map_err(|e| anyhow!("{LOCAL_STORAGE} unavailable: {e:?}"))?
|
||||
.ok_or_else(|| anyhow!("{LOCAL_STORAGE} unavailable"))
|
||||
}
|
||||
|
||||
/// Read a value previously written with [`set`]
|
||||
pub fn get(key: &str) -> Result<Option<String>, Error> {
|
||||
storage()?
|
||||
.get_item(key)
|
||||
.map_err(|e| anyhow!("{LOCAL_STORAGE} read failed: {e:?}"))
|
||||
}
|
||||
|
||||
/// Write a value, replacing any previous one
|
||||
pub fn set(key: &str, value: &str) -> Result<(), Error> {
|
||||
storage()?
|
||||
.set_item(key, value)
|
||||
.map_err(|e| anyhow!("{LOCAL_STORAGE} write failed: {e:?}"))
|
||||
}
|
||||
|
||||
/// Remove a value if it exists
|
||||
pub fn remove(key: &str) -> Result<(), Error> {
|
||||
storage()?
|
||||
.remove_item(key)
|
||||
.map_err(|e| anyhow!("{LOCAL_STORAGE} delete failed: {e:?}"))
|
||||
}
|
||||
Reference in New Issue
Block a user