doc_examples/
doc_examples.rs

1// Test that all documentation examples compile correctly
2// cargo run --example doc_examples
3
4use rust_queries_builder::{Query, JoinQuery};
5use key_paths_derive::Keypaths;
6
7#[derive(Debug, Clone, Keypaths)]
8struct Product {
9    id: u32,
10    name: String,
11    price: f64,
12    category: String,
13    stock: u32,
14    rating: f64,
15}
16
17#[derive(Debug, Clone, Keypaths)]
18struct User {
19    id: u32,
20    name: String,
21}
22
23#[derive(Debug, Clone, Keypaths)]
24struct Order {
25    id: u32,
26    user_id: u32,
27    total: f64,
28}
29
30fn main() {
31    println!("Testing documentation examples...\n");
32
33    // Example from README - Quick Start
34    println!("Test 1: README Quick Start Example");
35    {
36        let products = vec![
37            Product { id: 1, name: "Laptop".to_string(), price: 999.99, category: "Electronics".to_string(), stock: 15, rating: 4.5 },
38            Product { id: 2, name: "Mouse".to_string(), price: 29.99, category: "Electronics".to_string(), stock: 50, rating: 4.0 },
39            Product { id: 3, name: "Desk".to_string(), price: 299.99, category: "Furniture".to_string(), stock: 10, rating: 4.8 },
40        ];
41
42        let affordable_query = Query::new(&products)
43            .where_(Product::category_r(), |cat| cat == "Electronics")
44            .where_(Product::price_r(), |&price| price < 100.0);
45        let affordable_electronics = affordable_query.all();
46
47        println!("  Found {} affordable electronics ✅", affordable_electronics.len());
48    }
49
50    // Example from README - Filtering
51    println!("\nTest 2: Filtering Example");
52    {
53        let products = vec![
54            Product { id: 1, name: "Laptop".to_string(), price: 999.99, category: "Electronics".to_string(), stock: 15, rating: 4.5 },
55        ];
56
57        let electronics_query = Query::new(&products)
58            .where_(Product::category_r(), |cat| cat == "Electronics");
59        let electronics = electronics_query.all();
60
61        println!("  Found {} electronics ✅", electronics.len());
62    }
63
64    // Example from README - Selecting
65    println!("\nTest 3: Selecting Fields Example");
66    {
67        let products = vec![
68            Product { id: 1, name: "Laptop".to_string(), price: 999.99, category: "Electronics".to_string(), stock: 15, rating: 4.5 },
69            Product { id: 2, name: "Mouse".to_string(), price: 29.99, category: "Electronics".to_string(), stock: 50, rating: 4.0 },
70        ];
71
72        let names: Vec<String> = Query::new(&products)
73            .select(Product::name_r());
74
75        println!("  Selected {} names ✅", names.len());
76    }
77
78    // Example from README - Ordering
79    println!("\nTest 4: Ordering Example");
80    {
81        let products = vec![
82            Product { id: 1, name: "Laptop".to_string(), price: 999.99, category: "Electronics".to_string(), stock: 15, rating: 4.5 },
83            Product { id: 2, name: "Mouse".to_string(), price: 29.99, category: "Electronics".to_string(), stock: 50, rating: 4.0 },
84        ];
85
86        let by_price = Query::new(&products).order_by_float(Product::price_r());
87        println!("  Ordered {} products ✅", by_price.len());
88    }
89
90    // Example from README - Aggregations
91    println!("\nTest 5: Aggregations Example");
92    {
93        let products = vec![
94            Product { id: 1, name: "Laptop".to_string(), price: 999.99, category: "Electronics".to_string(), stock: 15, rating: 4.5 },
95            Product { id: 2, name: "Mouse".to_string(), price: 29.99, category: "Electronics".to_string(), stock: 50, rating: 4.0 },
96        ];
97
98        let electronics_query = Query::new(&products)
99            .where_(Product::category_r(), |cat| cat == "Electronics");
100
101        let count = electronics_query.count();
102        let total_value: f64 = electronics_query.sum(Product::price_r());
103        let avg_price = electronics_query.avg(Product::price_r()).unwrap_or(0.0);
104
105        println!("  Count: {}, Total: ${:.2}, Avg: ${:.2} ✅", count, total_value, avg_price);
106    }
107
108    // Example from README - Grouping
109    println!("\nTest 6: Grouping Example");
110    {
111        let products = vec![
112            Product { id: 1, name: "Laptop".to_string(), price: 999.99, category: "Electronics".to_string(), stock: 15, rating: 4.5 },
113            Product { id: 2, name: "Mouse".to_string(), price: 29.99, category: "Electronics".to_string(), stock: 50, rating: 4.0 },
114            Product { id: 3, name: "Desk".to_string(), price: 299.99, category: "Furniture".to_string(), stock: 10, rating: 4.8 },
115        ];
116
117        let by_category = Query::new(&products).group_by(Product::category_r());
118        println!("  Grouped into {} categories ✅", by_category.len());
119    }
120
121    // Example from README - Pagination
122    println!("\nTest 7: Pagination Example");
123    {
124        let products = vec![
125            Product { id: 1, name: "P1".to_string(), price: 10.0, category: "A".to_string(), stock: 1, rating: 4.0 },
126            Product { id: 2, name: "P2".to_string(), price: 20.0, category: "A".to_string(), stock: 1, rating: 4.0 },
127            Product { id: 3, name: "P3".to_string(), price: 30.0, category: "A".to_string(), stock: 1, rating: 4.0 },
128        ];
129
130        let query = Query::new(&products);
131        let first_10 = query.limit(10);
132        let page_1 = query.skip(0).limit(10);
133
134        println!("  Limited to {} products ✅", first_10.len());
135        println!("  Page 1 has {} products ✅", page_1.len());
136    }
137
138    // Example from README - Join
139    println!("\nTest 8: Join Example");
140    {
141        let users = vec![
142            User { id: 1, name: "Alice".to_string() },
143            User { id: 2, name: "Bob".to_string() },
144        ];
145
146        let orders = vec![
147            Order { id: 101, user_id: 1, total: 99.99 },
148            Order { id: 102, user_id: 1, total: 149.99 },
149        ];
150
151        let user_orders = JoinQuery::new(&users, &orders).inner_join(
152            User::id_r(),
153            Order::user_id_r(),
154            |user, order| (user.name.clone(), order.total),
155        );
156
157        println!("  Joined {} user-order pairs ✅", user_orders.len());
158    }
159
160    // Example from SQL_COMPARISON - SELECT with WHERE
161    println!("\nTest 9: SQL Comparison - SELECT with WHERE");
162    {
163        #[derive(Clone, Keypaths)]
164        struct Employee {
165            department: String,
166        }
167
168        let employees = vec![
169            Employee { department: "Engineering".to_string() },
170            Employee { department: "Sales".to_string() },
171        ];
172
173        let engineering_query = Query::new(&employees)
174            .where_(Employee::department_r(), |dept| dept == "Engineering");
175        let engineering = engineering_query.all();
176
177        println!("  Found {} engineering employees ✅", engineering.len());
178    }
179
180    // Example from USAGE.md - Complex Filtering
181    println!("\nTest 10: USAGE - Complex Filtering");
182    {
183        let products = vec![
184            Product { id: 1, name: "Laptop".to_string(), price: 999.99, category: "Electronics".to_string(), stock: 15, rating: 4.5 },
185        ];
186
187        let results_query = Query::new(&products)
188            .where_(Product::category_r(), |cat| cat == "Electronics")
189            .where_(Product::price_r(), |&price| price >= 100.0 && price <= 500.0)
190            .where_(Product::stock_r(), |&stock| stock > 10);
191        let results = results_query.order_by_float(Product::price_r());
192
193        println!("  Filtered {} products ✅", results.len());
194    }
195
196    println!("\n✅ All documentation examples compile and run successfully!");
197}
198