Skip to content

Commit

Permalink
Simplify error conversion codes in IdbStorage, (gluesql#1465)
Browse files Browse the repository at this point in the history
Add StoreReqIntoFuture trait impl to flatten two err_into into a single into_future call.
  • Loading branch information
panarch authored Mar 16, 2024
1 parent 0bcf765 commit 5d010aa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 67 deletions.
17 changes: 17 additions & 0 deletions storages/idb-storage/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use {
async_trait::async_trait,
core::fmt::Display,
gluesql_core::error::{Error, Result},
std::{future::IntoFuture, result::Result as StdResult},
};

pub trait ErrInto<T> {
Expand All @@ -12,3 +14,18 @@ impl<T, E: Display> ErrInto<T> for Result<T, E> {
self.map_err(|e| Error::StorageMsg(e.to_string()))
}
}

#[async_trait(?Send)]
pub trait StoreReqIntoFuture<T> {
async fn into_future(self) -> Result<T>;
}

#[async_trait(?Send)]
impl<F, T, E: Display> StoreReqIntoFuture<T> for Result<F, E>
where
F: IntoFuture<Output = StdResult<T, E>>,
{
async fn into_future(self) -> Result<T> {
self.err_into()?.await.err_into()
}
}
92 changes: 25 additions & 67 deletions storages/idb-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod error;
use {
async_trait::async_trait,
convert::convert,
error::ErrInto,
error::{ErrInto, StoreReqIntoFuture},
futures::stream::{empty, iter},
gloo_utils::format::JsValueSerdeExt,
gluesql_core::{
Expand Down Expand Up @@ -103,11 +103,7 @@ impl IdbStorage {
}

pub async fn delete(&self) -> Result<()> {
self.factory
.delete(&self.namespace)
.err_into()?
.await
.err_into()
self.factory.delete(&self.namespace).into_future().await
}

async fn alter_object_store(
Expand Down Expand Up @@ -208,9 +204,9 @@ impl Store for IdbStorage {
.err_into()?;

let store = transaction.object_store(SCHEMA_STORE).err_into()?;
let schemas = store.get_all(None, None).err_into()?.await.err_into()?;
let schemas = store.get_all(None, None).into_future().await?;

transaction.commit().err_into()?.await.err_into()?;
transaction.commit().into_future().await?;
schemas
.into_iter()
.map(|schema| {
Expand All @@ -234,14 +230,13 @@ impl Store for IdbStorage {
let store = transaction.object_store(SCHEMA_STORE).err_into()?;
let schema = store
.get(JsValue::from_str(table_name))
.err_into()?
.await
.err_into()?
.into_future()
.await?
.and_then(|schema| JsValue::as_string(&schema))
.map(|schema| Schema::from_ddl(schema.as_str()))
.transpose()?;

transaction.commit().err_into()?.await.err_into()?;
transaction.commit().into_future().await?;
Ok(schema)
}

Expand All @@ -260,9 +255,9 @@ impl Store for IdbStorage {
let key: Value = target.clone().into();
let key: JsonValue = key.try_into()?;
let key = JsValue::from_serde(&key).err_into()?;
let row = store.get(key).err_into()?.await.err_into()?;
let row = store.get(key).into_future().await?;

transaction.commit().err_into()?.await.err_into()?;
transaction.commit().into_future().await?;

match row {
Some(row) => convert(row, column_defs.as_deref()).map(Some),
Expand All @@ -283,9 +278,8 @@ impl Store for IdbStorage {
let store = transaction.object_store(table_name).err_into()?;
let cursor = store
.open_cursor(None, Some(CursorDirection::Next))
.err_into()?
.await
.err_into()?;
.into_future()
.await?;

let mut cursor = match cursor {
Some(cursor) => cursor.into_managed(),
Expand All @@ -311,7 +305,7 @@ impl Store for IdbStorage {
current_row = cursor.value().err_into()?.unwrap_or(JsValue::NULL);
}

transaction.commit().err_into()?.await.err_into()?;
transaction.commit().into_future().await?;

let rows = rows.into_iter().map(Ok);
Ok(Box::pin(iter(rows)))
Expand Down Expand Up @@ -342,25 +336,13 @@ impl StoreMut for IdbStorage {
let key = JsValue::from_str(&schema.table_name);
let schema = JsValue::from(schema.to_ddl());

match schema_exists {
true => store
.put(&schema, Some(&key))
.err_into()?
.await
.err_into()?,
false => store
.add(&schema, Some(&key))
.err_into()?
.await
.err_into()?,
if schema_exists {
store.put(&schema, Some(&key)).into_future().await?;
} else {
store.add(&schema, Some(&key)).into_future().await?;
};

transaction
.commit()
.err_into()?
.await
.err_into()
.map(|_| ())
transaction.commit().into_future().await.map(|_| ())
}

async fn delete_schema(&mut self, table_name: &str) -> Result<()> {
Expand All @@ -374,18 +356,9 @@ impl StoreMut for IdbStorage {
let store = transaction.object_store(SCHEMA_STORE).err_into()?;

let key = JsValue::from_str(table_name);
store
.delete(Query::from(key))
.err_into()?
.await
.err_into()?;
store.delete(Query::from(key)).into_future().await?;

transaction
.commit()
.err_into()?
.await
.err_into()
.map(|_| ())
transaction.commit().into_future().await.map(|_| ())
}

async fn append_data(&mut self, table_name: &str, new_rows: Vec<DataRow>) -> Result<()> {
Expand All @@ -404,15 +377,10 @@ impl StoreMut for IdbStorage {
let row = JsonValue::try_from(row)?;
let row = JsValue::from_serde(&row).err_into()?;

store.add(&row, None).err_into()?.await.err_into()?;
store.add(&row, None).into_future().await?;
}

transaction
.commit()
.err_into()?
.await
.err_into()
.map(|_| ())
transaction.commit().into_future().await.map(|_| ())
}

async fn insert_data(&mut self, table_name: &str, new_rows: Vec<(Key, DataRow)>) -> Result<()> {
Expand All @@ -434,15 +402,10 @@ impl StoreMut for IdbStorage {
let key: JsonValue = Value::from(key).try_into()?;
let key = JsValue::from_serde(&key).err_into()?;

store.put(&row, Some(&key)).err_into()?.await.err_into()?;
store.put(&row, Some(&key)).into_future().await?;
}

transaction
.commit()
.err_into()?
.await
.err_into()
.map(|_| ())
transaction.commit().into_future().await.map(|_| ())
}

async fn delete_data(&mut self, table_name: &str, keys: Vec<Key>) -> Result<()> {
Expand All @@ -457,15 +420,10 @@ impl StoreMut for IdbStorage {
let key = JsValue::from_serde(&key).err_into()?;
let key = Query::from(key);

store.delete(key).err_into()?.await.err_into()?;
store.delete(key).into_future().await?;
}

transaction
.commit()
.err_into()?
.await
.err_into()
.map(|_| ())
transaction.commit().into_future().await.map(|_| ())
}
}

Expand Down

0 comments on commit 5d010aa

Please sign in to comment.