From 233062d2d6ecf5a00fe75e6b1dfa37279a0e4834 Mon Sep 17 00:00:00 2001 From: Devashish Dixit Date: Wed, 28 Aug 2024 16:18:36 +0800 Subject: [PATCH] Fix the leak in API where KeyRange returns idb::Error --- src/key_range.rs | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 6 ++-- 2 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 src/key_range.rs diff --git a/src/key_range.rs b/src/key_range.rs new file mode 100644 index 0000000..090c5bb --- /dev/null +++ b/src/key_range.rs @@ -0,0 +1,83 @@ +use idb::{KeyRange as IdbKeyRange, Query}; +use wasm_bindgen::JsValue; + +use crate::Error; + +/// Represents a continuous interval over some data type that is used for keys. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct KeyRange { + inner: IdbKeyRange, +} + +impl KeyRange { + /// Returns a new [`KeyRange`] spanning only key. + pub fn only(value: &JsValue) -> Result { + IdbKeyRange::only(value).map(Into::into).map_err(Into::into) + } + + /// Returns a new [`KeyRange`] spanning from lower to upper. If `lower_open` is true, `lower` is not included in the + /// range. If `upper_open` is true, `upper` is not included in the range. + pub fn bound( + lower: &JsValue, + upper: &JsValue, + lower_open: Option, + upper_open: Option, + ) -> Result { + IdbKeyRange::bound(lower, upper, lower_open, upper_open) + .map(Into::into) + .map_err(Into::into) + } + + /// Returns a new [`KeyRange`] starting at key with no upper bound. If `lower_open` is true, key is not included in + /// the range. + pub fn lower_bound(lower: &JsValue, lower_open: Option) -> Result { + IdbKeyRange::lower_bound(lower, lower_open) + .map(Into::into) + .map_err(Into::into) + } + + /// Returns a new [`KeyRange`] with no lower bound and ending at key. If `upper_open` is true, key is not included + /// in the range. + pub fn upper_bound(upper: &JsValue, upper_open: Option) -> Result { + IdbKeyRange::upper_bound(upper, upper_open) + .map(Into::into) + .map_err(Into::into) + } + + /// Returns the range’s lower bound, or undefined if none. + pub fn lower(&self) -> Result { + self.inner.lower().map_err(Into::into) + } + + /// Returns the range’s upper bound, or undefined if none. + pub fn upper(&self) -> Result { + self.inner.upper().map_err(Into::into) + } + + /// Returns the range’s lower open flag. + pub fn lower_open(&self) -> bool { + self.inner.lower_open() + } + + /// Returns the range’s upper open flag. + pub fn upper_open(&self) -> bool { + self.inner.upper_open() + } + + /// Returns true if key is included in the range, and false otherwise. + pub fn includes(&self, value: &JsValue) -> Result { + self.inner.includes(value).map_err(Into::into) + } +} + +impl From for KeyRange { + fn from(inner: IdbKeyRange) -> Self { + Self { inner } + } +} + +impl From for Query { + fn from(key_range: KeyRange) -> Self { + key_range.inner.into() + } +} diff --git a/src/lib.rs b/src/lib.rs index 36c49f1..cfefe10 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -99,18 +99,18 @@ //! ``` mod error; mod index; +mod key_range; mod object_store; mod rexie; mod rexie_builder; mod transaction; -pub use idb::{ - CursorDirection as Direction, KeyPath, KeyRange, TransactionMode, TransactionResult, -}; +pub use idb::{CursorDirection as Direction, KeyPath, TransactionMode, TransactionResult}; pub use self::{ error::{Error, Result}, index::Index, + key_range::KeyRange, object_store::ObjectStore, rexie::Rexie, rexie_builder::RexieBuilder,