diff --git a/mm2src/mm2_db/src/indexed_db/drivers/cursor/cursor.rs b/mm2src/mm2_db/src/indexed_db/drivers/cursor/cursor.rs index 6eb10b754b..0fdc26261b 100644 --- a/mm2src/mm2_db/src/indexed_db/drivers/cursor/cursor.rs +++ b/mm2src/mm2_db/src/indexed_db/drivers/cursor/cursor.rs @@ -168,17 +168,15 @@ impl CursorBoundValue { } } -/// TODO rename to `CursorAction`. #[derive(Debug, PartialEq)] -pub enum CollectCursorAction { +pub enum CursorAction { Continue, ContinueWithValue(JsValue), Stop, } -/// TODO rename to `IterItemAction`. #[derive(Debug, PartialEq)] -pub enum CollectItemAction { +pub enum CursorItemAction { Include, Skip, } @@ -186,7 +184,7 @@ pub enum CollectItemAction { pub trait CursorDriverImpl: Sized { fn key_range(&self) -> CursorResult>; - fn on_iteration(&mut self, key: JsValue) -> CursorResult<(CollectItemAction, CollectCursorAction)>; + fn on_iteration(&mut self, key: JsValue) -> CursorResult<(CursorItemAction, CursorAction)>; } pub(crate) struct CursorDriver { @@ -194,7 +192,7 @@ pub(crate) struct CursorDriver { inner: IdbCursorEnum, cursor_request: IdbRequest, cursor_item_rx: mpsc::Receiver>, - /// Whether we got `CollectCursorAction::Stop` at the last iteration or not. + /// Whether we got `CursorAction::Stop` at the last iteration or not. stopped: bool, /// We need to hold the closures in memory till `cursor` exists. _onsuccess_closure: Closure, @@ -244,7 +242,7 @@ impl CursorDriver { pub(crate) async fn next(&mut self) -> CursorResult> { loop { - // Check if we got `CollectCursorAction::Stop` at the last iteration. + // Check if we got `CursorAction::Stop` at the last iteration. if self.stopped { return Ok(None); } @@ -285,10 +283,10 @@ impl CursorDriver { let (item_action, cursor_action) = self.inner.on_iteration(key)?; match cursor_action { - CollectCursorAction::Continue => cursor.continue_().map_to_mm(|e| CursorError::AdvanceError { + CursorAction::Continue => cursor.continue_().map_to_mm(|e| CursorError::AdvanceError { description: stringify_js_error(&e), })?, - CollectCursorAction::ContinueWithValue(next_value) => { + CursorAction::ContinueWithValue(next_value) => { cursor .continue_with_key(&next_value) .map_to_mm(|e| CursorError::AdvanceError { @@ -299,13 +297,13 @@ impl CursorDriver { // Here we set the `stopped` flag so we return `Ok(None)` at the next iteration immediately. // This is required because `item_action` can be `CollectItemAction::Include`, // and at this iteration we will return `Ok(Some)`. - CollectCursorAction::Stop => self.stopped = true, + CursorAction::Stop => self.stopped = true, } match item_action { - CollectItemAction::Include => return Ok(Some(item.into_pair())), + CursorItemAction::Include => return Ok(Some(item.into_pair())), // Try to fetch the next item. - CollectItemAction::Skip => (), + CursorItemAction::Skip => (), } } } @@ -366,7 +364,7 @@ impl CursorDriverImpl for IdbCursorEnum { } } - fn on_iteration(&mut self, key: JsValue) -> CursorResult<(CollectItemAction, CollectCursorAction)> { + fn on_iteration(&mut self, key: JsValue) -> CursorResult<(CursorItemAction, CursorAction)> { match self { IdbCursorEnum::Empty(empty) => empty.on_iteration(key), IdbCursorEnum::SingleKey(single) => single.on_iteration(key), diff --git a/mm2src/mm2_db/src/indexed_db/drivers/cursor/empty_cursor.rs b/mm2src/mm2_db/src/indexed_db/drivers/cursor/empty_cursor.rs index ca0e9d213a..441180d8bb 100644 --- a/mm2src/mm2_db/src/indexed_db/drivers/cursor/empty_cursor.rs +++ b/mm2src/mm2_db/src/indexed_db/drivers/cursor/empty_cursor.rs @@ -1,4 +1,4 @@ -use super::{CollectCursorAction, CollectItemAction, CursorDriverImpl, CursorResult}; +use super::{CursorAction, CursorDriverImpl, CursorItemAction, CursorResult}; use wasm_bindgen::prelude::*; use web_sys::IdbKeyRange; @@ -8,7 +8,7 @@ pub struct IdbEmptyCursor; impl CursorDriverImpl for IdbEmptyCursor { fn key_range(&self) -> CursorResult> { Ok(None) } - fn on_iteration(&mut self, _key: JsValue) -> CursorResult<(CollectItemAction, CollectCursorAction)> { - Ok((CollectItemAction::Include, CollectCursorAction::Continue)) + fn on_iteration(&mut self, _key: JsValue) -> CursorResult<(CursorItemAction, CursorAction)> { + Ok((CursorItemAction::Include, CursorAction::Continue)) } } diff --git a/mm2src/mm2_db/src/indexed_db/drivers/cursor/multi_key_bound_cursor.rs b/mm2src/mm2_db/src/indexed_db/drivers/cursor/multi_key_bound_cursor.rs index 170b3c516f..6a6b224fcd 100644 --- a/mm2src/mm2_db/src/indexed_db/drivers/cursor/multi_key_bound_cursor.rs +++ b/mm2src/mm2_db/src/indexed_db/drivers/cursor/multi_key_bound_cursor.rs @@ -1,5 +1,5 @@ -use super::{index_key_as_array, CollectCursorAction, CollectItemAction, CursorBoundValue, CursorDriverImpl, - CursorError, CursorResult}; +use super::{index_key_as_array, CursorAction, CursorBoundValue, CursorDriverImpl, CursorError, CursorItemAction, + CursorResult}; use common::{deserialize_from_js, serialize_to_js, stringify_js_error}; use js_sys::Array; use mm2_err_handle::prelude::*; @@ -108,7 +108,7 @@ impl CursorDriverImpl for IdbMultiKeyBoundCursor { /// The range `IDBKeyRange.bound([2,2], [4,4])` includes values like `[3,0]` and `[3,5]` as `[2,2] < [3,0] < [3,5] < [4,4]`, /// so we need to do additional filtering. /// For more information on why it's required, see https://stackoverflow.com/a/32976384. - fn on_iteration(&mut self, index_key: JsValue) -> CursorResult<(CollectItemAction, CollectCursorAction)> { + fn on_iteration(&mut self, index_key: JsValue) -> CursorResult<(CursorItemAction, CursorAction)> { let index_keys_js_array = index_key_as_array(index_key)?; let index_keys: Vec = index_keys_js_array .iter() @@ -157,8 +157,8 @@ impl CursorDriverImpl for IdbMultiKeyBoundCursor { return Ok(( // the `actual_index_value` is not in our expected bounds - CollectItemAction::Skip, - CollectCursorAction::ContinueWithValue(new_index.into()), + CursorItemAction::Skip, + CursorAction::ContinueWithValue(new_index.into()), )); } if &actual_index_value > upper_bound { @@ -177,13 +177,13 @@ impl CursorDriverImpl for IdbMultiKeyBoundCursor { return Ok(( // the `actual_index_value` is not in our expected bounds - CollectItemAction::Skip, - CollectCursorAction::ContinueWithValue(new_index.into()), + CursorItemAction::Skip, + CursorAction::ContinueWithValue(new_index.into()), )); } // otherwise there is no an index greater than actual `index`, stop the cursor - return Ok((CollectItemAction::Skip, CollectCursorAction::Stop)); + return Ok((CursorItemAction::Skip, CursorAction::Stop)); } let increased_index_key = actual_index_value.next(); @@ -194,7 +194,7 @@ impl CursorDriverImpl for IdbMultiKeyBoundCursor { idx_in_index += 1; idx_in_bounds += 1; } - Ok((CollectItemAction::Include, CollectCursorAction::Continue)) + Ok((CursorItemAction::Include, CursorAction::Continue)) } } @@ -212,7 +212,7 @@ mod tests { let result = cursor.on_iteration(input_index_js_value); assert_eq!( result, - Ok((CollectItemAction::Include, CollectCursorAction::Continue)), + Ok((CursorItemAction::Include, CursorAction::Continue)), "'{}' index is expected to be in a bound", input_index ); @@ -230,15 +230,12 @@ mod tests { .expect(&format!("Error due to the index '{:?}'", input_index)); let actual_next: Json = match cursor_action { - CollectCursorAction::ContinueWithValue(next_index_js_value) => { + CursorAction::ContinueWithValue(next_index_js_value) => { deserialize_from_js(next_index_js_value).expect("Error deserializing next index}") }, - action => panic!( - "Expected 'CollectCursorAction::ContinueWithValue', found '{:?}'", - action - ), + action => panic!("Expected 'CursorAction::ContinueWithValue', found '{:?}'", action), }; - assert_eq!(item_action, CollectItemAction::Skip); + assert_eq!(item_action, CursorItemAction::Skip); assert_eq!(actual_next, expected_next); } } @@ -250,7 +247,7 @@ mod tests { let result = cursor.on_iteration(input_index_js_value); assert_eq!( result, - Ok((CollectItemAction::Skip, CollectCursorAction::Stop)), + Ok((CursorItemAction::Skip, CursorAction::Stop)), "'{}' index is expected to be out of bound", input_index ); diff --git a/mm2src/mm2_db/src/indexed_db/drivers/cursor/multi_key_cursor.rs b/mm2src/mm2_db/src/indexed_db/drivers/cursor/multi_key_cursor.rs index ddd2cd8fa0..f6b128aa1b 100644 --- a/mm2src/mm2_db/src/indexed_db/drivers/cursor/multi_key_cursor.rs +++ b/mm2src/mm2_db/src/indexed_db/drivers/cursor/multi_key_cursor.rs @@ -1,4 +1,4 @@ -use super::{CollectCursorAction, CollectItemAction, CursorDriverImpl, CursorError, CursorResult}; +use super::{CursorAction, CursorDriverImpl, CursorError, CursorItemAction, CursorResult}; use common::{serialize_to_js, stringify_js_error}; use js_sys::Array; use mm2_err_handle::prelude::*; @@ -36,7 +36,7 @@ impl CursorDriverImpl for IdbMultiKeyCursor { Ok(Some(key_range)) } - fn on_iteration(&mut self, _key: JsValue) -> CursorResult<(CollectItemAction, CollectCursorAction)> { - Ok((CollectItemAction::Include, CollectCursorAction::Continue)) + fn on_iteration(&mut self, _key: JsValue) -> CursorResult<(CursorItemAction, CursorAction)> { + Ok((CursorItemAction::Include, CursorAction::Continue)) } } diff --git a/mm2src/mm2_db/src/indexed_db/drivers/cursor/single_key_bound_cursor.rs b/mm2src/mm2_db/src/indexed_db/drivers/cursor/single_key_bound_cursor.rs index d868984526..92e7ee087b 100644 --- a/mm2src/mm2_db/src/indexed_db/drivers/cursor/single_key_bound_cursor.rs +++ b/mm2src/mm2_db/src/indexed_db/drivers/cursor/single_key_bound_cursor.rs @@ -1,4 +1,4 @@ -use super::{CollectCursorAction, CollectItemAction, CursorBoundValue, CursorDriverImpl, CursorError, CursorResult}; +use super::{CursorAction, CursorBoundValue, CursorDriverImpl, CursorError, CursorItemAction, CursorResult}; use common::{log::warn, stringify_js_error}; use mm2_err_handle::prelude::*; use wasm_bindgen::prelude::*; @@ -56,7 +56,7 @@ impl CursorDriverImpl for IdbSingleKeyBoundCursor { Ok(Some(key_range)) } - fn on_iteration(&mut self, _key: JsValue) -> CursorResult<(CollectItemAction, CollectCursorAction)> { - Ok((CollectItemAction::Include, CollectCursorAction::Continue)) + fn on_iteration(&mut self, _key: JsValue) -> CursorResult<(CursorItemAction, CursorAction)> { + Ok((CursorItemAction::Include, CursorAction::Continue)) } } diff --git a/mm2src/mm2_db/src/indexed_db/drivers/cursor/single_key_cursor.rs b/mm2src/mm2_db/src/indexed_db/drivers/cursor/single_key_cursor.rs index 6416317943..b3d6efa476 100644 --- a/mm2src/mm2_db/src/indexed_db/drivers/cursor/single_key_cursor.rs +++ b/mm2src/mm2_db/src/indexed_db/drivers/cursor/single_key_cursor.rs @@ -1,4 +1,4 @@ -use super::{CollectCursorAction, CollectItemAction, CursorDriverImpl, CursorError, CursorResult}; +use super::{CursorAction, CursorDriverImpl, CursorError, CursorItemAction, CursorResult}; use common::{serialize_to_js, stringify_js_error}; use mm2_err_handle::prelude::*; use serde_json::Value as Json; @@ -38,7 +38,7 @@ impl CursorDriverImpl for IdbSingleKeyCursor { Ok(Some(key_range)) } - fn on_iteration(&mut self, _key: JsValue) -> CursorResult<(CollectItemAction, CollectCursorAction)> { - Ok((CollectItemAction::Include, CollectCursorAction::Continue)) + fn on_iteration(&mut self, _key: JsValue) -> CursorResult<(CursorItemAction, CursorAction)> { + Ok((CursorItemAction::Include, CursorAction::Continue)) } }