Skip to content

Commit de0feab

Browse files
authored
Merge pull request #1 from lowcarboncode/event
Event
2 parents d58f1d4 + d60aa0b commit de0feab

File tree

8 files changed

+82
-84
lines changed

8 files changed

+82
-84
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ repository = "https://github.com/lowcarboncode/quickleaf"
1212
readme = "README.md"
1313

1414
[dependencies]
15-
valu3 = "0.6.5"
15+
valu3 = "0.6"
1616

1717
[features]
1818
default = []

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Quickleaf Cache is a Rust library that provides a simple and efficient in-memory
1010
- List cache entries with support for filtering, ordering, and limiting results
1111
- Custom error handling
1212
- Event notifications for cache operations
13+
- Support for generic values using [valu3](https://github.com/lowcarboncode/valu3)
1314

1415
## Installation
1516

@@ -18,6 +19,7 @@ Add the following to your `Cargo.toml`:
1819
```toml
1920
[dependencies]
2021
quickleaf = "0.1"
22+
valu3 = "0.1"
2123
```
2224

2325
## Usage
@@ -26,6 +28,7 @@ Here's a basic example of how to use Quickleaf Cache:
2628

2729
```rust
2830
use quickleaf::{Quickleaf, ListProps, Order, Filter};
31+
use quickleaf::valu3::value::Value;
2932

3033
fn main() {
3134
let mut cache = Quickleaf::new(2);
@@ -34,8 +37,8 @@ fn main() {
3437
cache.insert("key3", 3);
3538

3639
assert_eq!(cache.get("key1"), None);
37-
assert_eq!(cache.get("key2"), Some(&2));
38-
assert_eq!(cache.get("key3"), Some(&3));
40+
assert_eq!(cache.get("key2"), Some(&2.to_value()));
41+
assert_eq!(cache.get("key3"), Some(&3.to_value()));
3942

4043
let list_props = ListProps::default()
4144
.order(Order::Asc)
@@ -128,6 +131,7 @@ You can use events to get notified when cache entries are inserted, removed, or
128131
```rust
129132
use quickleaf::{Quickleaf, Event};
130133
use std::sync::mpsc::channel;
134+
use quickleaf::valu3::value::Value;
131135

132136
fn main() {
133137
let (tx, rx) = channel();
@@ -150,15 +154,15 @@ fn main() {
150154
assert_eq!(items.len(), 3);
151155
assert_eq!(
152156
items[0],
153-
Event::insert("key1".to_string(), 1)
157+
Event::insert("key1".to_string(), 1.to_value())
154158
);
155159
assert_eq!(
156160
items[1],
157-
Event::insert("key2".to_string(), 2)
161+
Event::insert("key2".to_string(), 2.to_value())
158162
);
159163
assert_eq!(
160164
items[2],
161-
Event::insert("key3".to_string(), 3)
165+
Event::insert("key3".to_string(), 3.to_value())
162166
);
163167
}
164168
```

src/cache.rs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::HashMap;
22
use std::fmt::Debug;
33

4+
use valu3::traits::ToValueBehavior;
45
use valu3::value::Value;
56

67
use crate::error::Error;
@@ -12,27 +13,21 @@ use std::sync::mpsc::Sender;
1213
pub type Key = String;
1314

1415
#[derive(Clone, Debug)]
15-
pub struct Cache<V>
16-
where
17-
V: PartialEq,
18-
{
19-
map: HashMap<Key, V>,
16+
pub struct Cache {
17+
map: HashMap<Key, Value>,
2018
list: Vec<Key>,
2119
capacity: usize,
22-
sender: Option<Sender<Event<V>>>,
23-
_phantom: std::marker::PhantomData<V>,
20+
sender: Option<Sender<Event<Value>>>,
21+
_phantom: std::marker::PhantomData<Value>,
2422
}
2523

26-
impl PartialEq for Cache<Value> {
24+
impl PartialEq for Cache {
2725
fn eq(&self, other: &Self) -> bool {
2826
self.map == other.map && self.list == other.list && self.capacity == other.capacity
2927
}
3028
}
3129

32-
impl<V> Cache<V>
33-
where
34-
V: PartialEq + Clone,
35-
{
30+
impl Cache {
3631
pub fn new(capacity: usize) -> Self {
3732
Self {
3833
map: HashMap::new(),
@@ -43,7 +38,7 @@ where
4338
}
4439
}
4540

46-
pub fn with_sender(capacity: usize, sender: Sender<Event<V>>) -> Self {
41+
pub fn with_sender(capacity: usize, sender: Sender<Event<Value>>) -> Self {
4742
Self {
4843
map: HashMap::new(),
4944
list: Vec::new(),
@@ -53,22 +48,22 @@ where
5348
}
5449
}
5550

56-
pub fn set_event(&mut self, sender: Sender<Event<V>>) {
51+
pub fn set_event(&mut self, sender: Sender<Event<Value>>) {
5752
self.sender = Some(sender);
5853
}
5954

6055
pub fn remove_event(&mut self) {
6156
self.sender = None;
6257
}
6358

64-
fn send_insert(&self, key: Key, value: V) {
59+
fn send_insert(&self, key: Key, value: Value) {
6560
if let Some(sender) = &self.sender {
6661
let event = Event::insert(key, value);
6762
sender.send(event).unwrap();
6863
}
6964
}
7065

71-
fn send_remove(&self, key: Key, value: V) {
66+
fn send_remove(&self, key: Key, value: Value) {
7267
if let Some(sender) = &self.sender {
7368
let event = Event::remove(key, value);
7469
sender.send(event).unwrap();
@@ -82,9 +77,10 @@ where
8277
}
8378
}
8479

85-
pub fn insert<T>(&mut self, key: T, value: V)
80+
pub fn insert<T, V>(&mut self, key: T, value: V)
8681
where
8782
T: Into<String> + Clone + AsRef<str>,
83+
V: ToValueBehavior,
8884
{
8985
let key = key.into();
9086

@@ -108,26 +104,16 @@ where
108104
.unwrap_or(self.list.len());
109105

110106
self.list.insert(position, key.clone());
111-
self.map.insert(key.clone(), value.clone().into());
107+
self.map.insert(key.clone(), value.to_value());
112108

113-
self.send_insert(key, value);
109+
self.send_insert(key, value.to_value());
114110
}
115111

116-
pub fn insert_if_not_exists(&mut self, key: Key, value: V) -> Result<(), Error> {
117-
if self.map.contains_key(&key) {
118-
return Err(Error::SortKeyExists);
119-
}
120-
121-
self.insert(key, value);
122-
123-
Ok(())
124-
}
125-
126-
pub fn get(&self, key: &str) -> Option<&V> {
112+
pub fn get(&self, key: &str) -> Option<&Value> {
127113
self.map.get(key)
128114
}
129115

130-
pub fn get_mut(&mut self, key: &str) -> Option<&mut V> {
116+
pub fn get_mut(&mut self, key: &str) -> Option<&mut Value> {
131117
self.map.get_mut(key)
132118
}
133119

@@ -175,7 +161,7 @@ where
175161
self.map.contains_key(key)
176162
}
177163

178-
pub fn list<T>(&self, props: T) -> Result<Vec<(Key, &V)>, Error>
164+
pub fn list<T>(&self, props: T) -> Result<Vec<(Key, &Value)>, Error>
179165
where
180166
T: Into<ListProps>,
181167
{
@@ -191,7 +177,7 @@ where
191177
&self,
192178
mut list_iter: I,
193179
props: ListProps,
194-
) -> Result<Vec<(Key, &V)>, Error>
180+
) -> Result<Vec<(Key, &Value)>, Error>
195181
where
196182
I: Iterator<Item = &'a Key>,
197183
{

src/event.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ pub struct EventData<V> {
1414
}
1515

1616
impl<V> Event<V> {
17-
pub(crate) fn insert(key: Key, value: V) -> Self {
17+
pub fn insert(key: Key, value: V) -> Self {
1818
Self::Insert(EventData { key, value })
1919
}
2020

21-
pub(crate) fn remove(key: Key, value: V) -> Self {
21+
pub fn remove(key: Key, value: V) -> Self {
2222
Self::Remove(EventData { key, value })
2323
}
2424

25-
pub(crate) fn clear() -> Self {
25+
pub fn clear() -> Self {
2626
Self::Clear
2727
}
2828
}

src/lib.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//! - List cache entries with support for filtering, ordering, and limiting results
1111
//! - Custom error handling
1212
//! - Event notifications for cache operations
13+
//! - Support for generic values using [valu3](https://github.com/lowcarboncode/valu3)
1314
//!
1415
//! ## Installation
1516
//!
@@ -18,14 +19,16 @@
1819
//! ```toml
1920
//! [dependencies]
2021
//! quickleaf = "0.1"
22+
//! valu3 = "0.1"
2123
//! ```
2224
//!
2325
//! ## Usage
2426
//!
2527
//! Here's a basic example of how to use Quickleaf Cache:
2628
//!
2729
//! ```rust
28-
//! use quickleaf::{Quickleaf, ListProps, Order, Filter};
30+
//! use quickleaf::{Quickleaf, ListProps, Order, Filter, prelude::*};
31+
//! use quickleaf::valu3::value::Value;
2932
//!
3033
//! fn main() {
3134
//! let mut cache = Quickleaf::new(2);
@@ -34,12 +37,11 @@
3437
//! cache.insert("key3", 3);
3538
//!
3639
//! assert_eq!(cache.get("key1"), None);
37-
//! assert_eq!(cache.get("key2"), Some(&2));
38-
//! assert_eq!(cache.get("key3"), Some(&3));
40+
//! assert_eq!(cache.get("key2"), Some(&2.to_value()));
41+
//! assert_eq!(cache.get("key3"), Some(&3.to_value()));
3942
//!
4043
//! let list_props = ListProps::default()
41-
//! .order(Order::Asc)
42-
//! .limit(10);
44+
//! .order(Order::Asc);
4345
//!
4446
//! let result = cache.list(list_props).unwrap();
4547
//! for (key, value) in result {
@@ -57,6 +59,7 @@
5759
//! ```rust
5860
//! use quickleaf::{Quickleaf, ListProps, Order, Filter};
5961
//!
62+
//!
6063
//! fn main() {
6164
//! let mut cache = Quickleaf::new(10);
6265
//! cache.insert("apple", 1);
@@ -65,8 +68,7 @@
6568
//!
6669
//! let list_props = ListProps::default()
6770
//! .order(Order::Asc)
68-
//! .filter(Filter::StartWith("ap"))
69-
//! .limit(10);
71+
//! .filter(Filter::StartWith("ap"));
7072
//!
7173
//! let result = cache.list(list_props).unwrap();
7274
//! for (key, value) in result {
@@ -88,8 +90,7 @@
8890
//!
8991
//! let list_props = ListProps::default()
9092
//! .order(Order::Asc)
91-
//! .filter(Filter::EndWith("apple"))
92-
//! .limit(10);
93+
//! .filter(Filter::EndWith("apple"));
9394
//!
9495
//! let result = cache.list(list_props).unwrap();
9596
//! for (key, value) in result {
@@ -111,8 +112,7 @@
111112
//!
112113
//! let list_props = ListProps::default()
113114
//! .order(Order::Asc)
114-
//! .filter(Filter::StartAndEndWith("apple", "pie"))
115-
//! .limit(10);
115+
//! .filter(Filter::StartAndEndWith("apple", "pie"));
116116
//!
117117
//! let result = cache.list(list_props).unwrap();
118118
//! for (key, value) in result {
@@ -126,8 +126,9 @@
126126
//! You can use events to get notified when cache entries are inserted, removed, or cleared. Here is an example:
127127
//!
128128
//! ```rust
129-
//! use quickleaf::{Quickleaf, Event};
129+
//! use quickleaf::{Quickleaf, Event, prelude::*};
130130
//! use std::sync::mpsc::channel;
131+
//! use quickleaf::valu3::value::Value;
131132
//!
132133
//! fn main() {
133134
//! let (tx, rx) = channel();
@@ -150,15 +151,15 @@
150151
//! assert_eq!(items.len(), 3);
151152
//! assert_eq!(
152153
//! items[0],
153-
//! Event::insert("key1".to_string(), 1)
154+
//! Event::insert("key1".to_string(), 1.to_value())
154155
//! );
155156
//! assert_eq!(
156157
//! items[1],
157-
//! Event::insert("key2".to_string(), 2)
158+
//! Event::insert("key2".to_string(), 2.to_value())
158159
//! );
159160
//! assert_eq!(
160161
//! items[2],
161-
//! Event::insert("key3".to_string(), 3)
162+
//! Event::insert("key3".to_string(), 3.to_value())
162163
//! );
163164
//! }
164165
//! ```
@@ -176,15 +177,16 @@ mod error;
176177
mod event;
177178
mod filter;
178179
mod list_props;
180+
pub mod prelude;
181+
mod quickleaf;
179182
#[cfg(test)]
180183
mod tests;
181184

182185
pub use cache::Cache;
183186
pub use error::Error;
184187
pub use event::{Event, EventData};
185188
pub use filter::Filter;
186-
pub use list_props::ListProps;
189+
pub use list_props::{ListProps, Order, StartAfter};
190+
pub use quickleaf::Quickleaf;
187191
pub use valu3;
188192
pub use valu3::value::Value;
189-
190-
pub type Quickleaf = Cache<Value>;

src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub use valu3::prelude::*;

src/quickleaf.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
use crate::Cache;
2+
pub type Quickleaf = Cache;

0 commit comments

Comments
 (0)