use rullst_orm::schema::{Blueprint, Schema};
use rullst_orm::{Orm, RullstCollection, sqlx::FromRow};
#[derive(Debug, Clone, FromRow, rullst_orm::Orm)]
#[orm(table = "products")]
pub struct Product {
pub id: i32,
pub name: String,
pub price: f64,
pub category: String,
}
#[tokio::main]
async fn main() -> Result<(), rullst_orm::sqlx::Error> {
let _ = std::fs::remove_file("collections_test.db");
std::fs::File::create("collections_test.db").unwrap();
Orm::init("sqlite://collections_test.db").await?;
Schema::create("products", |table: &mut Blueprint| {
table.id();
table.string("name").not_null();
table.float("price").not_null();
table.string("category").not_null();
})
.await?;
let mut p1 = Product {
id: 0,
name: "Laptop".to_string(),
price: 1200.50,
category: "Tech".to_string(),
};
p1.save().await?;
let mut p2 = Product {
id: 0,
name: "Mouse".to_string(),
price: 45.00,
category: "Tech".to_string(),
};
p2.save().await?;
let mut p3 = Product {
id: 0,
name: "Desk".to_string(),
price: 250.00,
category: "Furniture".to_string(),
};
p3.save().await?;
let mut p4 = Product {
id: 0,
name: "Chair".to_string(),
price: 150.00,
category: "Furniture".to_string(),
};
p4.save().await?;
let collection = Product::all().await?;
println!("--- Testing Orm Collections ---\n");
let names = collection.implode(", ", |p| p.name.clone());
println!("1. Implode Names: {}", names);
let total_price: f64 = collection.sum_by(|p| p.price);
println!("2. Total Price of all items: ${:.2}", total_price);
let most_expensive = collection.max_by_key(|p| (p.price * 100.0) as i64).unwrap(); println!(
"3. Most Expensive Item: {} (${:.2})",
most_expensive.name, most_expensive.price
);
let chunks = collection.clone().chunk(2);
println!("4. Chunks of 2:");
for (i, chunk) in chunks.iter().enumerate() {
println!(
" Chunk {}: {:?}",
i + 1,
chunk.iter().map(|p| p.name.clone()).collect::<Vec<_>>()
);
}
let keyed_by_id = collection.key_by(|p| p.id);
println!("5. Key By ID:");
println!(" Product ID 3 is: {}", keyed_by_id.get(&3).unwrap().name);
Ok(())
}