hashiverse-lib 1.0.8

Core protocol library for Hashiverse — your open-source decentralized X/Twitter replacement.
use anyhow::anyhow;
use wasm_bindgen::JsValue;

pub trait JsResultExt<T> {
    fn with_js_context<F, S>(self, f: F) -> anyhow::Result<T>
    where
        F: FnOnce() -> S,
        S: Into<String>;
}

impl<T> JsResultExt<T> for std::result::Result<T, JsValue> {
    fn with_js_context<F, S>(self, f: F) -> anyhow::Result<T>
    where
        F: FnOnce() -> S,
        S: Into<String>,
    {
        self.map_err(|e| anyhow!("{}: {:?}", f().into(), e))
    }
}

impl<T> JsResultExt<T> for std::result::Result<T, indexed_db_futures::error::Error> {
    fn with_js_context<F, S>(self, f: F) -> anyhow::Result<T>
    where
        F: FnOnce() -> S,
        S: Into<String>,
    {
        // IdbError usually implements Display, so we can use {} instead of {:?}
        self.map_err(|e| anyhow::anyhow!("{}: {}", f().into(), e))
    }
}