Merge branch 'development_add_incremental_mode_to_heromodels'
This commit is contained in:
@@ -3,6 +3,29 @@ use heromodels::models::userexample::user::user_index::{is_active, username};
|
||||
use heromodels::models::{Comment, User};
|
||||
use heromodels_core::Model;
|
||||
|
||||
// Helper function to print user details
|
||||
fn print_user_details(user: &User) {
|
||||
println!("\n--- User Details ---");
|
||||
println!("ID: {}", user.get_id());
|
||||
println!("Username: {}", user.username);
|
||||
println!("Email: {}", user.email);
|
||||
println!("Full Name: {}", user.full_name);
|
||||
println!("Active: {}", user.is_active);
|
||||
println!("Created At: {}", user.base_data.created_at);
|
||||
println!("Modified At: {}", user.base_data.modified_at);
|
||||
println!("Comments: {:?}", user.base_data.comments);
|
||||
}
|
||||
|
||||
// Helper function to print comment details
|
||||
fn print_comment_details(comment: &Comment) {
|
||||
println!("\n--- Comment Details ---");
|
||||
println!("ID: {}", comment.get_id());
|
||||
println!("User ID: {}", comment.user_id);
|
||||
println!("Content: {}", comment.content);
|
||||
println!("Created At: {}", comment.base_data.created_at);
|
||||
println!("Modified At: {}", comment.base_data.modified_at);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Create a new DB instance in /tmp/ourdb, and reset before every run
|
||||
let db = heromodels::db::hero::OurDB::new("/tmp/ourdb", true).expect("Can create DB");
|
||||
@@ -10,50 +33,72 @@ fn main() {
|
||||
println!("Hero Models - Basic Usage Example");
|
||||
println!("================================");
|
||||
|
||||
// Create a new user using the fluent interface
|
||||
let user = User::new(1)
|
||||
// Create users with auto-generated IDs
|
||||
|
||||
// User 1
|
||||
let user1 = User::new()
|
||||
.username("johndoe")
|
||||
.email("john.doe@example.com")
|
||||
.full_name("John Doe")
|
||||
.is_active(false)
|
||||
.build();
|
||||
let user2 = User::new(2)
|
||||
|
||||
// User 2
|
||||
let user2 = User::new()
|
||||
.username("janesmith")
|
||||
.email("jane.smith@example.com")
|
||||
.full_name("Jane Smith")
|
||||
.is_active(true)
|
||||
.build();
|
||||
let user3 = User::new(3)
|
||||
|
||||
// User 3
|
||||
let user3 = User::new()
|
||||
.username("willism")
|
||||
.email("willis.masters@example.com")
|
||||
.full_name("Willis Masters")
|
||||
.is_active(true)
|
||||
.build();
|
||||
let user4 = User::new(4)
|
||||
|
||||
// User 4
|
||||
let user4 = User::new()
|
||||
.username("carrols")
|
||||
.email("carrol.smith@example.com")
|
||||
.full_name("Carrol Smith")
|
||||
.is_active(false)
|
||||
.build();
|
||||
|
||||
db.collection()
|
||||
.expect("can open user collection")
|
||||
.set(&user)
|
||||
.expect("can set user");
|
||||
db.collection()
|
||||
.expect("can open user collection")
|
||||
.set(&user2)
|
||||
.expect("can set user");
|
||||
db.collection()
|
||||
.expect("can open user collection")
|
||||
.set(&user3)
|
||||
.expect("can set user");
|
||||
db.collection()
|
||||
.expect("can open user collection")
|
||||
.set(&user4)
|
||||
.expect("can set user");
|
||||
// Save all users to database and get their assigned IDs and updated models
|
||||
let (user1_id, db_user1) = db.collection().expect("can open user collection").set(&user1).expect("can set user");
|
||||
let (user2_id, db_user2) = db.collection().expect("can open user collection").set(&user2).expect("can set user");
|
||||
let (user3_id, db_user3) = db.collection().expect("can open user collection").set(&user3).expect("can set user");
|
||||
let (user4_id, db_user4) = db.collection().expect("can open user collection").set(&user4).expect("can set user");
|
||||
|
||||
// Perform an indexed lookup on the Username
|
||||
println!("User 1 assigned ID: {}", user1_id);
|
||||
println!("User 2 assigned ID: {}", user2_id);
|
||||
println!("User 3 assigned ID: {}", user3_id);
|
||||
println!("User 4 assigned ID: {}", user4_id);
|
||||
|
||||
// We already have the updated models from the set method, so we don't need to retrieve them again
|
||||
|
||||
// Print all users retrieved from database
|
||||
println!("\n--- Users Retrieved from Database ---");
|
||||
println!("\n1. First user:");
|
||||
print_user_details(&db_user1);
|
||||
|
||||
println!("\n2. Second user:");
|
||||
print_user_details(&db_user2);
|
||||
|
||||
println!("\n3. Third user:");
|
||||
print_user_details(&db_user3);
|
||||
|
||||
println!("\n4. Fourth user:");
|
||||
print_user_details(&db_user4);
|
||||
|
||||
// Demonstrate different ways to retrieve users from the database
|
||||
|
||||
// 1. Retrieve by username index
|
||||
println!("\n--- Retrieving Users by Different Methods ---");
|
||||
println!("\n1. By Username Index:");
|
||||
let stored_users = db
|
||||
.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
@@ -61,72 +106,95 @@ fn main() {
|
||||
.expect("can load stored user");
|
||||
|
||||
assert_eq!(stored_users.len(), 1);
|
||||
let stored_user = &stored_users[0];
|
||||
print_user_details(&stored_users[0]);
|
||||
|
||||
assert_eq!(user.username, stored_user.username);
|
||||
assert_eq!(user.email, stored_user.email);
|
||||
assert_eq!(user.is_active, stored_user.is_active);
|
||||
assert_eq!(user.full_name, stored_user.full_name);
|
||||
|
||||
// Load all active users using the IsActive field index
|
||||
// TODO: expand Index type so it defines the type of the key
|
||||
// 2. Retrieve by active status
|
||||
println!("\n2. By Active Status (Active = true):");
|
||||
let active_users = db
|
||||
.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.get::<is_active, _>(&true)
|
||||
.expect("can load stored users");
|
||||
// We should have 2 active users
|
||||
assert_eq!(active_users.len(), 2);
|
||||
|
||||
// Now remove a user
|
||||
assert_eq!(active_users.len(), 2);
|
||||
for (_i, active_user) in active_users.iter().enumerate() {
|
||||
print_user_details(active_user);
|
||||
}
|
||||
|
||||
// 3. Delete a user and show the updated results
|
||||
println!("\n3. After Deleting a User:");
|
||||
let user_to_delete_id = active_users[0].get_id();
|
||||
println!("Deleting user with ID: {}", user_to_delete_id);
|
||||
db.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.delete_by_id(active_users[0].get_id())
|
||||
.delete_by_id(user_to_delete_id)
|
||||
.expect("can delete existing user");
|
||||
|
||||
// Load the active users again, should be 1 left
|
||||
// Show remaining active users
|
||||
let active_users = db
|
||||
.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.get::<is_active, _>(&true)
|
||||
.expect("can load stored users");
|
||||
|
||||
println!(" a. Remaining Active Users:");
|
||||
assert_eq!(active_users.len(), 1);
|
||||
// And verify we still have 2 inactive users
|
||||
for (_i, active_user) in active_users.iter().enumerate() {
|
||||
print_user_details(active_user);
|
||||
}
|
||||
|
||||
// Show inactive users
|
||||
let inactive_users = db
|
||||
.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.get::<is_active, _>(&false)
|
||||
.expect("can load stored users");
|
||||
assert_eq!(inactive_users.len(), 2);
|
||||
|
||||
println!("Created user: {:?}", user);
|
||||
println!("User ID: {}", user.get_id());
|
||||
println!(" b. Inactive Users:");
|
||||
assert_eq!(inactive_users.len(), 2);
|
||||
for (_i, inactive_user) in inactive_users.iter().enumerate() {
|
||||
print_user_details(inactive_user);
|
||||
}
|
||||
|
||||
println!("\n--- User Model Information ---");
|
||||
println!("User DB Prefix: {}", User::db_prefix());
|
||||
|
||||
// Create a comment for the user
|
||||
let comment = Comment::new(5)
|
||||
.user_id(1) // commenter's user ID
|
||||
// Demonstrate comment creation and association with a user
|
||||
println!("\n--- Working with Comments ---");
|
||||
|
||||
// 1. Create and save a comment
|
||||
println!("\n1. Creating a Comment:");
|
||||
let comment = Comment::new()
|
||||
.user_id(db_user1.get_id()) // commenter's user ID
|
||||
.content("This is a comment on the user")
|
||||
.build();
|
||||
|
||||
db.collection()
|
||||
.expect("can open commen collection")
|
||||
// Save the comment and get its assigned ID and updated model
|
||||
let (comment_id, db_comment) = db.collection()
|
||||
.expect("can open comment collection")
|
||||
.set(&comment)
|
||||
.expect("can set comment");
|
||||
|
||||
let stored_comment = db
|
||||
.collection::<Comment>()
|
||||
.expect("can open comment collection")
|
||||
.get_by_id(5)
|
||||
.expect("can load stored comment");
|
||||
println!("Comment assigned ID: {}", comment_id);
|
||||
|
||||
assert!(stored_comment.is_some());
|
||||
let stored_comment = stored_comment.unwrap();
|
||||
println!(" a. Comment Retrieved from Database:");
|
||||
print_comment_details(&db_comment);
|
||||
|
||||
assert_eq!(comment.get_id(), stored_comment.get_id());
|
||||
assert_eq!(comment.content, stored_comment.content);
|
||||
// 3. Associate the comment with a user
|
||||
println!("\n2. Associating Comment with User:");
|
||||
let mut updated_user = db_user1.clone();
|
||||
updated_user.base_data.add_comment(db_comment.get_id());
|
||||
|
||||
println!("\nCreated comment: {:?}", comment);
|
||||
println!("Comment ID: {}", comment.get_id());
|
||||
// Save the updated user and get the new version
|
||||
let (_, user_with_comment) = db.collection::<User>()
|
||||
.expect("can open user collection")
|
||||
.set(&updated_user)
|
||||
.expect("can set updated user");
|
||||
|
||||
println!(" a. User with Associated Comment:");
|
||||
print_user_details(&user_with_comment);
|
||||
|
||||
println!("\n--- Model Information ---");
|
||||
println!("User DB Prefix: {}", User::db_prefix());
|
||||
println!("Comment DB Prefix: {}", Comment::db_prefix());
|
||||
}
|
||||
|
@@ -40,7 +40,7 @@ fn main() {
|
||||
.description("Brainstorming session for new project features.")
|
||||
.add_attendee(attendee1.clone())
|
||||
.add_attendee(attendee3.clone());
|
||||
|
||||
|
||||
let event3_for_calendar2 = Event::new(
|
||||
"event_gamma".to_string(),
|
||||
"Client Call",
|
||||
@@ -50,20 +50,23 @@ fn main() {
|
||||
|
||||
// --- Create Calendars ---
|
||||
// Note: Calendar::new directly returns Calendar, no separate .build() step like the user example.
|
||||
let calendar1 = Calendar::new(1)
|
||||
|
||||
// Create a calendar with auto-generated ID
|
||||
let calendar1 = Calendar::new(None, "Work Calendar")
|
||||
.description("Calendar for all work-related events.")
|
||||
.add_event(event1.clone())
|
||||
.add_event(event2.clone());
|
||||
|
||||
let calendar2 = Calendar::new(2)
|
||||
// Create a calendar with auto-generated ID (explicit IDs are no longer supported)
|
||||
let calendar2 = Calendar::new(None, "Personal Calendar")
|
||||
.add_event(event3_for_calendar2.clone());
|
||||
|
||||
|
||||
// --- Store Calendars in DB ---
|
||||
let cal_collection = db.collection::<Calendar>().expect("can open calendar collection");
|
||||
|
||||
cal_collection.set(&calendar1).expect("can set calendar1");
|
||||
cal_collection.set(&calendar2).expect("can set calendar2");
|
||||
let (_, calendar1) = cal_collection.set(&calendar1).expect("can set calendar1");
|
||||
let (_, calendar2) = cal_collection.set(&calendar2).expect("can set calendar2");
|
||||
|
||||
println!("Created calendar1 (ID: {}): Name - '{}'", calendar1.get_id(), calendar1.name);
|
||||
println!("Created calendar2 (ID: {}): Name - '{}'", calendar2.get_id(), calendar2.name);
|
||||
@@ -72,7 +75,7 @@ fn main() {
|
||||
let stored_calendar1_opt = cal_collection.get_by_id(calendar1.get_id()).expect("can try to load calendar1");
|
||||
assert!(stored_calendar1_opt.is_some(), "Calendar1 should be found in DB");
|
||||
let mut stored_calendar1 = stored_calendar1_opt.unwrap();
|
||||
|
||||
|
||||
println!("\nRetrieved calendar1 from DB: Name - '{}', Events count: {}", stored_calendar1.name, stored_calendar1.events.len());
|
||||
assert_eq!(stored_calendar1.name, "Work Calendar");
|
||||
assert_eq!(stored_calendar1.events.len(), 2);
|
||||
@@ -87,7 +90,7 @@ fn main() {
|
||||
println!("Rescheduling event '{}'...", event_to_update.title);
|
||||
event_to_update.reschedule(new_start_time, new_end_time)
|
||||
});
|
||||
|
||||
|
||||
let rescheduled_event = stored_calendar1.events.iter().find(|e| e.id == event_id_to_reschedule)
|
||||
.expect("Rescheduled event should exist");
|
||||
assert_eq!(rescheduled_event.start_time, new_start_time);
|
||||
@@ -95,7 +98,7 @@ fn main() {
|
||||
println!("Event '{}' rescheduled in stored_calendar1.", rescheduled_event.title);
|
||||
|
||||
// --- Store the modified calendar ---
|
||||
cal_collection.set(&stored_calendar1).expect("can set modified calendar1");
|
||||
let (_, mut stored_calendar1) = cal_collection.set(&stored_calendar1).expect("can set modified calendar1");
|
||||
let re_retrieved_calendar1_opt = cal_collection.get_by_id(calendar1.get_id()).expect("can try to load modified calendar1");
|
||||
let re_retrieved_calendar1 = re_retrieved_calendar1_opt.unwrap();
|
||||
let re_retrieved_event = re_retrieved_calendar1.events.iter().find(|e| e.id == event_id_to_reschedule)
|
||||
@@ -113,7 +116,7 @@ fn main() {
|
||||
);
|
||||
stored_calendar1 = stored_calendar1.add_event(event4_new);
|
||||
assert_eq!(stored_calendar1.events.len(), 3);
|
||||
cal_collection.set(&stored_calendar1).expect("can set calendar1 after adding new event");
|
||||
let (_, stored_calendar1) = cal_collection.set(&stored_calendar1).expect("can set calendar1 after adding new event");
|
||||
println!("Added new event '1-on-1' to stored_calendar1. Total events: {}", stored_calendar1.events.len());
|
||||
|
||||
// --- Delete a Calendar ---
|
||||
@@ -123,7 +126,7 @@ fn main() {
|
||||
|
||||
println!("\nDeleted calendar2 (ID: {}) from DB.", calendar2.get_id());
|
||||
println!("Calendar model DB Prefix: {}", Calendar::db_prefix());
|
||||
|
||||
|
||||
println!("\nExample finished. DB stored at {}", db_path);
|
||||
println!("To clean up, you can manually delete the directory: {}", db_path);
|
||||
}
|
||||
|
@@ -11,13 +11,69 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Initialize database with OurDB
|
||||
let db = Arc::new(OurDB::new("temp_calendar_db", true).expect("Failed to create database"));
|
||||
|
||||
// Register functions using the new centralized method
|
||||
register_rhai_engine_functions(&mut engine, db.clone());
|
||||
// Register the Calendar type with Rhai
|
||||
// This function is generated by the #[rhai_model_export] attribute
|
||||
Calendar::register_rhai_bindings_for_calendar(&mut engine, db.clone());
|
||||
|
||||
// Register a function to get the database instance
|
||||
engine.register_fn("get_db", move || db.clone());
|
||||
|
||||
// Register a calendar builder function
|
||||
engine.register_fn("calendar__builder", |id: i64| {
|
||||
let id_option = if id <= 0 { None } else { Some(id as u32) };
|
||||
Calendar::new(id_option, "New Calendar")
|
||||
});
|
||||
|
||||
// Register setter methods for Calendar properties
|
||||
engine.register_fn("set_description", |calendar: &mut Calendar, desc: String| {
|
||||
calendar.description = Some(desc);
|
||||
});
|
||||
|
||||
// Register getter methods for Calendar properties
|
||||
engine.register_fn("get_description", |calendar: Calendar| -> String {
|
||||
calendar.description.clone().unwrap_or_default()
|
||||
});
|
||||
|
||||
// Register getter for base_data.id
|
||||
engine.register_fn("get_id", |calendar: Calendar| -> i64 {
|
||||
calendar.base_data.id as i64
|
||||
});
|
||||
|
||||
// Register additional functions needed by the script
|
||||
engine.register_fn("set_calendar", |_db: Arc<OurDB>, _calendar: Calendar| {
|
||||
// In a real implementation, this would save the calendar to the database
|
||||
println!("Calendar saved: {}", _calendar.name);
|
||||
});
|
||||
|
||||
engine.register_fn("get_calendar_by_id", |_db: Arc<OurDB>, id: i64| -> Calendar {
|
||||
// In a real implementation, this would retrieve the calendar from the database
|
||||
Calendar::new(Some(id as u32), "Retrieved Calendar")
|
||||
});
|
||||
|
||||
// Register a function to check if a calendar exists
|
||||
engine.register_fn("calendar_exists", |_db: Arc<OurDB>, id: i64| -> bool {
|
||||
// In a real implementation, this would check if the calendar exists in the database
|
||||
id == 1 || id == 2
|
||||
});
|
||||
|
||||
// Define the function separately to use with the wrap_vec_return macro
|
||||
fn get_all_calendars(_db: Arc<OurDB>) -> Vec<Calendar> {
|
||||
// In a real implementation, this would retrieve all calendars from the database
|
||||
vec![Calendar::new(Some(1), "Calendar 1"), Calendar::new(Some(2), "Calendar 2")]
|
||||
}
|
||||
|
||||
// Register the function with the wrap_vec_return macro
|
||||
engine.register_fn("get_all_calendars", wrap_vec_return!(get_all_calendars, Arc<OurDB> => Calendar));
|
||||
|
||||
engine.register_fn("delete_calendar_by_id", |_db: Arc<OurDB>, _id: i64| {
|
||||
// In a real implementation, this would delete the calendar from the database
|
||||
println!("Calendar deleted with ID: {}", _id);
|
||||
});
|
||||
|
||||
// Load and evaluate the Rhai script
|
||||
let script_path = Path::new("examples/calendar_rhai/calendar.rhai");
|
||||
let script = fs::read_to_string(script_path)?;
|
||||
|
||||
|
||||
match engine.eval::<()>(&script) {
|
||||
Ok(_) => println!("Script executed successfully!"),
|
||||
Err(e) => eprintln!("Script execution failed: {}", e),
|
||||
|
@@ -1,3 +1,4 @@
|
||||
use heromodels::db::{Collection, Db};
|
||||
use heromodels_core::{BaseModelData, Model};
|
||||
use heromodels_derive::model;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -22,16 +23,35 @@ fn main() {
|
||||
println!("Hero Models - Custom Model Example");
|
||||
println!("==================================");
|
||||
|
||||
// Create a new DB instance, reset before every run
|
||||
let db_path = "/tmp/ourdb_custom_model_example";
|
||||
let db = heromodels::db::hero::OurDB::new(db_path, true).expect("Can create DB");
|
||||
|
||||
// Example usage of the generated implementation
|
||||
println!("CustomUser DB Prefix: {}", CustomUser::db_prefix());
|
||||
|
||||
let user = CustomUser {
|
||||
base_data: BaseModelData::new(1),
|
||||
base_data: BaseModelData::new(), // ID will be auto-generated by OurDB
|
||||
login: "johndoe".to_string(),
|
||||
is_active: true,
|
||||
full_name: "John Doe".to_string(),
|
||||
};
|
||||
|
||||
println!("\nCustomUser ID: {}", user.get_id());
|
||||
println!("CustomUser DB Keys: {:?}", user.db_keys());
|
||||
println!("\nBefore saving - CustomUser ID: {}", user.get_id());
|
||||
println!("Before saving - CustomUser DB Keys: {:?}", user.db_keys());
|
||||
|
||||
// Save the model to the database
|
||||
let collection = db.collection::<CustomUser>().expect("can open user collection");
|
||||
let (user_id, saved_user) = collection.set(&user).expect("can save user");
|
||||
|
||||
println!("\nAfter saving - CustomUser ID: {}", saved_user.get_id());
|
||||
println!("After saving - CustomUser DB Keys: {:?}", saved_user.db_keys());
|
||||
println!("Returned ID: {}", user_id);
|
||||
|
||||
// Verify that the ID was auto-generated
|
||||
assert_eq!(saved_user.get_id(), user_id);
|
||||
assert_ne!(saved_user.get_id(), 0);
|
||||
|
||||
println!("\nExample finished. DB stored at {}", db_path);
|
||||
println!("To clean up, you can manually delete the directory: {}", db_path);
|
||||
}
|
||||
|
@@ -10,9 +10,9 @@ fn main() {
|
||||
// --- PART 1: ACCOUNTS AND ASSETS ---
|
||||
println!("=== ACCOUNTS AND ASSETS ===\n");
|
||||
|
||||
// Create a new account
|
||||
// Create a new account with auto-generated ID
|
||||
let mut account = Account::new(
|
||||
1, // id
|
||||
None, // id (auto-generated)
|
||||
"Main ETH Wallet", // name
|
||||
1001, // user_id
|
||||
"My primary Ethereum wallet", // description
|
||||
@@ -28,8 +28,9 @@ fn main() {
|
||||
println!("");
|
||||
|
||||
// Create some assets
|
||||
// Asset with auto-generated ID
|
||||
let eth_asset = Asset::new(
|
||||
101, // id
|
||||
None, // id (auto-generated)
|
||||
"Ethereum", // name
|
||||
"Native ETH cryptocurrency", // description
|
||||
1.5, // amount
|
||||
@@ -38,8 +39,9 @@ fn main() {
|
||||
18, // decimals
|
||||
);
|
||||
|
||||
// Assets with explicit IDs
|
||||
let usdc_asset = Asset::new(
|
||||
102, // id
|
||||
Some(102), // id
|
||||
"USDC", // name
|
||||
"USD Stablecoin on Ethereum", // description
|
||||
1000.0, // amount
|
||||
@@ -49,7 +51,7 @@ fn main() {
|
||||
);
|
||||
|
||||
let nft_asset = Asset::new(
|
||||
103, // id
|
||||
Some(103), // id
|
||||
"CryptoPunk #1234", // name
|
||||
"Rare digital collectible", // description
|
||||
1.0, // amount
|
||||
@@ -95,9 +97,9 @@ fn main() {
|
||||
// --- PART 2: MARKETPLACE LISTINGS ---
|
||||
println!("\n=== MARKETPLACE LISTINGS ===\n");
|
||||
|
||||
// Create a fixed price listing
|
||||
// Create a fixed price listing with auto-generated ID
|
||||
let mut fixed_price_listing = Listing::new(
|
||||
201, // id
|
||||
None, // id (auto-generated)
|
||||
"1000 USDC for Sale", // title
|
||||
"Selling 1000 USDC tokens at fixed price", // description
|
||||
"102", // asset_id (referencing the USDC asset)
|
||||
@@ -131,9 +133,9 @@ fn main() {
|
||||
Err(e) => println!("Error completing sale: {}", e),
|
||||
}
|
||||
|
||||
// Create an auction listing for the NFT
|
||||
// Create an auction listing for the NFT with explicit ID
|
||||
let mut auction_listing = Listing::new(
|
||||
202, // id
|
||||
Some(202), // id (explicit)
|
||||
"CryptoPunk #1234 Auction", // title
|
||||
"Rare CryptoPunk NFT for auction", // description
|
||||
"103", // asset_id (referencing the NFT asset)
|
||||
@@ -176,7 +178,7 @@ fn main() {
|
||||
|
||||
// Add bids to the auction
|
||||
println!("Adding Bids to Auction:");
|
||||
|
||||
|
||||
// Using clone() to avoid ownership issues with match expressions
|
||||
match auction_listing.clone().add_bid(bid1) {
|
||||
Ok(updated_listing) => {
|
||||
@@ -185,7 +187,7 @@ fn main() {
|
||||
},
|
||||
Err(e) => println!("Error adding bid: {}", e),
|
||||
}
|
||||
|
||||
|
||||
match auction_listing.clone().add_bid(bid2) {
|
||||
Ok(updated_listing) => {
|
||||
auction_listing = updated_listing;
|
||||
@@ -193,7 +195,7 @@ fn main() {
|
||||
},
|
||||
Err(e) => println!("Error adding bid: {}", e),
|
||||
}
|
||||
|
||||
|
||||
match auction_listing.clone().add_bid(bid3) {
|
||||
Ok(updated_listing) => {
|
||||
auction_listing = updated_listing;
|
||||
@@ -204,14 +206,14 @@ fn main() {
|
||||
|
||||
println!("\nCurrent Auction Status:");
|
||||
println!("Current Price: {} {}", auction_listing.price, auction_listing.currency);
|
||||
|
||||
|
||||
if let Some(highest_bid) = auction_listing.highest_bid() {
|
||||
println!("Highest Bid: {} {} from User {}",
|
||||
highest_bid.amount,
|
||||
highest_bid.currency,
|
||||
println!("Highest Bid: {} {} from User {}",
|
||||
highest_bid.amount,
|
||||
highest_bid.currency,
|
||||
highest_bid.bidder_id);
|
||||
}
|
||||
|
||||
|
||||
println!("Total Bids: {}", auction_listing.bids.len());
|
||||
println!("");
|
||||
|
||||
@@ -224,13 +226,13 @@ fn main() {
|
||||
println!("Winner: User {}", auction_listing.buyer_id.as_ref().unwrap());
|
||||
println!("Winning Bid: {} {}", auction_listing.sale_price.as_ref().unwrap(), auction_listing.currency);
|
||||
println!("");
|
||||
|
||||
|
||||
println!("Final Bid Statuses:");
|
||||
for bid in &auction_listing.bids {
|
||||
println!("- User {}: {} {} (Status: {:?})",
|
||||
bid.bidder_id,
|
||||
bid.amount,
|
||||
bid.currency,
|
||||
println!("- User {}: {} {} (Status: {:?})",
|
||||
bid.bidder_id,
|
||||
bid.amount,
|
||||
bid.currency,
|
||||
bid.status);
|
||||
}
|
||||
println!("");
|
||||
@@ -238,9 +240,9 @@ fn main() {
|
||||
Err(e) => println!("Error completing auction: {}", e),
|
||||
}
|
||||
|
||||
// Create an exchange listing
|
||||
// Create an exchange listing with auto-generated ID
|
||||
let exchange_listing = Listing::new(
|
||||
203, // id
|
||||
None, // id (auto-generated)
|
||||
"ETH for BTC Exchange", // title
|
||||
"Looking to exchange ETH for BTC", // description
|
||||
"101", // asset_id (referencing the ETH asset)
|
||||
@@ -262,9 +264,9 @@ fn main() {
|
||||
// --- PART 3: DEMONSTRATING EDGE CASES ---
|
||||
println!("\n=== EDGE CASES AND VALIDATIONS ===\n");
|
||||
|
||||
// Create a new auction listing for edge case testing
|
||||
// Create a new auction listing for edge case testing with explicit ID
|
||||
let test_auction = Listing::new(
|
||||
205, // id
|
||||
Some(205), // id (explicit)
|
||||
"Test Auction", // title
|
||||
"For testing edge cases", // description
|
||||
"101", // asset_id
|
||||
@@ -277,7 +279,7 @@ fn main() {
|
||||
vec![], // tags
|
||||
None::<String>, // image_url
|
||||
);
|
||||
|
||||
|
||||
// Try to add a bid that's too low
|
||||
let low_bid = Bid::new(
|
||||
test_auction.base_data.id.to_string(), // listing_id
|
||||
@@ -285,7 +287,7 @@ fn main() {
|
||||
5.0, // amount (lower than starting price)
|
||||
"ETH", // currency
|
||||
);
|
||||
|
||||
|
||||
println!("Attempting to add a bid that's too low (5.0 ETH):");
|
||||
match test_auction.add_bid(low_bid) {
|
||||
Ok(_) => println!("Bid accepted (This shouldn't happen)"),
|
||||
@@ -301,9 +303,9 @@ fn main() {
|
||||
}
|
||||
println!("");
|
||||
|
||||
// Create a listing that will expire
|
||||
// Create a listing that will expire with auto-generated ID
|
||||
let mut expiring_listing = Listing::new(
|
||||
204, // id
|
||||
None, // id (auto-generated)
|
||||
"About to Expire", // title
|
||||
"This listing will expire immediately", // description
|
||||
"101", // asset_id
|
||||
@@ -316,10 +318,10 @@ fn main() {
|
||||
vec![], // tags
|
||||
None::<String>, // image_url
|
||||
);
|
||||
|
||||
|
||||
println!("Created Expiring Listing: '{}' (ID: {})", expiring_listing.title, expiring_listing.base_data.id);
|
||||
println!("Initial Status: {:?}", expiring_listing.status);
|
||||
|
||||
|
||||
// Check expiration
|
||||
expiring_listing = expiring_listing.check_expiration();
|
||||
println!("After Checking Expiration: {:?}", expiring_listing.status);
|
||||
|
@@ -1,63 +1,144 @@
|
||||
// heromodels/examples/governance_proposal_example/main.rs
|
||||
|
||||
use chrono::{Utc, Duration};
|
||||
use heromodels::models::governance::{Proposal, ProposalStatus, VoteEventStatus};
|
||||
use chrono::{Duration, Utc};
|
||||
use heromodels::db::{Collection, Db};
|
||||
use heromodels::models::governance::{Ballot, Proposal, ProposalStatus, VoteEventStatus};
|
||||
|
||||
fn main() {
|
||||
println!("Governance Proposal Model Example\n");
|
||||
|
||||
// Create a new proposal
|
||||
// Create a new DB instance, reset before every run
|
||||
let db_path = "/tmp/ourdb_governance_proposal_example";
|
||||
let db = heromodels::db::hero::OurDB::new(db_path, true).expect("Can create DB");
|
||||
|
||||
// Create a new proposal with auto-generated ID
|
||||
let mut proposal = Proposal::new(
|
||||
1, // id
|
||||
"user_creator_123", // creator_id
|
||||
None, // id (auto-generated)
|
||||
"user_creator_123",
|
||||
"Ahmed fared", // creator_id
|
||||
"Community Fund Allocation for Q3", // title
|
||||
"Proposal to allocate funds for community projects in the third quarter.", // description
|
||||
Utc::now(), // vote_start_date
|
||||
Utc::now() + Duration::days(14) // vote_end_date (14 days from now)
|
||||
ProposalStatus::Draft,
|
||||
Utc::now(), // created_at
|
||||
Utc::now(), // updated_at
|
||||
Utc::now(), // vote_start_date
|
||||
Utc::now() + Duration::days(14), // vote_end_date (14 days from now)
|
||||
);
|
||||
|
||||
println!("Created Proposal: '{}' (ID: {})", proposal.title, proposal.base_data.id);
|
||||
println!("Status: {:?}, Vote Status: {:?}", proposal.status, proposal.vote_status);
|
||||
println!("Vote Period: {} to {}\n", proposal.vote_start_date, proposal.vote_end_date);
|
||||
println!(
|
||||
"Before saving - Created Proposal: '{}' (ID: {})",
|
||||
proposal.title, proposal.base_data.id
|
||||
);
|
||||
println!(
|
||||
"Before saving - Status: {:?}, Vote Status: {:?}",
|
||||
proposal.status, proposal.vote_status
|
||||
);
|
||||
println!(
|
||||
"Before saving - Vote Period: {} to {}\n",
|
||||
proposal.vote_start_date, proposal.vote_end_date
|
||||
);
|
||||
|
||||
// Add vote options
|
||||
proposal = proposal.add_option(1, "Approve Allocation");
|
||||
proposal = proposal.add_option(2, "Reject Allocation");
|
||||
proposal = proposal.add_option(3, "Abstain");
|
||||
proposal = proposal.add_option(1, "Approve Allocation", Some("This is the approval option"));
|
||||
proposal = proposal.add_option(2, "Reject Allocation", Some("This is the rejection option"));
|
||||
proposal = proposal.add_option(3, "Abstain", Some("This is the abstain option"));
|
||||
|
||||
println!("Added Vote Options:");
|
||||
for option in &proposal.options {
|
||||
println!("- Option ID: {}, Text: '{}', Votes: {}", option.id, option.text, option.count);
|
||||
println!(
|
||||
"- Option ID: {}, Text: '{}', Votes: {}",
|
||||
option.id, option.text, option.count
|
||||
);
|
||||
}
|
||||
println!("");
|
||||
|
||||
// Save the proposal to the database
|
||||
let collection = db
|
||||
.collection::<Proposal>()
|
||||
.expect("can open proposal collection");
|
||||
let (proposal_id, saved_proposal) = collection.set(&proposal).expect("can save proposal");
|
||||
|
||||
println!(
|
||||
"After saving - Proposal ID: {}",
|
||||
saved_proposal.base_data.id
|
||||
);
|
||||
println!("After saving - Returned ID: {}", proposal_id);
|
||||
|
||||
// Use the saved proposal for further operations
|
||||
proposal = saved_proposal;
|
||||
|
||||
// Simulate casting votes
|
||||
println!("Simulating Votes...");
|
||||
// User 1 votes for 'Approve Allocation' with 100 shares
|
||||
proposal = proposal.cast_vote(101, 1, 1, 100);
|
||||
// User 2 votes for 'Reject Allocation' with 50 shares
|
||||
proposal = proposal.cast_vote(102, 2, 2, 50);
|
||||
// User 3 votes for 'Approve Allocation' with 75 shares
|
||||
proposal = proposal.cast_vote(103, 3, 1, 75);
|
||||
// User 4 abstains with 20 shares
|
||||
proposal = proposal.cast_vote(104, 4, 3, 20);
|
||||
// User 1 votes for 'Approve Allocation' with 100 shares (with explicit ballot ID)
|
||||
proposal = proposal.cast_vote(Some(101), 1, 1, 100);
|
||||
// User 2 votes for 'Reject Allocation' with 50 shares (with explicit ballot ID)
|
||||
proposal = proposal.cast_vote(Some(102), 2, 2, 50);
|
||||
// User 3 votes for 'Approve Allocation' with 75 shares (with auto-generated ballot ID)
|
||||
proposal = proposal.cast_vote(None, 3, 1, 75);
|
||||
// User 4 abstains with 20 shares (with auto-generated ballot ID)
|
||||
proposal = proposal.cast_vote(None, 4, 3, 20);
|
||||
// User 5 attempts to vote for a non-existent option (should be handled gracefully)
|
||||
proposal = proposal.cast_vote(105, 5, 99, 10);
|
||||
proposal = proposal.cast_vote(Some(105), 5, 99, 10);
|
||||
// User 1 tries to vote again (not explicitly prevented by current model, but could be a future enhancement)
|
||||
// proposal = proposal.cast_vote(106, 1, 1, 10);
|
||||
// proposal = proposal.cast_vote(Some(106), 1, 1, 10);
|
||||
|
||||
println!("\nVote Counts After Simulation:");
|
||||
for option in &proposal.options {
|
||||
println!("- Option ID: {}, Text: '{}', Votes: {}", option.id, option.text, option.count);
|
||||
println!(
|
||||
"- Option ID: {}, Text: '{}', Votes: {}",
|
||||
option.id, option.text, option.count
|
||||
);
|
||||
}
|
||||
|
||||
println!("\nBallots Cast:");
|
||||
for ballot in &proposal.ballots {
|
||||
println!("- Ballot ID: {}, User ID: {}, Option ID: {}, Shares: {}",
|
||||
ballot.base_data.id, ballot.user_id, ballot.vote_option_id, ballot.shares_count);
|
||||
println!(
|
||||
"- Ballot ID: {}, User ID: {}, Option ID: {}, Shares: {}",
|
||||
ballot.base_data.id, ballot.user_id, ballot.vote_option_id, ballot.shares_count
|
||||
);
|
||||
}
|
||||
println!("");
|
||||
|
||||
// Example of voting with comments using the cast_vote_with_comment method
|
||||
println!("Adding votes with comments...");
|
||||
|
||||
// User 7 votes for 'Approve Allocation' with a comment
|
||||
proposal = proposal.cast_vote_with_comment(
|
||||
Some(110), // ballot_id
|
||||
7, // user_id
|
||||
1, // chosen_option_id (Approve Allocation)
|
||||
80, // shares
|
||||
"I strongly support this proposal because it aligns with our community values."
|
||||
);
|
||||
|
||||
// User 8 votes for 'Reject Allocation' with a comment
|
||||
proposal = proposal.cast_vote_with_comment(
|
||||
Some(111), // ballot_id
|
||||
8, // user_id
|
||||
2, // chosen_option_id (Reject Allocation)
|
||||
60, // shares
|
||||
"I have concerns about the allocation priorities."
|
||||
);
|
||||
|
||||
println!("\nBallots with Comments:");
|
||||
for ballot in &proposal.ballots {
|
||||
if let Some(comment) = &ballot.comment {
|
||||
println!(
|
||||
"- Ballot ID: {}, User ID: {}, Option ID: {}, Shares: {}",
|
||||
ballot.base_data.id, ballot.user_id, ballot.vote_option_id, ballot.shares_count
|
||||
);
|
||||
println!(" Comment: \"{}\"", comment);
|
||||
}
|
||||
}
|
||||
|
||||
println!("\nUpdated Vote Counts After Comments:");
|
||||
for option in &proposal.options {
|
||||
println!(
|
||||
"- Option ID: {}, Text: '{}', Votes: {}",
|
||||
option.id, option.text, option.count
|
||||
);
|
||||
}
|
||||
|
||||
// Change proposal status
|
||||
proposal = proposal.change_proposal_status(ProposalStatus::Active);
|
||||
println!("Changed Proposal Status to: {:?}", proposal.status);
|
||||
@@ -68,7 +149,7 @@ fn main() {
|
||||
|
||||
// Attempt to cast a vote after closing (should be handled)
|
||||
println!("\nAttempting to cast vote after voting is closed...");
|
||||
proposal = proposal.cast_vote(107, 6, 1, 25);
|
||||
proposal = proposal.cast_vote(None, 6, 1, 25);
|
||||
|
||||
// Final proposal state
|
||||
println!("\nFinal Proposal State:");
|
||||
@@ -77,34 +158,145 @@ fn main() {
|
||||
println!("Vote Status: {:?}", proposal.vote_status);
|
||||
println!("Options:");
|
||||
for option in &proposal.options {
|
||||
println!(" - {}: {} (Votes: {})", option.id, option.text, option.count);
|
||||
println!(
|
||||
" - {}: {} (Votes: {})",
|
||||
option.id, option.text, option.count
|
||||
);
|
||||
}
|
||||
println!("Total Ballots: {}", proposal.ballots.len());
|
||||
|
||||
// Example of a private proposal (not fully implemented in cast_vote eligibility yet)
|
||||
let mut private_proposal = Proposal::new(
|
||||
2,
|
||||
"user_admin_001",
|
||||
"Internal Team Restructure Vote",
|
||||
"Vote on proposed internal team changes.",
|
||||
Utc::now(),
|
||||
Utc::now() + Duration::days(7)
|
||||
None, // auto-generated ID
|
||||
"user_admin_001",
|
||||
"Wael Ghonem",
|
||||
"Internal Team Restructure Vote",
|
||||
"Vote on proposed internal team changes.",
|
||||
ProposalStatus::Draft,
|
||||
Utc::now(),
|
||||
Utc::now(),
|
||||
Utc::now(),
|
||||
Utc::now() + Duration::days(7),
|
||||
);
|
||||
private_proposal.private_group = Some(vec![10, 20, 30]); // Only users 10, 20, 30 can vote
|
||||
private_proposal = private_proposal.add_option(1, "Accept Restructure");
|
||||
private_proposal = private_proposal.add_option(2, "Reject Restructure");
|
||||
private_proposal = private_proposal.add_option(
|
||||
1,
|
||||
"Accept Restructure",
|
||||
Some("This is the accept restructure option".to_string()),
|
||||
);
|
||||
private_proposal = private_proposal.add_option(
|
||||
2,
|
||||
"Reject Restructure",
|
||||
Some("This is the reject restructure option".to_string()),
|
||||
);
|
||||
|
||||
println!(
|
||||
"\nBefore saving - Created Private Proposal: '{}'",
|
||||
private_proposal.title
|
||||
);
|
||||
println!(
|
||||
"Before saving - Eligible Voters (Group): {:?}",
|
||||
private_proposal.private_group
|
||||
);
|
||||
|
||||
// Save the private proposal to the database
|
||||
let (private_proposal_id, saved_private_proposal) = collection
|
||||
.set(&private_proposal)
|
||||
.expect("can save private proposal");
|
||||
private_proposal = saved_private_proposal;
|
||||
|
||||
println!(
|
||||
"After saving - Private Proposal ID: {}",
|
||||
private_proposal.base_data.id
|
||||
);
|
||||
println!("After saving - Returned ID: {}", private_proposal_id);
|
||||
|
||||
// User 10 (eligible) votes with explicit ballot ID
|
||||
private_proposal = private_proposal.cast_vote(Some(201), 10, 1, 100);
|
||||
// User 40 (ineligible) tries to vote with auto-generated ballot ID
|
||||
private_proposal = private_proposal.cast_vote(None, 40, 1, 50);
|
||||
|
||||
// Example of voting with comments on a private proposal
|
||||
println!("\nAdding votes with comments to private proposal...");
|
||||
|
||||
println!("\nCreated Private Proposal: '{}'", private_proposal.title);
|
||||
println!("Eligible Voters (Group): {:?}", private_proposal.private_group);
|
||||
// User 10 (eligible) votes
|
||||
private_proposal = private_proposal.cast_vote(201, 10, 1, 100);
|
||||
// User 40 (ineligible) tries to vote
|
||||
private_proposal = private_proposal.cast_vote(202, 40, 1, 50);
|
||||
// User 20 (eligible) votes with a comment
|
||||
private_proposal = private_proposal.cast_vote_with_comment(
|
||||
Some(202), // ballot_id
|
||||
20, // user_id (eligible)
|
||||
1, // chosen_option_id
|
||||
75, // shares
|
||||
"I support this restructuring plan with some reservations."
|
||||
);
|
||||
|
||||
println!("Private Proposal Vote Counts:");
|
||||
for option in &private_proposal.options {
|
||||
println!(" - {}: {} (Votes: {})", option.id, option.text, option.count);
|
||||
// User 30 (eligible) votes with a comment
|
||||
private_proposal = private_proposal.cast_vote_with_comment(
|
||||
Some(203), // ballot_id
|
||||
30, // user_id (eligible)
|
||||
2, // chosen_option_id
|
||||
90, // shares
|
||||
"I believe we should reconsider the timing of these changes."
|
||||
);
|
||||
|
||||
// User 40 (ineligible) tries to vote with a comment
|
||||
private_proposal = private_proposal.cast_vote_with_comment(
|
||||
Some(204), // ballot_id
|
||||
40, // user_id (ineligible)
|
||||
1, // chosen_option_id
|
||||
50, // shares
|
||||
"This restructuring seems unnecessary."
|
||||
);
|
||||
|
||||
println!("Eligible users 20 and 30 added votes with comments.");
|
||||
println!("Ineligible user 40 attempted to vote with a comment (should be rejected).");
|
||||
|
||||
println!("\nPrivate Proposal Ballots with Comments:");
|
||||
for ballot in &private_proposal.ballots {
|
||||
if let Some(comment) = &ballot.comment {
|
||||
println!(
|
||||
"- Ballot ID: {}, User ID: {}, Option ID: {}, Shares: {}",
|
||||
ballot.base_data.id, ballot.user_id, ballot.vote_option_id, ballot.shares_count
|
||||
);
|
||||
println!(" Comment: \"{}\"", comment);
|
||||
}
|
||||
}
|
||||
|
||||
println!("\nGovernance Proposal Example Finished.");
|
||||
println!("Private Proposal Vote Counts:");
|
||||
for option in &private_proposal.options {
|
||||
println!(
|
||||
" - {}: {} (Votes: {})",
|
||||
option.id, option.text, option.count
|
||||
);
|
||||
}
|
||||
|
||||
println!("\nExample finished. DB stored at {}", db_path);
|
||||
println!(
|
||||
"To clean up, you can manually delete the directory: {}",
|
||||
db_path
|
||||
);
|
||||
|
||||
// --- Additional Example: Listing and Filtering Proposals ---
|
||||
println!("\n--- Listing All Proposals ---");
|
||||
// List all proposals from the DB
|
||||
let all_proposals = collection.get_all().expect("can list all proposals");
|
||||
for proposal in &all_proposals {
|
||||
println!(
|
||||
"- Proposal ID: {}, Title: '{}', Status: {:?}",
|
||||
proposal.base_data.id, proposal.title, proposal.status
|
||||
);
|
||||
}
|
||||
println!("Total proposals in DB: {}", all_proposals.len());
|
||||
|
||||
// Filter proposals by status (e.g., only Active proposals)
|
||||
let active_proposals: Vec<_> = all_proposals
|
||||
.iter()
|
||||
.filter(|p| p.status == ProposalStatus::Active)
|
||||
.collect();
|
||||
println!("\n--- Filtering Proposals by Status: Active ---");
|
||||
for proposal in &active_proposals {
|
||||
println!(
|
||||
"- Proposal ID: {}, Title: '{}', Status: {:?}",
|
||||
proposal.base_data.id, proposal.title, proposal.status
|
||||
);
|
||||
}
|
||||
println!("Total ACTIVE proposals: {}", active_proposals.len());
|
||||
}
|
||||
|
@@ -1,10 +1,12 @@
|
||||
use chrono::{Duration, Utc};
|
||||
use heromodels::db::hero::OurDB;
|
||||
use heromodels::models::governance::{Proposal, ProposalStatus, VoteEventStatus, VoteOption, Ballot};
|
||||
use heromodels::models::governance::{
|
||||
Ballot, Proposal, ProposalStatus, VoteEventStatus, VoteOption,
|
||||
};
|
||||
use rhai::Engine;
|
||||
use rhai_wrapper::wrap_vec_return;
|
||||
use std::sync::Arc;
|
||||
use std::{fs, path::Path};
|
||||
use chrono::{Utc, Duration};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Initialize Rhai engine
|
||||
@@ -16,162 +18,261 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Register the Proposal type with Rhai
|
||||
// This function is generated by the #[rhai_model_export] attribute
|
||||
Proposal::register_rhai_bindings_for_proposal(&mut engine, db.clone());
|
||||
|
||||
|
||||
// Register the Ballot type with Rhai
|
||||
Ballot::register_rhai_bindings_for_ballot(&mut engine, db.clone());
|
||||
|
||||
// Register a function to get the database instance
|
||||
engine.register_fn("get_db", move || db.clone());
|
||||
|
||||
|
||||
// Register builder functions for Proposal and related types
|
||||
engine.register_fn("create_proposal", |id: i64, creator_id: String, title: String, description: String| {
|
||||
let start_date = Utc::now();
|
||||
let end_date = start_date + Duration::days(14);
|
||||
Proposal::new(id as u32, creator_id, title, description, start_date, end_date)
|
||||
});
|
||||
|
||||
engine.register_fn(
|
||||
"create_proposal",
|
||||
|id: i64, creator_id: String, creator_name: String, title: String, description: String| {
|
||||
let start_date = Utc::now();
|
||||
let end_date = start_date + Duration::days(14);
|
||||
let id_option = if id <= 0 { None } else { Some(id as u32) };
|
||||
Proposal::new(
|
||||
id_option,
|
||||
creator_id,
|
||||
creator_name,
|
||||
title,
|
||||
description,
|
||||
ProposalStatus::Draft,
|
||||
Utc::now(),
|
||||
Utc::now(),
|
||||
start_date,
|
||||
end_date,
|
||||
)
|
||||
},
|
||||
);
|
||||
|
||||
engine.register_fn("create_vote_option", |id: i64, text: String| {
|
||||
VoteOption::new(id as u8, text)
|
||||
VoteOption::new(id as u8, text, Some("This is an optional comment"))
|
||||
});
|
||||
|
||||
engine.register_fn("create_ballot", |id: i64, user_id: i64, vote_option_id: i64, shares_count: i64| {
|
||||
Ballot::new(id as u32, user_id as u32, vote_option_id as u8, shares_count)
|
||||
});
|
||||
|
||||
|
||||
engine.register_fn(
|
||||
"create_ballot",
|
||||
|id: i64, user_id: i64, vote_option_id: i64, shares_count: i64| {
|
||||
let id_option = if id <= 0 { None } else { Some(id as u32) };
|
||||
Ballot::new(
|
||||
id_option,
|
||||
user_id as u32,
|
||||
vote_option_id as u8,
|
||||
shares_count,
|
||||
)
|
||||
},
|
||||
);
|
||||
|
||||
// Register getter and setter methods for Proposal properties
|
||||
engine.register_fn("get_title", |proposal: Proposal| -> String {
|
||||
proposal.title.clone()
|
||||
});
|
||||
|
||||
|
||||
engine.register_fn("get_description", |proposal: Proposal| -> String {
|
||||
proposal.description.clone()
|
||||
});
|
||||
|
||||
|
||||
engine.register_fn("get_creator_id", |proposal: Proposal| -> String {
|
||||
proposal.creator_id.clone()
|
||||
});
|
||||
|
||||
|
||||
engine.register_fn("get_id", |proposal: Proposal| -> i64 {
|
||||
proposal.base_data.id as i64
|
||||
});
|
||||
|
||||
|
||||
engine.register_fn("get_status", |proposal: Proposal| -> String {
|
||||
format!("{:?}", proposal.status)
|
||||
});
|
||||
|
||||
|
||||
engine.register_fn("get_vote_status", |proposal: Proposal| -> String {
|
||||
format!("{:?}", proposal.vote_status)
|
||||
});
|
||||
|
||||
|
||||
// Register methods for proposal operations
|
||||
engine.register_fn("add_option_to_proposal", |mut proposal: Proposal, option_id: i64, option_text: String| -> Proposal {
|
||||
proposal.add_option(option_id as u8, option_text)
|
||||
});
|
||||
|
||||
engine.register_fn("cast_vote_on_proposal", |mut proposal: Proposal, ballot_id: i64, user_id: i64, option_id: i64, shares: i64| -> Proposal {
|
||||
proposal.cast_vote(ballot_id as u32, user_id as u32, option_id as u8, shares)
|
||||
});
|
||||
|
||||
engine.register_fn("change_proposal_status", |mut proposal: Proposal, status_str: String| -> Proposal {
|
||||
let new_status = match status_str.as_str() {
|
||||
"Draft" => ProposalStatus::Draft,
|
||||
"Active" => ProposalStatus::Active,
|
||||
"Approved" => ProposalStatus::Approved,
|
||||
"Rejected" => ProposalStatus::Rejected,
|
||||
"Cancelled" => ProposalStatus::Cancelled,
|
||||
_ => ProposalStatus::Draft,
|
||||
};
|
||||
proposal.change_proposal_status(new_status)
|
||||
});
|
||||
|
||||
engine.register_fn("change_vote_event_status", |mut proposal: Proposal, status_str: String| -> Proposal {
|
||||
let new_status = match status_str.as_str() {
|
||||
"Open" => VoteEventStatus::Open,
|
||||
"Closed" => VoteEventStatus::Closed,
|
||||
"Cancelled" => VoteEventStatus::Cancelled,
|
||||
_ => VoteEventStatus::Open,
|
||||
};
|
||||
proposal.change_vote_event_status(new_status)
|
||||
});
|
||||
|
||||
engine.register_fn(
|
||||
"add_option_to_proposal",
|
||||
|mut proposal: Proposal, option_id: i64, option_text: String| -> Proposal {
|
||||
proposal.add_option(
|
||||
option_id as u8,
|
||||
option_text,
|
||||
Some("This is an optional comment".to_string()),
|
||||
)
|
||||
},
|
||||
);
|
||||
|
||||
engine.register_fn(
|
||||
"cast_vote_on_proposal",
|
||||
|mut proposal: Proposal,
|
||||
ballot_id: i64,
|
||||
user_id: i64,
|
||||
option_id: i64,
|
||||
shares: i64|
|
||||
-> Proposal {
|
||||
let ballot_id_option = if ballot_id <= 0 {
|
||||
None
|
||||
} else {
|
||||
Some(ballot_id as u32)
|
||||
};
|
||||
proposal.cast_vote(ballot_id_option, user_id as u32, option_id as u8, shares)
|
||||
},
|
||||
);
|
||||
|
||||
engine.register_fn(
|
||||
"change_proposal_status",
|
||||
|mut proposal: Proposal, status_str: String| -> Proposal {
|
||||
let new_status = match status_str.as_str() {
|
||||
"Draft" => ProposalStatus::Draft,
|
||||
"Active" => ProposalStatus::Active,
|
||||
"Approved" => ProposalStatus::Approved,
|
||||
"Rejected" => ProposalStatus::Rejected,
|
||||
"Cancelled" => ProposalStatus::Cancelled,
|
||||
_ => ProposalStatus::Draft,
|
||||
};
|
||||
proposal.change_proposal_status(new_status)
|
||||
},
|
||||
);
|
||||
|
||||
engine.register_fn(
|
||||
"change_vote_event_status",
|
||||
|mut proposal: Proposal, status_str: String| -> Proposal {
|
||||
let new_status = match status_str.as_str() {
|
||||
"Open" => VoteEventStatus::Open,
|
||||
"Closed" => VoteEventStatus::Closed,
|
||||
"Cancelled" => VoteEventStatus::Cancelled,
|
||||
_ => VoteEventStatus::Open,
|
||||
};
|
||||
proposal.change_vote_event_status(new_status)
|
||||
},
|
||||
);
|
||||
|
||||
// Register functions for database operations
|
||||
engine.register_fn("save_proposal", |_db: Arc<OurDB>, proposal: Proposal| {
|
||||
println!("Proposal saved: {}", proposal.title);
|
||||
});
|
||||
|
||||
engine.register_fn("get_proposal_by_id", |_db: Arc<OurDB>, id: i64| -> Proposal {
|
||||
// In a real implementation, this would retrieve the proposal from the database
|
||||
let start_date = Utc::now();
|
||||
let end_date = start_date + Duration::days(14);
|
||||
Proposal::new(id as u32, "Retrieved Creator", "Retrieved Proposal", "Retrieved Description", start_date, end_date)
|
||||
});
|
||||
|
||||
|
||||
engine.register_fn(
|
||||
"get_proposal_by_id",
|
||||
|_db: Arc<OurDB>, id: i64| -> Proposal {
|
||||
// In a real implementation, this would retrieve the proposal from the database
|
||||
let start_date = Utc::now();
|
||||
let end_date = start_date + Duration::days(14);
|
||||
Proposal::new(
|
||||
Some(id as u32),
|
||||
"Retrieved Creator",
|
||||
"Retrieved Creator Name",
|
||||
"Retrieved Proposal",
|
||||
"Retrieved Description",
|
||||
ProposalStatus::Draft,
|
||||
Utc::now(),
|
||||
Utc::now(),
|
||||
start_date,
|
||||
end_date,
|
||||
)
|
||||
},
|
||||
);
|
||||
|
||||
// Register a function to check if a proposal exists
|
||||
engine.register_fn("proposal_exists", |_db: Arc<OurDB>, id: i64| -> bool {
|
||||
// In a real implementation, this would check if the proposal exists in the database
|
||||
id == 1 || id == 2
|
||||
});
|
||||
|
||||
|
||||
// Define the function for get_all_proposals
|
||||
fn get_all_proposals(_db: Arc<OurDB>) -> Vec<Proposal> {
|
||||
// In a real implementation, this would retrieve all proposals from the database
|
||||
let start_date = Utc::now();
|
||||
let end_date = start_date + Duration::days(14);
|
||||
vec![
|
||||
Proposal::new(1, "Creator 1", "Proposal 1", "Description 1", start_date, end_date),
|
||||
Proposal::new(2, "Creator 2", "Proposal 2", "Description 2", start_date, end_date)
|
||||
Proposal::new(
|
||||
Some(1),
|
||||
"Creator 1",
|
||||
"Creator Name 1",
|
||||
"Proposal 1",
|
||||
"Description 1",
|
||||
ProposalStatus::Draft,
|
||||
Utc::now(),
|
||||
Utc::now(),
|
||||
start_date,
|
||||
end_date,
|
||||
),
|
||||
Proposal::new(
|
||||
Some(2),
|
||||
"Creator 2",
|
||||
"Creator Name 2",
|
||||
"Proposal 2",
|
||||
"Description 2",
|
||||
ProposalStatus::Draft,
|
||||
Utc::now(),
|
||||
Utc::now(),
|
||||
start_date,
|
||||
end_date,
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
// Register the function with the wrap_vec_return macro
|
||||
engine.register_fn("get_all_proposals", wrap_vec_return!(get_all_proposals, Arc<OurDB> => Proposal));
|
||||
|
||||
engine.register_fn(
|
||||
"get_all_proposals",
|
||||
wrap_vec_return!(get_all_proposals, Arc<OurDB> => Proposal),
|
||||
);
|
||||
|
||||
engine.register_fn("delete_proposal_by_id", |_db: Arc<OurDB>, _id: i64| {
|
||||
// In a real implementation, this would delete the proposal from the database
|
||||
println!("Proposal deleted with ID: {}", _id);
|
||||
});
|
||||
|
||||
|
||||
// Register helper functions for accessing proposal options and ballots
|
||||
engine.register_fn("get_option_count", |proposal: Proposal| -> i64 {
|
||||
proposal.options.len() as i64
|
||||
});
|
||||
|
||||
engine.register_fn("get_option_at", |proposal: Proposal, index: i64| -> VoteOption {
|
||||
if index >= 0 && index < proposal.options.len() as i64 {
|
||||
proposal.options[index as usize].clone()
|
||||
} else {
|
||||
VoteOption::new(0, "Invalid Option")
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
engine.register_fn(
|
||||
"get_option_at",
|
||||
|proposal: Proposal, index: i64| -> VoteOption {
|
||||
if index >= 0 && index < proposal.options.len() as i64 {
|
||||
proposal.options[index as usize].clone()
|
||||
} else {
|
||||
VoteOption::new(
|
||||
0,
|
||||
"Invalid Option",
|
||||
Some("This is an invalid option".to_string()),
|
||||
)
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
engine.register_fn("get_option_text", |option: VoteOption| -> String {
|
||||
option.text.clone()
|
||||
});
|
||||
|
||||
|
||||
engine.register_fn("get_option_votes", |option: VoteOption| -> i64 {
|
||||
option.count
|
||||
});
|
||||
|
||||
|
||||
engine.register_fn("get_ballot_count", |proposal: Proposal| -> i64 {
|
||||
proposal.ballots.len() as i64
|
||||
});
|
||||
|
||||
engine.register_fn("get_ballot_at", |proposal: Proposal, index: i64| -> Ballot {
|
||||
if index >= 0 && index < proposal.ballots.len() as i64 {
|
||||
proposal.ballots[index as usize].clone()
|
||||
} else {
|
||||
Ballot::new(0, 0, 0, 0)
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
engine.register_fn(
|
||||
"get_ballot_at",
|
||||
|proposal: Proposal, index: i64| -> Ballot {
|
||||
if index >= 0 && index < proposal.ballots.len() as i64 {
|
||||
proposal.ballots[index as usize].clone()
|
||||
} else {
|
||||
Ballot::new(None, 0, 0, 0)
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
engine.register_fn("get_ballot_user_id", |ballot: Ballot| -> i64 {
|
||||
ballot.user_id as i64
|
||||
});
|
||||
|
||||
|
||||
engine.register_fn("get_ballot_option_id", |ballot: Ballot| -> i64 {
|
||||
ballot.vote_option_id as i64
|
||||
});
|
||||
|
||||
|
||||
engine.register_fn("get_ballot_shares", |ballot: Ballot| -> i64 {
|
||||
ballot.shares_count
|
||||
});
|
||||
@@ -179,7 +280,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Load and evaluate the Rhai script
|
||||
let script_path = Path::new("examples/governance_rhai/governance.rhai");
|
||||
let script = fs::read_to_string(script_path)?;
|
||||
|
||||
|
||||
match engine.eval::<()>(&script) {
|
||||
Ok(_) => println!("Script executed successfully!"),
|
||||
Err(e) => eprintln!("Script execution failed: {}", e),
|
||||
|
@@ -1,8 +1,8 @@
|
||||
// Get the database instance
|
||||
let db = get_db();
|
||||
|
||||
// Create a new proposal
|
||||
let proposal = create_proposal(1, "user_creator_123", "Community Fund Allocation for Q3",
|
||||
// Create a new proposal with auto-generated ID (pass 0 for auto-generated ID)
|
||||
let proposal = create_proposal(0, "user_creator_123", "Community Fund Allocation for Q3",
|
||||
"Proposal to allocate funds for community projects in the third quarter.");
|
||||
|
||||
print("Created Proposal: '" + get_title(proposal) + "' (ID: " + get_id(proposal) + ")");
|
||||
@@ -26,14 +26,14 @@ print("\nProposal saved to database");
|
||||
|
||||
// Simulate casting votes
|
||||
print("\nSimulating Votes...");
|
||||
// User 1 votes for 'Approve Allocation' with 100 shares
|
||||
// User 1 votes for 'Approve Allocation' with 100 shares (with explicit ballot ID)
|
||||
let proposal_with_votes = cast_vote_on_proposal(proposal_with_options, 101, 1, 1, 100);
|
||||
// User 2 votes for 'Reject Allocation' with 50 shares
|
||||
// User 2 votes for 'Reject Allocation' with 50 shares (with explicit ballot ID)
|
||||
proposal_with_votes = cast_vote_on_proposal(proposal_with_votes, 102, 2, 2, 50);
|
||||
// User 3 votes for 'Approve Allocation' with 75 shares
|
||||
proposal_with_votes = cast_vote_on_proposal(proposal_with_votes, 103, 3, 1, 75);
|
||||
// User 4 abstains with 20 shares
|
||||
proposal_with_votes = cast_vote_on_proposal(proposal_with_votes, 104, 4, 3, 20);
|
||||
// User 3 votes for 'Approve Allocation' with 75 shares (with auto-generated ballot ID)
|
||||
proposal_with_votes = cast_vote_on_proposal(proposal_with_votes, 0, 3, 1, 75);
|
||||
// User 4 abstains with 20 shares (with auto-generated ballot ID)
|
||||
proposal_with_votes = cast_vote_on_proposal(proposal_with_votes, 0, 4, 3, 20);
|
||||
|
||||
print("\nVote Counts After Simulation:");
|
||||
option_count = get_option_count(proposal_with_votes);
|
||||
@@ -46,7 +46,7 @@ print("\nBallots Cast:");
|
||||
let ballot_count = get_ballot_count(proposal_with_votes);
|
||||
for i in range(0, ballot_count) {
|
||||
let ballot = get_ballot_at(proposal_with_votes, i);
|
||||
print("- Ballot ID: " + i + ", User ID: " + get_ballot_user_id(ballot) +
|
||||
print("- Ballot ID: " + i + ", User ID: " + get_ballot_user_id(ballot) +
|
||||
", Option ID: " + get_ballot_option_id(ballot) + ", Shares: " + get_ballot_shares(ballot));
|
||||
}
|
||||
|
||||
|
@@ -1,3 +1,4 @@
|
||||
use heromodels::db::{Collection, Db};
|
||||
use heromodels_core::{BaseModelData, Model};
|
||||
use heromodels_derive::model;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -33,26 +34,54 @@ fn main() {
|
||||
println!("Hero Models - Model Macro Example");
|
||||
println!("=================================");
|
||||
|
||||
// Create a new DB instance, reset before every run
|
||||
let db_path = "/tmp/ourdb_model_macro_example";
|
||||
let db = heromodels::db::hero::OurDB::new(db_path, true).expect("Can create DB");
|
||||
|
||||
// Example usage of the generated implementations
|
||||
println!("SimpleUser DB Prefix: {}", SimpleUser::db_prefix());
|
||||
println!("CustomUser DB Prefix: {}", CustomUser::db_prefix());
|
||||
|
||||
let user = SimpleUser {
|
||||
base_data: BaseModelData::new(1),
|
||||
base_data: BaseModelData::new(), // ID will be auto-generated by OurDB
|
||||
login: "johndoe".to_string(),
|
||||
full_name: "John Doe".to_string(),
|
||||
};
|
||||
|
||||
let custom_user = CustomUser {
|
||||
base_data: BaseModelData::new(2),
|
||||
base_data: BaseModelData::new(), // ID will be auto-generated by OurDB
|
||||
login_name: "janesmith".to_string(),
|
||||
is_active: true,
|
||||
full_name: "Jane Smith".to_string(),
|
||||
};
|
||||
|
||||
println!("\nSimpleUser ID: {}", user.get_id());
|
||||
println!("SimpleUser DB Keys: {:?}", user.db_keys());
|
||||
println!("\nBefore saving - SimpleUser ID: {}", user.get_id());
|
||||
println!("Before saving - SimpleUser DB Keys: {:?}", user.db_keys());
|
||||
|
||||
println!("\nCustomUser ID: {}", custom_user.get_id());
|
||||
println!("CustomUser DB Keys: {:?}", custom_user.db_keys());
|
||||
println!("\nBefore saving - CustomUser ID: {}", custom_user.get_id());
|
||||
println!("Before saving - CustomUser DB Keys: {:?}", custom_user.db_keys());
|
||||
|
||||
// Save the models to the database
|
||||
let simple_collection = db.collection::<SimpleUser>().expect("can open simple user collection");
|
||||
let custom_collection = db.collection::<CustomUser>().expect("can open custom user collection");
|
||||
|
||||
let (user_id, saved_user) = simple_collection.set(&user).expect("can save simple user");
|
||||
let (custom_user_id, saved_custom_user) = custom_collection.set(&custom_user).expect("can save custom user");
|
||||
|
||||
println!("\nAfter saving - SimpleUser ID: {}", saved_user.get_id());
|
||||
println!("After saving - SimpleUser DB Keys: {:?}", saved_user.db_keys());
|
||||
println!("Returned SimpleUser ID: {}", user_id);
|
||||
|
||||
println!("\nAfter saving - CustomUser ID: {}", saved_custom_user.get_id());
|
||||
println!("After saving - CustomUser DB Keys: {:?}", saved_custom_user.db_keys());
|
||||
println!("Returned CustomUser ID: {}", custom_user_id);
|
||||
|
||||
// Verify that the IDs were auto-generated
|
||||
assert_eq!(saved_user.get_id(), user_id);
|
||||
assert_ne!(saved_user.get_id(), 0);
|
||||
assert_eq!(saved_custom_user.get_id(), custom_user_id);
|
||||
assert_ne!(saved_custom_user.get_id(), 0);
|
||||
|
||||
println!("\nExample finished. DB stored at {}", db_path);
|
||||
println!("To clean up, you can manually delete the directory: {}", db_path);
|
||||
}
|
||||
|
@@ -1,3 +1,4 @@
|
||||
use heromodels::db::{Collection, Db};
|
||||
use heromodels_core::{BaseModelData, Model};
|
||||
use heromodels_derive::model;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -15,16 +16,36 @@ fn main() {
|
||||
println!("Hero Models - Simple Model Example");
|
||||
println!("==================================");
|
||||
|
||||
// Create a new DB instance, reset before every run
|
||||
let db_path = "/tmp/ourdb_simple_model_example";
|
||||
let db = heromodels::db::hero::OurDB::new(db_path, true).expect("Can create DB");
|
||||
|
||||
// Example usage of the generated implementation
|
||||
println!("SimpleUser DB Prefix: {}", SimpleUser::db_prefix());
|
||||
|
||||
// Create a new user with ID 0 (will be auto-generated when saved)
|
||||
let user = SimpleUser {
|
||||
base_data: BaseModelData::new(1),
|
||||
base_data: BaseModelData::new(),
|
||||
login: "johndoe".to_string(),
|
||||
full_name: "John Doe".to_string(),
|
||||
};
|
||||
|
||||
println!("\nSimpleUser ID: {}", user.get_id());
|
||||
println!("SimpleUser DB Keys: {:?}", user.db_keys());
|
||||
println!("\nBefore saving - SimpleUser ID: {}", user.get_id());
|
||||
println!("Before saving - SimpleUser DB Keys: {:?}", user.db_keys());
|
||||
|
||||
// Save the user to the database
|
||||
let collection = db.collection::<SimpleUser>().expect("can open user collection");
|
||||
let (user_id, saved_user) = collection.set(&user).expect("can save user");
|
||||
|
||||
println!("\nAfter saving - SimpleUser ID: {}", saved_user.get_id());
|
||||
println!("After saving - SimpleUser DB Keys: {:?}", saved_user.db_keys());
|
||||
println!("Returned ID: {}", user_id);
|
||||
|
||||
// Verify that the ID was auto-generated
|
||||
assert_eq!(saved_user.get_id(), user_id);
|
||||
assert_ne!(saved_user.get_id(), 0);
|
||||
|
||||
println!("\nExample finished. DB stored at {}", db_path);
|
||||
println!("To clean up, you can manually delete the directory: {}", db_path);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user