individual_crates/
individual_crates.rs

1// Example: Using Individual Crates (rust-queries-core + rust-queries-derive)
2//
3// This example demonstrates how to use the individual crates directly
4// instead of the umbrella rust-queries-builder crate.
5//
6// Cargo.toml:
7// [dependencies]
8// rust-queries-core = "0.6.0"
9// rust-queries-derive = "0.6.0"
10// key-paths-core = "1.0.1"
11// key-paths-derive = "0.5.0"
12
13// Import from individual crates
14use rust_queries_core::{Query, QueryExt};  // Core functionality (LazyQuery available via QueryExt)
15use rust_queries_derive::QueryBuilder;      // Derive macro
16use key_paths_derive::Keypaths;             // Key-paths
17
18#[derive(Debug, Clone, Keypaths, QueryBuilder)]
19struct Product {
20    id: u32,
21    name: String,
22    price: f64,
23    category: String,
24    stock: u32,
25}
26
27fn main() {
28    println!("Individual Crates Example");
29    println!("=========================\n");
30    println!("Using rust-queries-core + rust-queries-derive directly\n");
31
32    let products = vec![
33        Product {
34            id: 1,
35            name: "Laptop".to_string(),
36            price: 999.99,
37            category: "Electronics".to_string(),
38            stock: 5,
39        },
40        Product {
41            id: 2,
42            name: "Mouse".to_string(),
43            price: 29.99,
44            category: "Electronics".to_string(),
45            stock: 50,
46        },
47        Product {
48            id: 3,
49            name: "Keyboard".to_string(),
50            price: 79.99,
51            category: "Electronics".to_string(),
52            stock: 30,
53        },
54        Product {
55            id: 4,
56            name: "Monitor".to_string(),
57            price: 299.99,
58            category: "Electronics".to_string(),
59            stock: 12,
60        },
61        Product {
62            id: 5,
63            name: "Desk Chair".to_string(),
64            price: 199.99,
65            category: "Furniture".to_string(),
66            stock: 8,
67        },
68    ];
69
70    println!("1. QueryExt from rust_queries_core");
71    println!("   products.query().where_(price > 100).all()");
72    
73    let query = products
74        .query()  // From QueryExt trait
75        .where_(Product::price_r(), |&p| p > 100.0);
76    let expensive = query.all();
77    
78    println!("   Found {} expensive products:", expensive.len());
79    for product in expensive {
80        println!("   - {} (${:.2})", product.name, product.price);
81    }
82    println!();
83
84    println!("2. QueryBuilder from rust_queries_derive");
85    println!("   Product::query(&products).where_(stock < 10).all()");
86    
87    let query2 = Product::query(&products)  // From QueryBuilder derive
88        .where_(Product::stock_r(), |&s| s < 10);
89    let low_stock = query2.all();
90    
91    println!("   Found {} low stock products:", low_stock.len());
92    for product in low_stock {
93        println!("   - {} (stock: {})", product.name, product.stock);
94    }
95    println!();
96
97    println!("3. LazyQuery from rust_queries_core");
98    println!("   products.lazy_query().where_(...).collect()");
99    
100    let electronics: Vec<_> = products
101        .lazy_query()  // From QueryExt trait
102        .where_(Product::category_r(), |cat| cat == "Electronics")
103        .collect();
104    
105    println!("   Found {} electronics:", electronics.len());
106    for product in electronics {
107        println!("   - {}", product.name);
108    }
109    println!();
110
111    println!("4. Aggregations with LazyQuery");
112    println!("   products.lazy_query().sum_by(Product::price_r())");
113    
114    let total_value: f64 = products
115        .lazy_query()
116        .sum_by(Product::price_r());
117    
118    println!("   Total inventory value: ${:.2}", total_value);
119    println!();
120
121    println!("5. Early termination with lazy queries");
122    println!("   products.lazy_query().where_(...).first()");
123    
124    if let Some(first_cheap) = products
125        .lazy_query()
126        .where_(Product::price_r(), |&p| p < 50.0)
127        .first()
128    {
129        println!("   First cheap product: {} (${:.2})", first_cheap.name, first_cheap.price);
130    }
131    println!();
132
133    println!("6. Using Query (eager) from rust_queries_core");
134    println!("   Query::new(&products).where_(...).count()");
135    
136    let query3 = Query::new(&products)  // Traditional approach
137        .where_(Product::price_r(), |&p| p > 50.0);
138    let count = query3.count();
139    
140    println!("   Products over $50: {}", count);
141    println!();
142
143    println!("Summary:");
144    println!("--------");
145    println!("✓ rust_queries_core provides: Query, LazyQuery, QueryExt");
146    println!("✓ rust_queries_derive provides: #[derive(QueryBuilder)]");
147    println!("✓ key_paths_derive provides: #[derive(Keypaths)]");
148    println!("✓ All features work with individual crates!");
149}
150