rust-queries-builder 1.0.7

A powerful, type-safe query builder library for Rust that leverages key-paths for SQL-like operations on in-memory collections
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# Macro Helpers Guide

## Overview

Version 0.4.0 introduces **12 helper macros** that reduce boilerplate code and make common query patterns more concise.

## Quick Reference

| Macro | Description | Code Reduction |
|-------|-------------|----------------|
| `lazy_query!` | Create LazyQuery | ~15 chars |
| `query!` | Create Query | ~10 chars |
| `collect_lazy!` | Collect all items | ~20 chars |
| `filter_collect!` | Filter and collect | ~35 chars |
| `count_where!` | Count with filter | ~30 chars |
| `find_first!` | Find first match | ~30 chars |
| `exists_where!` | Check existence | ~30 chars |
| `paginate!` | Pagination | ~45 chars |
| `sum_where!` | Sum with filter | ~25 chars |
| `avg_where!` | Average with filter | ~25 chars |
| `select_all!` | Select field | ~20 chars |
| `select_where!` | Select with filter | ~40 chars |

**Total savings**: 20-45 characters per operation!

## Usage Examples

### 1. lazy_query! - Create LazyQuery

**Before:**
```rust
let query = LazyQuery::new(&products);
```

**After:**
```rust
let query = lazy_query!(&products);
```

**Saved**: 15 characters

### 2. collect_lazy! - Simple Collection

**Before:**
```rust
let results: Vec<_> = LazyQuery::new(&products).collect();
```

**After:**
```rust
let results = collect_lazy!(&products);
```

**Saved**: 20 characters

### 3. filter_collect! - Filter and Collect

**Before:**
```rust
let electronics: Vec<_> = LazyQuery::new(&products)
    .where_(Product::category_r(), |cat| cat == "Electronics")
    .collect();
```

**After:**
```rust
let electronics = filter_collect!(
    &products,
    Product::category_r(),
    |cat| cat == "Electronics"
);
```

**Saved**: ~35 characters, more readable

### 4. count_where! - Count with Filter

**Before:**
```rust
let count = LazyQuery::new(&products)
    .where_(Product::stock_r(), |&s| s > 0)
    .count();
```

**After:**
```rust
let count = count_where!(&products, Product::stock_r(), |&s| s > 0);
```

**Saved**: ~30 characters

### 5. find_first! - Find First Match

**Before:**
```rust
let found = LazyQuery::new(&products)
    .where_(Product::price_r(), |&p| p > 500.0)
    .first();
```

**After:**
```rust
let found = find_first!(&products, Product::price_r(), |&p| p > 500.0);
```

**Saved**: ~30 characters

### 6. exists_where! - Existence Check

**Before:**
```rust
let has_furniture = LazyQuery::new(&products)
    .where_(Product::category_r(), |cat| cat == "Furniture")
    .any();
```

**After:**
```rust
let has_furniture = exists_where!(
    &products,
    Product::category_r(),
    |cat| cat == "Furniture"
);
```

**Saved**: ~30 characters

### 7. paginate! - Easy Pagination

**Before:**
```rust
let page_2: Vec<_> = LazyQuery::new(&products)
    .skip_lazy(2 * 10)  // page * size
    .take_lazy(10)
    .collect();
```

**After:**
```rust
let page_2 = paginate!(&products, page: 2, size: 10);
```

**Saved**: ~45 characters, much clearer intent

### 8. sum_where! - Sum with Filter

**Before:**
```rust
let total: f64 = LazyQuery::new(&products)
    .where_(Product::active_r(), |&a| a)
    .sum_by(Product::price_r());
```

**After:**
```rust
let total = sum_where!(
    &products,
    Product::price_r(),
    Product::active_r(),
    |&a| a
);
```

**Saved**: ~25 characters

**Without filter:**
```rust
let total = sum_where!(&products, Product::price_r());
```

### 9. avg_where! - Average with Filter

**Before:**
```rust
let avg = LazyQuery::new(&products)
    .where_(Product::category_r(), |cat| cat == "Electronics")
    .avg_by(Product::price_r())
    .unwrap_or(0.0);
```

**After:**
```rust
let avg = avg_where!(
    &products,
    Product::price_r(),
    Product::category_r(),
    |cat| cat == "Electronics"
).unwrap_or(0.0);
```

**Saved**: ~25 characters

**Without filter:**
```rust
let avg = avg_where!(&products, Product::price_r()).unwrap_or(0.0);
```

### 10. select_all! - Select Field

**Before:**
```rust
let names: Vec<String> = LazyQuery::new(&products)
    .select_lazy(Product::name_r())
    .collect();
```

**After:**
```rust
let names: Vec<String> = select_all!(&products, Product::name_r());
```

**Saved**: ~20 characters

### 11. select_where! - Select with Filter

**Before:**
```rust
let furniture_names: Vec<String> = LazyQuery::new(&products)
    .where_(Product::category_r(), |cat| cat == "Furniture")
    .select_lazy(Product::name_r())
    .collect();
```

**After:**
```rust
let furniture_names: Vec<String> = select_where!(
    &products,
    Product::name_r(),
    Product::category_r(),
    |cat| cat == "Furniture"
);
```

**Saved**: ~40 characters

### 12. query! - Create Query

**Before:**
```rust
let query = Query::new(&products);
```

**After:**
```rust
let query = query!(&products);
```

**Saved**: ~10 characters

## Real-World Examples

### Before Macros (Verbose)

```rust
// Count active electronics
let count = LazyQuery::new(&products)
    .where_(Product::category_r(), |cat| cat == "Electronics")
    .where_(Product::active_r(), |&a| a)
    .count();

// Get page 3
let page: Vec<_> = LazyQuery::new(&products)
    .skip_lazy(3 * 20)
    .take_lazy(20)
    .collect();

// Find expensive item
let expensive = LazyQuery::new(&products)
    .where_(Product::price_r(), |&p| p > 1000.0)
    .first();

// Get all names
let names: Vec<String> = LazyQuery::new(&products)
    .select_lazy(Product::name_r())
    .collect();

// Total value of active products
let total: f64 = LazyQuery::new(&products)
    .where_(Product::active_r(), |&a| a)
    .sum_by(Product::price_r());
```

**Total**: ~500 characters

### After Macros (Concise)

```rust
// Count active electronics (still need chaining for multiple filters)
let count = count_where!(&products, Product::active_r(), |&a| a);

// Get page 3
let page = paginate!(&products, page: 3, size: 20);

// Find expensive item
let expensive = find_first!(&products, Product::price_r(), |&p| p > 1000.0);

// Get all names
let names: Vec<String> = select_all!(&products, Product::name_r());

// Total value of active products
let total = sum_where!(&products, Product::price_r(), Product::active_r(), |&a| a);
```

**Total**: ~350 characters  
**Savings**: 30% less code!

## When to Use Macros

### ✅ Use Macros For:

- Single filter + terminal operation
- Common patterns (pagination, counting, sum)
- Quick prototyping
- Simple queries
- Code golf / conciseness

### ⚠️ Use Full API For:

- Multiple filters (still need chaining)
- Complex predicates
- Custom transformations
- When clarity is more important than brevity
- Library/framework code

## Combining Macros with Full API

You can mix macros and full API:

```rust
// Start with macro
let base = lazy_query!(&products);

// Continue with full API
let results: Vec<_> = base
    .where_(Product::category_r(), |cat| cat == "Electronics")
    .where_(Product::price_r(), |&p| p < 500.0)
    .where_(Product::stock_r(), |&s| s > 0)
    .take_lazy(10)
    .collect();
```

## Performance

**All macros are zero-cost abstractions!**

They expand at compile-time to the same code you would write manually:

```rust
// This macro:
count_where!(&products, Product::stock_r(), |&s| s > 0)

// Expands to exactly:
LazyQuery::new(&products)
    .where_(Product::stock_r(), |&s| s > 0)
    .count()

// Same compiled code, same performance!
```

## Type Safety

Macros maintain full type safety:

```rust
// ✅ Compiles
let count = count_where!(&products, Product::price_r(), |&p| p > 100.0);

// ❌ Won't compile - type mismatch
let count = count_where!(&products, Product::price_r(), |p| p == "100");

// ❌ Won't compile - wrong field
let count = count_where!(&products, Product::nonexistent_r(), |&x| x > 0);
```

All compile-time checking is preserved!

## Complete Example

Run the demonstration:

```bash
cargo run --example macro_helpers
```

**Output shows:**
```
✅ 12 helper macros provided:
   • lazy_query! - Create LazyQuery
   • query! - Create Query
   • collect_lazy! - Quick collect
   [... all 12 macros ...]

📊 Benefits:
   • Less typing (20-45 characters saved per operation)
   • More readable code
   • Common patterns encapsulated
   • Same performance (zero-cost abstraction)
   • Type-safe (compile-time checked)
```

## Macro List

### Creation Macros

```rust
lazy_query!(&data)          // → LazyQuery::new(&data)
query!(&data)               // → Query::new(&data)
```

### Collection Macros

```rust
collect_lazy!(&data)                           // Collect all
filter_collect!(&data, field, pred)            // Filter + collect
select_all!(&data, field)                      // Select field
select_where!(&data, sel_field, filt_field, pred)  // Select with filter
```

### Search Macros

```rust
find_first!(&data, field, pred)     // Find first match
exists_where!(&data, field, pred)   // Check existence
count_where!(&data, field, pred)    // Count matches
```

### Aggregation Macros

```rust
sum_where!(&data, sum_field, filter_field, pred)   // Sum with filter
sum_where!(&data, sum_field)                       // Sum all
avg_where!(&data, avg_field, filter_field, pred)   // Average with filter
avg_where!(&data, avg_field)                       // Average all
```

### Utility Macros

```rust
paginate!(&data, page: p, size: s)  // Easy pagination
```

## Migration Guide

### Step 1: Import Macros

```rust
use rust_queries_builder::{
    lazy_query, filter_collect, count_where, 
    find_first, paginate, sum_where, // ... etc
};
```

### Step 2: Replace Common Patterns

```rust
// Before
let count = LazyQuery::new(&products).where_(...).count();

// After
let count = count_where!(&products, field, pred);
```

### Step 3: Keep Complex Queries As-Is

```rust
// Complex queries still use full API
let results: Vec<_> = LazyQuery::new(&products)
    .where_(Product::category_r(), |cat| cat == "Electronics")
    .where_(Product::price_r(), |&p| p > 100.0 && p < 500.0)
    .where_(Product::stock_r(), |&s| s > 0)
    .where_(Product::rating_r(), |&r| r > 4.5)
    .take_lazy(10)
    .collect();
```

## See Also

- [examples/macro_helpers.rs]examples/macro_helpers.rs - Complete demonstrations
- [src/macros.rs]src/macros.rs - Macro implementations
- [LAZY_EVALUATION.md]LAZY_EVALUATION.md - Lazy query guide