checkpoint

This commit is contained in:
John Smith
2022-07-11 08:37:08 -04:00
parent 7e0d7dad06
commit b73511142a
12 changed files with 128 additions and 110 deletions

View File

@@ -135,7 +135,7 @@ impl ProtectedStore {
{
Ok(v) => Ok(Some(v)),
Err(KeyringError::NoPasswordFound) => Ok(None),
Err(e) => Err(eyre!("Failed to load user secret")),
Err(e) => Err(eyre!("Failed to load user secret: {}", e)),
}
}
@@ -150,7 +150,7 @@ impl ProtectedStore {
{
Ok(_) => Ok(true),
Err(KeyringError::NoPasswordFound) => Ok(false),
Err(e) => Err(eyre!("Failed to remove user secret")),
Err(e) => Err(eyre!("Failed to remove user secret: {}", e)),
}
}

View File

@@ -100,12 +100,7 @@ impl TableDB {
return Ok(None);
}
};
let obj = match serde_cbor::from_slice::<T>(&b) {
Ok(value) => value,
Err(e) => {
bail!("failed to deserialize");
}
};
let obj = serde_cbor::from_slice::<T>(&b).wrap_err("failed to deserialize")?;
Ok(Some(obj))
}

View File

@@ -9,3 +9,4 @@ pub use block_store::*;
pub use protected_store::*;
pub use system::*;
pub use table_store::*;
use utils::*;

View File

@@ -1,10 +1,11 @@
use super::utils;
use super::*;
use crate::xx::*;
use crate::*;
use data_encoding::BASE64URL_NOPAD;
use js_sys::*;
use wasm_bindgen_futures::*;
use web_sys::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(catch, js_name = setPassword, js_namespace = ["global", "wasmhost", "keytar"])]
@@ -69,10 +70,11 @@ impl ProtectedStore {
}
pub async fn save_user_secret_string(&self, key: &str, value: &str) -> EyreResult<bool> {
if utils::is_nodejs() {
if is_nodejs() {
let prev = match JsFuture::from(
keytar_getPassword(self.keyring_name().as_str(), key)
.wrap_err("exception thrown")?,
.map_err(map_jsvalue_error)
.wrap_err("exception thrown")?,
)
.await
{
@@ -82,7 +84,8 @@ impl ProtectedStore {
match JsFuture::from(
keytar_setPassword(self.keyring_name().as_str(), key, value)
.wrap_err("exception thrown")?,
.map_err(map_jsvalue_error)
.wrap_err("exception thrown")?,
)
.await
{
@@ -91,7 +94,7 @@ impl ProtectedStore {
}
Ok(prev)
} else if utils::is_browser() {
} else if is_browser() {
let win = match window() {
Some(w) => w,
None => {
@@ -101,6 +104,7 @@ impl ProtectedStore {
let ls = match win
.local_storage()
.map_err(map_jsvalue_error)
.wrap_err("exception getting local storage")?
{
Some(l) => l,
@@ -113,6 +117,7 @@ impl ProtectedStore {
let prev = match ls
.get_item(&vkey)
.map_err(map_jsvalue_error)
.wrap_err("exception_thrown")?
{
Some(_) => true,
@@ -120,6 +125,7 @@ impl ProtectedStore {
};
ls.set_item(&vkey, value)
.map_err(map_jsvalue_error)
.wrap_err("exception_thrown")?;
Ok(prev)
@@ -129,9 +135,10 @@ impl ProtectedStore {
}
pub async fn load_user_secret_string(&self, key: &str) -> EyreResult<Option<String>> {
if utils::is_nodejs() {
if is_nodejs() {
let prev = match JsFuture::from(
keytar_getPassword(self.keyring_name().as_str(), key)
.map_err(map_jsvalue_error)
.wrap_err("exception thrown")?,
)
.await
@@ -145,7 +152,7 @@ impl ProtectedStore {
}
Ok(prev.as_string())
} else if utils::is_browser() {
} else if is_browser() {
let win = match window() {
Some(w) => w,
None => {
@@ -155,6 +162,7 @@ impl ProtectedStore {
let ls = match win
.local_storage()
.map_err(map_jsvalue_error)
.wrap_err("exception getting local storage")?
{
Some(l) => l,
@@ -166,6 +174,7 @@ impl ProtectedStore {
let vkey = self.browser_key_name(key);
ls.get_item(&vkey)
.map_err(map_jsvalue_error)
.wrap_err("exception_thrown")
} else {
unimplemented!();
@@ -173,16 +182,18 @@ impl ProtectedStore {
}
pub async fn remove_user_secret_string(&self, key: &str) -> EyreResult<bool> {
if utils::is_nodejs() {
if is_nodejs() {
match JsFuture::from(
keytar_deletePassword(self.keyring_name().as_str(), key).wrap_err("exception thrown")?,
keytar_deletePassword(self.keyring_name().as_str(), key)
.map_err(map_jsvalue_error)
.wrap_err("exception thrown")?,
)
.await
{
Ok(v) => Ok(v.is_truthy()),
Err(_) => bail!("Failed to delete"),
}
} else if utils::is_browser() {
} else if is_browser() {
let win = match window() {
Some(w) => w,
None => {
@@ -192,6 +203,7 @@ impl ProtectedStore {
let ls = match win
.local_storage()
.map_err(map_jsvalue_error)
.wrap_err("exception getting local storage")?
{
Some(l) => l,
@@ -204,10 +216,12 @@ impl ProtectedStore {
match ls
.get_item(&vkey)
.map_err(map_jsvalue_error)
.wrap_err("exception_thrown")?
{
Some(_) => {
ls.delete(&vkey)
.map_err(map_jsvalue_error)
.wrap_err("exception_thrown")?;
Ok(true)
}

View File

@@ -2,7 +2,6 @@ use super::utils;
use crate::xx::*;
use crate::*;
use async_executors::{Bindgen, LocalSpawnHandleExt /*, SpawnHandleExt*/};
use core::fmt;
use futures_util::future::{select, Either};
use js_sys::*;
use wasm_bindgen_futures::*;

View File

@@ -28,7 +28,7 @@ impl TableStore {
}
}
pub async fn init(&self) -> Result<(), String> {
pub async fn init(&self) -> EyreResult<()> {
Ok(())
}
@@ -49,12 +49,12 @@ impl TableStore {
}
}
fn get_table_name(&self, table: &str) -> Result<String, String> {
fn get_table_name(&self, table: &str) -> EyreResult<String> {
if !table
.chars()
.all(|c| char::is_alphanumeric(c) || c == '_' || c == '-')
{
return Err(format!("table name '{}' is invalid", table));
bail!("table name '{}' is invalid", table);
}
let c = self.config.get();
let namespace = c.namespace.clone();
@@ -65,7 +65,7 @@ impl TableStore {
})
}
pub async fn open(&self, name: &str, column_count: u32) -> Result<TableDB, String> {
pub async fn open(&self, name: &str, column_count: u32) -> EyreResult<TableDB> {
let table_name = self.get_table_name(name)?;
let mut inner = self.inner.lock();
@@ -81,7 +81,7 @@ impl TableStore {
}
let db = Database::open(table_name.clone(), column_count)
.await
.map_err(|e| format!("failed to open tabledb at: {} ({})", table_name, e))?;
.wrap_err("failed to open tabledb")?;
info!("opened table store '{}' with table name '{:?}' with {} columns", name, table_name, column_count);
let table_db = TableDB::new(table_name.clone(), self.clone(), db);
@@ -91,7 +91,7 @@ impl TableStore {
Ok(table_db)
}
pub async fn delete(&self, name: &str) -> Result<bool, String> {
pub async fn delete(&self, name: &str) -> EyreResult<bool> {
trace!("TableStore::delete {}", name);
let table_name = self.get_table_name(name)?;
@@ -101,11 +101,11 @@ impl TableStore {
"TableStore::delete {}: Not deleting, still open.",
table_name
);
return Err("Not deleting table that is still opened".to_owned());
bail!("Not deleting table that is still opened");
}
if utils::is_nodejs() {
Err("unimplemented".to_owned())
unimplemented!();
} else if utils::is_browser() {
let out = match Database::delete(table_name.clone()).await {
Ok(_) => true,
@@ -115,7 +115,7 @@ impl TableStore {
trace!("TableStore::deleted {}", table_name);
Ok(out)
} else {
Err("unimplemented".to_owned())
unimplemented!();
}
}
}

View File

@@ -77,3 +77,11 @@ pub fn node_require(module: &str) -> JsValue {
}
}
}
#[derive(ThisError, Debug, Clone, Eq, PartialEq)]
#[error("JsValue error")]
pub struct JsValueError(String);
pub fn map_jsvalue_error(x: JsValue) -> JsValueError {
JsValueError(x.as_string().unwrap_or_default())
}