improve and add models
This commit is contained in:
38
heromodels/examples/library_rhai/example.rs
Normal file
38
heromodels/examples/library_rhai/example.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use heromodels::db::hero::OurDB;
|
||||
use heromodels::models::register_library_rhai_module;
|
||||
use rhai::Engine;
|
||||
use std::sync::Arc;
|
||||
use std::{fs, path::Path};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Initialize Rhai engine
|
||||
let mut engine = Engine::new();
|
||||
|
||||
// Initialize database with OurDB
|
||||
let db_path = "temp_library_db";
|
||||
// Clean up previous database file if it exists
|
||||
if Path::new(db_path).exists() {
|
||||
fs::remove_dir_all(db_path)?;
|
||||
}
|
||||
let db = Arc::new(OurDB::new(db_path, true).expect("Failed to create database"));
|
||||
|
||||
// Register the library module with Rhai
|
||||
register_library_rhai_module(&mut engine, db.clone());
|
||||
|
||||
// Load and evaluate the Rhai script
|
||||
let script_path = Path::new(file!()).parent().unwrap().join("library.rhai");
|
||||
let script = fs::read_to_string(&script_path)?;
|
||||
|
||||
println!("--- Running Library Rhai Script ---");
|
||||
match engine.eval::<()>(&script) {
|
||||
Ok(_) => println!("\n--- Script executed successfully! ---"),
|
||||
Err(e) => eprintln!("\n--- Script execution failed: {} ---", e),
|
||||
}
|
||||
|
||||
// Clean up the database file
|
||||
fs::remove_dir_all(db_path)?;
|
||||
println!("--- Cleaned up temporary database. ---");
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
78
heromodels/examples/library_rhai/library.rhai
Normal file
78
heromodels/examples/library_rhai/library.rhai
Normal file
@@ -0,0 +1,78 @@
|
||||
// heromodels/examples/library_rhai/library.rhai
|
||||
|
||||
print("--- Testing Library Rhai Module ---");
|
||||
|
||||
// --- Image ---
|
||||
print("\n1. Creating and saving an image...");
|
||||
let my_image = new_image()
|
||||
.title("A Beautiful Sunset")
|
||||
.description("A photo taken from a drone.")
|
||||
.url("https://example.com/sunset.jpg")
|
||||
.width(1920)
|
||||
.height(1080);
|
||||
|
||||
let saved_image = save_image(my_image);
|
||||
print(" > Saved image with ID: " + saved_image.id);
|
||||
let image_id = saved_image.id;
|
||||
|
||||
// --- PDF ---
|
||||
print("\n2. Creating and saving a PDF...");
|
||||
let my_pdf = new_pdf()
|
||||
.title("Rust Programming Guide")
|
||||
.description("A comprehensive guide to Rust.")
|
||||
.url("https://example.com/rust.pdf")
|
||||
.page_count(500);
|
||||
|
||||
let saved_pdf = save_pdf(my_pdf);
|
||||
print(" > Saved PDF with ID: " + saved_pdf.id);
|
||||
let pdf_id = saved_pdf.id;
|
||||
|
||||
// --- Markdown ---
|
||||
print("\n3. Creating and saving a Markdown document...");
|
||||
let my_markdown = new_markdown()
|
||||
.title("Meeting Notes")
|
||||
.description("Notes from the weekly sync.")
|
||||
.content("# Meeting Notes\n\n- Discussed project status.\n- Planned next sprint.");
|
||||
|
||||
let saved_markdown = save_markdown(my_markdown);
|
||||
print(" > Saved Markdown with ID: " + saved_markdown.id);
|
||||
let markdown_id = saved_markdown.id;
|
||||
|
||||
// --- Collection ---
|
||||
print("\n4. Creating a collection and adding items...");
|
||||
let my_collection = new_collection()
|
||||
.title("My Awesome Collection")
|
||||
.description("A collection of various media.")
|
||||
.add_image(image_id)
|
||||
.add_pdf(pdf_id)
|
||||
.add_markdown(markdown_id);
|
||||
|
||||
let saved_collection = save_collection(my_collection);
|
||||
print(" > Saved collection with ID: " + saved_collection.id);
|
||||
let collection_id = saved_collection.id;
|
||||
|
||||
// --- Verification ---
|
||||
print("\n5. Verifying saved data...");
|
||||
let fetched_collection = get_collection(collection_id);
|
||||
print(" > Fetched collection: '" + fetched_collection.title + "'");
|
||||
print(" > Collection contains " + fetched_collection.images + " image(s).");
|
||||
print(" > Collection contains " + fetched_collection.pdfs + " pdf(s).");
|
||||
print(" > Collection contains " + fetched_collection.markdowns + " markdown(s).");
|
||||
|
||||
let fetched_image = get_image(image_id);
|
||||
print(" > Fetched image title: '" + fetched_image.title + "'");
|
||||
if (fetched_image.url != "https://example.com/sunset.jpg") {
|
||||
throw "Image URL mismatch!";
|
||||
}
|
||||
print(" > Image URL verified.");
|
||||
|
||||
// --- Deletion ---
|
||||
print("\n6. Cleaning up database...");
|
||||
delete_image(image_id);
|
||||
print(" > Deleted image with ID: " + image_id);
|
||||
delete_pdf(pdf_id);
|
||||
print(" > Deleted PDF with ID: " + pdf_id);
|
||||
delete_markdown(markdown_id);
|
||||
print(" > Deleted Markdown with ID: " + markdown_id);
|
||||
delete_collection(collection_id);
|
||||
print(" > Deleted collection with ID: " + collection_id);
|
Reference in New Issue
Block a user