chore: restore lib example after thread safety feature

This commit is contained in:
2025-10-29 11:26:28 +01:00
parent bc4ac445c9
commit 645e7560b3

View File

@@ -32,146 +32,147 @@
//! and the necessary `From` and `TryFrom` conversions. //! and the necessary `From` and `TryFrom` conversions.
//! //!
//! ```rust,no_run //! ```rust,no_run
//! // use heave::*; //! use heave::*;
//! // use std::convert::{From, TryFrom}; //! use std::convert::{From, TryFrom};
//! // use std::result::Result; //! use std::result::Result;
//! //!
//! // // Define a simple struct representing a product. //! // Define a simple struct representing a product.
//! // #[derive(Debug, Default, PartialEq, Clone)] //! #[derive(Debug, Default, PartialEq, Clone)]
//! // struct Product { //! struct Product {
//! // pub id: String, //! pub id: String,
//! // pub name: String, //! pub name: String,
//! // pub price: u64, //! pub price: u64,
//! // pub in_stock: bool, //! pub in_stock: bool,
//! // } //! }
//! //!
//! // // Implement the EAV trait to define the "class" of this entity. //! // Implement the EAV trait to define the "class" of this entity.
//! // impl EAV for Product { //! impl EAV for Product {
//! // fn class() -> &'static str { //! fn class() -> &'static str {
//! // "product" //! "product"
//! // } //! }
//! // } //! }
//! //!
//! // // Convert our Product into a generic Entity. //! // Convert our Product into a generic Entity.
//! // impl From<Product> for Entity { //! impl From<Product> for Entity {
//! // fn from(p: Product) -> Self { //! fn from(p: Product) -> Self {
//! // Entity::new::<Product>() //! Entity::new::<Product>()
//! // .with_id(&p.id) //! .with_id(&p.id)
//! // .with_attribute("name", p.name) //! .with_attribute("name", p.name)
//! // .with_attribute("price", p.price) //! .with_attribute("price", p.price)
//! // .with_attribute("in_stock", p.in_stock) //! .with_attribute("in_stock", p.in_stock)
//! // } //! }
//! // } //! }
//! //!
//! // // Convert a generic Entity back into our Product. //! // Convert a generic Entity back into our Product.
//! // impl TryFrom<Entity> for Product { //! impl TryFrom<Entity> for Product {
//! // type Error = FailedTo; //! type Error = FailedTo;
//! //!
//! // fn try_from(entity: Entity) -> Result<Self, Self::Error> { //! fn try_from(entity: Entity) -> Result<Self, Self::Error> {
//! // Ok(Self { //! Ok(Self {
//! // id: entity.id.clone(), //! id: entity.id.clone(),
//! // name: entity.unwrap("name").map_err(|_| FailedTo::ConvertEntity)?, //! name: entity.unwrap("name").map_err(|_| FailedTo::ConvertEntity)?,
//! // price: entity.unwrap("price").map_err(|_| FailedTo::ConvertEntity)?, //! price: entity.unwrap("price").map_err(|_| FailedTo::ConvertEntity)?,
//! // in_stock: entity.unwrap("in_stock").map_err(|_| FailedTo::ConvertEntity)?, //! in_stock: entity.unwrap("in_stock").map_err(|_| FailedTo::ConvertEntity)?,
//! // }) //! })
//! // } //! }
//! // } //! }
//! //!
//! // fn main() -> Result<(), FailedTo> { //! fn main() -> Result<(), FailedTo> {
//! // let db_path = "my_products.db"; //! let db_path = "my_products.db";
//! //!
//! // // Clean up previous runs if file exists //! // Clean up previous runs if file exists
//! // if std::path::Path::new(db_path).exists() { //! if std::path::Path::new(db_path).exists() {
//! // std::fs::remove_file(db_path).unwrap(); //! std::fs::remove_file(db_path).unwrap();
//! // } //! }
//! //!
//! // // == 1. Initialize and Persist Data == //! // == 1. Initialize and Persist Data ==
//! // let mut catalog = Catalog::new(db_path); //! let mut catalog = Catalog::new(db_path);
//! // catalog.init()?; //! catalog.init()?;
//! //!
//! // let products_to_add = vec![ //! let products_to_add = vec![
//! // Product { //! Product {
//! // id: "p1".to_string(), //! id: "p1".to_string(),
//! // name: "Laptop".to_string(), //! name: "Laptop".to_string(),
//! // price: 1200, //! price: 1200,
//! // in_stock: true, //! in_stock: true,
//! // }, //! },
//! // Product { //! Product {
//! // id: "p2".to_string(), //! id: "p2".to_string(),
//! // name: "Mouse".to_string(), //! name: "Mouse".to_string(),
//! // price: 25, //! price: 25,
//! // in_stock: true, //! in_stock: true,
//! // }, //! },
//! // Product { //! Product {
//! // id: "p3".to_string(), //! id: "p3".to_string(),
//! // name: "Keyboard".to_string(), //! name: "Keyboard".to_string(),
//! // price: 75, //! price: 75,
//! // in_stock: false, //! in_stock: false,
//! // }, //! },
//! // ]; //! ];
//! //!
//! // catalog.insert_many(products_to_add)?; //! catalog.insert_many(products_to_add)?;
//! // catalog.persist()?; //! catalog.persist()?;
//! // println!("✅ Products saved to the database."); //! println!("✅ Products saved to the database.");
//! //!
//! // // == 2. Load and Query Data == //! // == 2. Load and Query Data ==
//! // let mut query_catalog = Catalog::new(db_path); //! let mut query_catalog = Catalog::new(db_path);
//! //!
//! // // Load a single product by its ID. //! // Load a single product by its ID.
//! // query_catalog.load_by_id("p1")?; //! query_catalog.load_by_id("p1")?;
//! // let laptop: Product = query_catalog.get("p1")?.unwrap(); //! let laptop: Product = query_catalog.get("p1")?.unwrap();
//! // println!("✅ Loaded by ID: {:?}", laptop); //! println!("✅ Loaded by ID: {:?}", laptop);
//! // assert_eq!(laptop.name, "Laptop"); //! assert_eq!(laptop.name, "Laptop");
//! //!
//! // // Load products matching a filter (in stock, price < 100) //! // Load products matching a filter (in stock, price < 100)
//! // let filter = Filter::new() //! let filter = Filter::new()
//! // .with_bool("in_stock", true) //! .with_bool("in_stock", true)
//! // .with_unsigned_int("price", Comparison::Lesser, 100); //! .with_unsigned_int("price", Comparison::Lesser, 100);
//! //!
//! // let mut filtered_catalog = Catalog::new(db_path); //! let mut filtered_catalog = Catalog::new(db_path);
//! // filtered_catalog.load_by_filter(&filter)?; //! filtered_catalog.load_by_filter(&filter)?;
//! // let cheap_products: Vec<Product> = filtered_catalog.list_by_class().map(|p| p.unwrap()).collect(); //! let cheap_products: Vec<Product> = filtered_catalog.list_by_class()
//! // println!("✅ Found {} cheap, in-stock product(s).", cheap_products.len()); //! .unwrap().into_iter().map(|p| p.unwrap()).collect();
//! // assert_eq!(cheap_products.len(), 1); //! println!("✅ Found {} cheap, in-stock product(s).", cheap_products.len());
//! // assert_eq!(cheap_products[0].id, "p2"); //! assert_eq!(cheap_products.len(), 1);
//! assert_eq!(cheap_products[0].id, "p2");
//! //!
//! // // == 3. Update and Delete Data == //! // == 3. Update and Delete Data ==
//! // let mut update_catalog = Catalog::new(db_path); //! let mut update_catalog = Catalog::new(db_path);
//! // update_catalog.load_by_class::<Product>()?; //! update_catalog.load_by_class::<Product>()?;
//! //!
//! // // Update the price of the laptop //! // Update the price of the laptop
//! // let mut laptop_to_update: Product = update_catalog.get("p1")?.unwrap(); //! let mut laptop_to_update: Product = update_catalog.get("p1")?.unwrap();
//! // laptop_to_update.price = 1150; //! laptop_to_update.price = 1150;
//! // update_catalog.upsert(laptop_to_update)?; //! update_catalog.upsert(laptop_to_update)?;
//! //!
//! // // Delete the keyboard //! // Delete the keyboard
//! // update_catalog.delete("p3"); //! update_catalog.delete("p3");
//! //!
//! // // Persist all changes //! // Persist all changes
//! // update_catalog.persist()?; //! update_catalog.persist()?;
//! // println!("✅ Laptop price updated and keyboard deleted."); //! println!("✅ Laptop price updated and keyboard deleted.");
//! //!
//! // // == 4. Verify Final State == //! // == 4. Verify Final State ==
//! // let mut final_catalog = Catalog::new(db_path); //! let mut final_catalog = Catalog::new(db_path);
//! // final_catalog.load_by_class::<Product>()?; //! final_catalog.load_by_class::<Product>()?;
//! //!
//! // let final_count = final_catalog.list_by_class::<Product>().count(); //! let final_count = final_catalog.list_by_class::<Product>().into_iter().count();
//! // println!("✅ Final product count: {}", final_count); //! println!("✅ Final product count: {}", final_count);
//! // assert_eq!(final_count, 2); //! assert_eq!(final_count, 2);
//! //!
//! // let updated_laptop: Product = final_catalog.get("p1")?.unwrap(); //! let updated_laptop: Product = final_catalog.get("p1")?.unwrap();
//! // println!("✅ Verified updated laptop price: {}", updated_laptop.price); //! println!("✅ Verified updated laptop price: {}", updated_laptop.price);
//! // assert_eq!(updated_laptop.price, 1150); //! assert_eq!(updated_laptop.price, 1150);
//! //!
//! // let deleted_keyboard: Option<Product> = final_catalog.get("p3")?; //! let deleted_keyboard: Option<Product> = final_catalog.get("p3")?;
//! // println!("✅ Verified keyboard is deleted."); //! println!("✅ Verified keyboard is deleted.");
//! // assert!(deleted_keyboard.is_none()); //! assert!(deleted_keyboard.is_none());
//! //!
//! // // Clean up the created database file //! // Clean up the created database file
//! // std::fs::remove_file(db_path).unwrap(); //! std::fs::remove_file(db_path).unwrap();
//! //!
//! // Ok(()) //! Ok(())
//! // } //! }
//! ``` //! ```
use std::*; use std::*;