From e51d8a3339ca153b7451ad7330f5307159a39422 Mon Sep 17 00:00:00 2001 From: Samuel Onoja Date: Mon, 3 Jun 2024 21:03:54 +0100 Subject: [PATCH 1/2] try getting global with type safety and explicit type conversion --- mm2src/mm2_db/src/indexed_db/indexed_db.rs | 94 +++++++++++----------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/mm2src/mm2_db/src/indexed_db/indexed_db.rs b/mm2src/mm2_db/src/indexed_db/indexed_db.rs index d6e8ab15d4..20afd8c333 100644 --- a/mm2src/mm2_db/src/indexed_db/indexed_db.rs +++ b/mm2src/mm2_db/src/indexed_db/indexed_db.rs @@ -34,8 +34,8 @@ macro_rules! try_serialize_index_value { return MmError::err(DbTransactionError::ErrorSerializingIndex { index: $index.to_owned(), description: ser_err.to_string(), - }) - }, + }); + } } }}; } @@ -168,7 +168,7 @@ impl IndexedDbBuilder { // ignore if the receiver is closed let _res = init_tx.send(Err(e)); return; - }, + } }; // ignore if the receiver is closed @@ -191,8 +191,8 @@ async fn send_event_recv_response( event: Event, result_rx: oneshot::Receiver>, ) -> MmResult -where - Error: WithInternal + NotMmError, + where + Error: WithInternal + NotMmError, { if let Err(e) = event_tx.unbounded_send(event) { return MmError::err(Error::internal(format!("Error sending event: {}", e))); @@ -229,7 +229,7 @@ impl IndexedDb { // ignore if the receiver is closed result_tx.send(Err(e)).ok(); return; - }, + } }; let (transaction_event_tx, transaction_event_rx) = mpsc::unbounded(); @@ -276,10 +276,10 @@ impl DbTransaction<'_> { match event { internal::DbTransactionEvent::OpenTable { table_name, result_tx } => { Self::open_table(&transaction, table_name, result_tx) - }, + } internal::DbTransactionEvent::IsAborted { result_tx } => { result_tx.send(Ok(transaction.aborted())).ok(); - }, + } } } } @@ -295,7 +295,7 @@ impl DbTransaction<'_> { // ignore if the receiver is closed result_tx.send(Err(e)).ok(); return; - }, + } }; let (table_event_tx, table_event_rx) = mpsc::unbounded(); @@ -326,6 +326,7 @@ impl AddOrIgnoreResult { } } } + impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { /// Adds the given item to the table. /// https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/add @@ -347,8 +348,8 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { index_value: Value, item: &Table, ) -> DbTransactionResult - where - Value: Serialize, + where + Value: Serialize, { let ids = self.get_item_ids(index, index_value).await?; match ids.len() { @@ -380,8 +381,8 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { /// * `index` - the name of a corresponding `Table`'s field by which records will be searched. /// * `index_value` - the value of the `index`, therefore the value of a corresponding `Table`'s field. pub async fn get_items(&self, index: &str, index_value: Value) -> DbTransactionResult> - where - Value: Serialize, + where + Value: Serialize, { let (result_tx, result_rx) = oneshot::channel(); let index_value = try_serialize_index_value!(json::to_value(index_value), index); @@ -414,8 +415,8 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { index: &str, index_value: Value, ) -> DbTransactionResult> - where - Value: Serialize, + where + Value: Serialize, { let items = self.get_items(index, index_value).await?; if items.len() > 1 { @@ -446,8 +447,8 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { /// * `index` - the name of a corresponding `Table`'s field by which records will be searched. /// * `index_value` - the value of the `index`, therefore the value of a corresponding `Table`'s field. pub async fn get_item_ids(&self, index: &str, index_value: Value) -> DbTransactionResult> - where - Value: Serialize, + where + Value: Serialize, { let (result_tx, result_rx) = oneshot::channel(); let index_value = try_serialize_index_value!(json::to_value(index_value), index); @@ -535,8 +536,8 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { index_value: Value, item: &Table, ) -> DbTransactionResult - where - Value: Serialize, + where + Value: Serialize, { let ids = self.get_item_ids(index, index_value).await?; match ids.len() { @@ -544,7 +545,7 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { 1 => { let item_id = ids[0]; self.replace_item(item_id, item).await - }, + } got_items => MmError::err(DbTransactionError::MultipleItemsByUniqueIndex { index: index.to_owned(), got_items, @@ -584,8 +585,8 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { index: &str, index_value: Value, ) -> DbTransactionResult> - where - Value: Serialize, + where + Value: Serialize, { let ids = self.get_item_ids(index, index_value).await?; match ids.len() { @@ -594,7 +595,7 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { let item_id = ids[0]; self.delete_item(item_id).await?; Ok(Some(item_id)) - }, + } got_items => MmError::err(DbTransactionError::MultipleItemsByUniqueIndex { index: index.to_owned(), got_items, @@ -623,8 +624,8 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { index: &str, index_value: Value, ) -> DbTransactionResult> - where - Value: Serialize, + where + Value: Serialize, { let ids = self.get_item_ids(index, index_value).await?; for item_id in ids.iter() { @@ -703,7 +704,7 @@ async fn table_event_loop(mut rx: mpsc::UnboundedReceiver { let res = table.add_item(&item).await; result_tx.send(res).ok(); - }, + } internal::DbTableEvent::GetItems { index, index_value, @@ -711,7 +712,7 @@ async fn table_event_loop(mut rx: mpsc::UnboundedReceiver { let res = table.get_items(&index, index_value).await; result_tx.send(res).ok(); - }, + } internal::DbTableEvent::GetItemIds { index, index_value, @@ -719,11 +720,11 @@ async fn table_event_loop(mut rx: mpsc::UnboundedReceiver { let res = table.get_item_ids(&index, index_value).await; result_tx.send(res).ok(); - }, + } internal::DbTableEvent::GetAllItems { result_tx } => { let res = table.get_all_items().await; result_tx.send(res).ok(); - }, + } internal::DbTableEvent::Count { index, index_value, @@ -731,11 +732,11 @@ async fn table_event_loop(mut rx: mpsc::UnboundedReceiver { let res = table.count(&index, index_value).await; result_tx.send(res).ok(); - }, + } internal::DbTableEvent::CountAll { result_tx } => { let res = table.count_all().await; result_tx.send(res).ok(); - }, + } internal::DbTableEvent::ReplaceItem { item_id, item, @@ -743,18 +744,18 @@ async fn table_event_loop(mut rx: mpsc::UnboundedReceiver { let res = table.replace_item(item_id, item).await; result_tx.send(res).ok(); - }, + } internal::DbTableEvent::DeleteItem { item_id, result_tx } => { let res = table.delete_item(item_id).await; result_tx.send(res).ok(); - }, + } internal::DbTableEvent::Clear { result_tx } => { let res = table.clear().await; result_tx.send(res).ok(); - }, + } internal::DbTableEvent::IsAborted { result_tx } => { result_tx.send(Ok(table.aborted())).ok(); - }, + } internal::DbTableEvent::OpenCursor { index, filters, @@ -762,7 +763,7 @@ async fn table_event_loop(mut rx: mpsc::UnboundedReceiver { open_cursor(&table, index, filters, filters_ext, result_tx); - }, + } } } } @@ -807,14 +808,14 @@ fn open_cursor( }); result_tx.send(Err(cursor_err)).ok(); return; - }, + } }; let cursor = match CursorDriver::init_cursor(db_index, filters, filter_ext) { Ok(cursor) => cursor, Err(e) => { result_tx.send(Err(e)).ok(); return; - }, + } }; let (event_tx, event_rx) = mpsc::unbounded(); @@ -830,7 +831,10 @@ fn open_cursor( /// Detects the current execution environment (window or worker) and follows the appropriate way /// of getting `web_sys::IdbFactory` instance. pub(crate) fn get_idb_factory() -> Result { - let global = js_sys::global(); + // try getting global with type safety and explicit type conversion. + let global = js_sys::global() + .dyn_into::() + .map_err(|err| InitDbError::NotSupported(format!("{err:?}")))?; let idb_factory = if let Some(window) = global.dyn_ref::() { window.indexed_db() @@ -848,7 +852,7 @@ pub(crate) fn get_idb_factory() -> Result { } else { "IndexedDB not supported in worker context" } - .to_string(), + .to_string(), )), Err(e) => Err(InitDbError::NotSupported(stringify_js_error(&e))), } @@ -1068,7 +1072,7 @@ mod tests { AddOrIgnoreResult::Added(item_id) => item_id, AddOrIgnoreResult::ExistAlready(unknown_tx_id) => { panic!("Transaction should be added: found '{}'", unknown_tx_id) - }, + } }; let found_tx_id = match table .add_item_or_ignore_by_unique_index("tx_hash", TX_HASH, &tx_2) @@ -1216,7 +1220,7 @@ mod tests { .expect("Couldn't get items by the index 'ticker=RICK'"); assert_eq!(actual_rick_txs, vec![ (rick_tx_1_id, rick_tx_1_updated), - (rick_tx_2_id, rick_tx_2) + (rick_tx_2_id, rick_tx_2), ]); } @@ -1326,16 +1330,16 @@ mod tests { (0, 1) => { let table = upgrader.create_table("upgradable_table")?; table.create_index("first_index", false)?; - }, + } (0, 2) => { let table = upgrader.create_table("upgradable_table")?; table.create_index("first_index", false)?; table.create_index("second_index", false)?; - }, + } (1, 2) => { let table = upgrader.open_table("upgradable_table")?; table.create_index("second_index", false)?; - }, + } v => panic!("Unexpected old, new versions: {:?}", v), } Ok(()) From cba44a7004c95f51ea7105919a5d99ed943cf731 Mon Sep 17 00:00:00 2001 From: Samuel Onoja Date: Mon, 3 Jun 2024 21:10:07 +0100 Subject: [PATCH 2/2] fix cargo fmt --- mm2src/mm2_db/src/indexed_db/indexed_db.rs | 84 +++++++++++----------- mm2src/mm2_test_helpers/src/for_tests.rs | 1 - 2 files changed, 42 insertions(+), 43 deletions(-) diff --git a/mm2src/mm2_db/src/indexed_db/indexed_db.rs b/mm2src/mm2_db/src/indexed_db/indexed_db.rs index 20afd8c333..f7d04dfddb 100644 --- a/mm2src/mm2_db/src/indexed_db/indexed_db.rs +++ b/mm2src/mm2_db/src/indexed_db/indexed_db.rs @@ -35,7 +35,7 @@ macro_rules! try_serialize_index_value { index: $index.to_owned(), description: ser_err.to_string(), }); - } + }, } }}; } @@ -168,7 +168,7 @@ impl IndexedDbBuilder { // ignore if the receiver is closed let _res = init_tx.send(Err(e)); return; - } + }, }; // ignore if the receiver is closed @@ -191,8 +191,8 @@ async fn send_event_recv_response( event: Event, result_rx: oneshot::Receiver>, ) -> MmResult - where - Error: WithInternal + NotMmError, +where + Error: WithInternal + NotMmError, { if let Err(e) = event_tx.unbounded_send(event) { return MmError::err(Error::internal(format!("Error sending event: {}", e))); @@ -229,7 +229,7 @@ impl IndexedDb { // ignore if the receiver is closed result_tx.send(Err(e)).ok(); return; - } + }, }; let (transaction_event_tx, transaction_event_rx) = mpsc::unbounded(); @@ -276,10 +276,10 @@ impl DbTransaction<'_> { match event { internal::DbTransactionEvent::OpenTable { table_name, result_tx } => { Self::open_table(&transaction, table_name, result_tx) - } + }, internal::DbTransactionEvent::IsAborted { result_tx } => { result_tx.send(Ok(transaction.aborted())).ok(); - } + }, } } } @@ -295,7 +295,7 @@ impl DbTransaction<'_> { // ignore if the receiver is closed result_tx.send(Err(e)).ok(); return; - } + }, }; let (table_event_tx, table_event_rx) = mpsc::unbounded(); @@ -348,8 +348,8 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { index_value: Value, item: &Table, ) -> DbTransactionResult - where - Value: Serialize, + where + Value: Serialize, { let ids = self.get_item_ids(index, index_value).await?; match ids.len() { @@ -381,8 +381,8 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { /// * `index` - the name of a corresponding `Table`'s field by which records will be searched. /// * `index_value` - the value of the `index`, therefore the value of a corresponding `Table`'s field. pub async fn get_items(&self, index: &str, index_value: Value) -> DbTransactionResult> - where - Value: Serialize, + where + Value: Serialize, { let (result_tx, result_rx) = oneshot::channel(); let index_value = try_serialize_index_value!(json::to_value(index_value), index); @@ -415,8 +415,8 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { index: &str, index_value: Value, ) -> DbTransactionResult> - where - Value: Serialize, + where + Value: Serialize, { let items = self.get_items(index, index_value).await?; if items.len() > 1 { @@ -447,8 +447,8 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { /// * `index` - the name of a corresponding `Table`'s field by which records will be searched. /// * `index_value` - the value of the `index`, therefore the value of a corresponding `Table`'s field. pub async fn get_item_ids(&self, index: &str, index_value: Value) -> DbTransactionResult> - where - Value: Serialize, + where + Value: Serialize, { let (result_tx, result_rx) = oneshot::channel(); let index_value = try_serialize_index_value!(json::to_value(index_value), index); @@ -536,8 +536,8 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { index_value: Value, item: &Table, ) -> DbTransactionResult - where - Value: Serialize, + where + Value: Serialize, { let ids = self.get_item_ids(index, index_value).await?; match ids.len() { @@ -545,7 +545,7 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { 1 => { let item_id = ids[0]; self.replace_item(item_id, item).await - } + }, got_items => MmError::err(DbTransactionError::MultipleItemsByUniqueIndex { index: index.to_owned(), got_items, @@ -585,8 +585,8 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { index: &str, index_value: Value, ) -> DbTransactionResult> - where - Value: Serialize, + where + Value: Serialize, { let ids = self.get_item_ids(index, index_value).await?; match ids.len() { @@ -595,7 +595,7 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { let item_id = ids[0]; self.delete_item(item_id).await?; Ok(Some(item_id)) - } + }, got_items => MmError::err(DbTransactionError::MultipleItemsByUniqueIndex { index: index.to_owned(), got_items, @@ -624,8 +624,8 @@ impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> { index: &str, index_value: Value, ) -> DbTransactionResult> - where - Value: Serialize, + where + Value: Serialize, { let ids = self.get_item_ids(index, index_value).await?; for item_id in ids.iter() { @@ -704,7 +704,7 @@ async fn table_event_loop(mut rx: mpsc::UnboundedReceiver { let res = table.add_item(&item).await; result_tx.send(res).ok(); - } + }, internal::DbTableEvent::GetItems { index, index_value, @@ -712,7 +712,7 @@ async fn table_event_loop(mut rx: mpsc::UnboundedReceiver { let res = table.get_items(&index, index_value).await; result_tx.send(res).ok(); - } + }, internal::DbTableEvent::GetItemIds { index, index_value, @@ -720,11 +720,11 @@ async fn table_event_loop(mut rx: mpsc::UnboundedReceiver { let res = table.get_item_ids(&index, index_value).await; result_tx.send(res).ok(); - } + }, internal::DbTableEvent::GetAllItems { result_tx } => { let res = table.get_all_items().await; result_tx.send(res).ok(); - } + }, internal::DbTableEvent::Count { index, index_value, @@ -732,11 +732,11 @@ async fn table_event_loop(mut rx: mpsc::UnboundedReceiver { let res = table.count(&index, index_value).await; result_tx.send(res).ok(); - } + }, internal::DbTableEvent::CountAll { result_tx } => { let res = table.count_all().await; result_tx.send(res).ok(); - } + }, internal::DbTableEvent::ReplaceItem { item_id, item, @@ -744,18 +744,18 @@ async fn table_event_loop(mut rx: mpsc::UnboundedReceiver { let res = table.replace_item(item_id, item).await; result_tx.send(res).ok(); - } + }, internal::DbTableEvent::DeleteItem { item_id, result_tx } => { let res = table.delete_item(item_id).await; result_tx.send(res).ok(); - } + }, internal::DbTableEvent::Clear { result_tx } => { let res = table.clear().await; result_tx.send(res).ok(); - } + }, internal::DbTableEvent::IsAborted { result_tx } => { result_tx.send(Ok(table.aborted())).ok(); - } + }, internal::DbTableEvent::OpenCursor { index, filters, @@ -763,7 +763,7 @@ async fn table_event_loop(mut rx: mpsc::UnboundedReceiver { open_cursor(&table, index, filters, filters_ext, result_tx); - } + }, } } } @@ -808,14 +808,14 @@ fn open_cursor( }); result_tx.send(Err(cursor_err)).ok(); return; - } + }, }; let cursor = match CursorDriver::init_cursor(db_index, filters, filter_ext) { Ok(cursor) => cursor, Err(e) => { result_tx.send(Err(e)).ok(); return; - } + }, }; let (event_tx, event_rx) = mpsc::unbounded(); @@ -852,7 +852,7 @@ pub(crate) fn get_idb_factory() -> Result { } else { "IndexedDB not supported in worker context" } - .to_string(), + .to_string(), )), Err(e) => Err(InitDbError::NotSupported(stringify_js_error(&e))), } @@ -1072,7 +1072,7 @@ mod tests { AddOrIgnoreResult::Added(item_id) => item_id, AddOrIgnoreResult::ExistAlready(unknown_tx_id) => { panic!("Transaction should be added: found '{}'", unknown_tx_id) - } + }, }; let found_tx_id = match table .add_item_or_ignore_by_unique_index("tx_hash", TX_HASH, &tx_2) @@ -1330,16 +1330,16 @@ mod tests { (0, 1) => { let table = upgrader.create_table("upgradable_table")?; table.create_index("first_index", false)?; - } + }, (0, 2) => { let table = upgrader.create_table("upgradable_table")?; table.create_index("first_index", false)?; table.create_index("second_index", false)?; - } + }, (1, 2) => { let table = upgrader.open_table("upgradable_table")?; table.create_index("second_index", false)?; - } + }, v => panic!("Unexpected old, new versions: {:?}", v), } Ok(()) diff --git a/mm2src/mm2_test_helpers/src/for_tests.rs b/mm2src/mm2_test_helpers/src/for_tests.rs index 4d6d74fd71..0a637f5d15 100644 --- a/mm2src/mm2_test_helpers/src/for_tests.rs +++ b/mm2src/mm2_test_helpers/src/for_tests.rs @@ -1935,7 +1935,6 @@ pub async fn enable_eth_coin( json::from_str(&enable.1).unwrap() } - pub async fn enable_spl(mm: &MarketMakerIt, coin: &str) -> Json { let req = json!({ "userpass": mm.userpass,