feat: Support incremental mode:

- Support incremental mode in heromodels
- Updated the example to refelct the changes
- Updated the tests to reflect the changes
This commit is contained in:
Mahmoud Emad 2025-05-17 11:12:09 +03:00
parent bd4770b99b
commit bde5db0e52
16 changed files with 1074 additions and 136 deletions

19
.gitignore vendored
View File

@ -4,3 +4,22 @@ target/
*.wasm
herovm_build/
test_db
<<<<<<< Updated upstream
=======
# Node.js
**/node_modules/
**/dist/
**/*.log
**/package-lock.json
# TypeScript
**/*.js.map
**/*.d.ts
**/*.tsbuildinfo
rhaiinterface/client/**/*.js
rhaiinterface/server/**/*.js
!rhaiinterface/server/examples/**/client.js
!rhaiinterface/server/examples/webpack.config.js
.vscode
>>>>>>> Stashed changes

507
adapter_macros/Cargo.lock generated Normal file
View File

@ -0,0 +1,507 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "adapter_macros"
version = "0.1.0"
dependencies = [
"chrono",
"rhai",
]
[[package]]
name = "ahash"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75"
dependencies = [
"cfg-if",
"const-random",
"getrandom 0.3.3",
"once_cell",
"version_check",
"zerocopy",
]
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "autocfg"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "bitflags"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "bumpalo"
version = "3.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf"
[[package]]
name = "cc"
version = "1.2.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f4ac86a9e5bc1e2b3449ab9d7d3a6a405e3d1bb28d7b9be8614f55846ae3766"
dependencies = [
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-link",
]
[[package]]
name = "const-random"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87e00182fe74b066627d63b85fd550ac2998d4b0bd86bfed477a0ae4c7c71359"
dependencies = [
"const-random-macro",
]
[[package]]
name = "const-random-macro"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e"
dependencies = [
"getrandom 0.2.16",
"once_cell",
"tiny-keccak",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "crunchy"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929"
[[package]]
name = "getrandom"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592"
dependencies = [
"cfg-if",
"libc",
"wasi 0.11.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
dependencies = [
"cfg-if",
"libc",
"r-efi",
"wasi 0.14.2+wasi-0.2.4",
]
[[package]]
name = "iana-time-zone"
version = "0.1.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0c919e5debc312ad217002b8048a17b7d83f80703865bbfcfebb0458b0b27d8"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"log",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "instant"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222"
dependencies = [
"cfg-if",
]
[[package]]
name = "js-sys"
version = "0.3.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
dependencies = [
"once_cell",
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.172"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
[[package]]
name = "log"
version = "0.4.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94"
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
dependencies = [
"portable-atomic",
]
[[package]]
name = "portable-atomic"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e"
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "r-efi"
version = "5.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5"
[[package]]
name = "rhai"
version = "1.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce4d759a4729a655ddfdbb3ff6e77fb9eadd902dae12319455557796e435d2a6"
dependencies = [
"ahash",
"bitflags",
"instant",
"num-traits",
"once_cell",
"rhai_codegen",
"smallvec",
"smartstring",
"thin-vec",
]
[[package]]
name = "rhai_codegen"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5a11a05ee1ce44058fa3d5961d05194fdbe3ad6b40f904af764d81b86450e6b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "rustversion"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2"
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "smallvec"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8917285742e9f3e1683f0a9c4e6b57960b7314d0b08d30d1ecd426713ee2eee9"
[[package]]
name = "smartstring"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fb72c633efbaa2dd666986505016c32c3044395ceaf881518399d2f4127ee29"
dependencies = [
"autocfg",
"static_assertions",
"version_check",
]
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "syn"
version = "2.0.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thin-vec"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "144f754d318415ac792f9d69fc87abbbfc043ce2ef041c60f16ad828f638717d"
[[package]]
name = "tiny-keccak"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237"
dependencies = [
"crunchy",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasi"
version = "0.14.2+wasi-0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "wasm-bindgen"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
dependencies = [
"bumpalo",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
dependencies = [
"unicode-ident",
]
[[package]]
name = "windows-core"
version = "0.61.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46ec44dc15085cea82cf9c78f85a9114c463a369786585ad2882d1ff0b0acf40"
dependencies = [
"windows-implement",
"windows-interface",
"windows-link",
"windows-result",
"windows-strings",
]
[[package]]
name = "windows-implement"
version = "0.60.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a47fddd13af08290e67f4acabf4b459f647552718f683a7b415d290ac744a836"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-interface"
version = "0.59.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd9211b69f8dcdfa817bfd14bf1c97c9188afa36f4750130fcdf3f400eca9fa8"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-link"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76840935b766e1b0a05c0066835fb9ec80071d4c09a16f6bd5f7e655e3c14c38"
[[package]]
name = "windows-result"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b895b5356fc36103d0f64dd1e94dfa7ac5633f1c9dd6e80fe9ec4adef69e09d"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-strings"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a7ab927b2637c19b3dbe0965e75d8f2d30bdd697a1516191cad2ec4df8fb28a"
dependencies = [
"windows-link",
]
[[package]]
name = "wit-bindgen-rt"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [
"bitflags",
]
[[package]]
name = "zerocopy"
version = "0.8.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1702d9583232ddb9174e01bb7c15a2ab8fb1bc6f227aa1233858c351a3ba0cb"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28a6e20d751156648aa063f3800b706ee209a32c0b4d9f24be3d980b01be55ef"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

View File

@ -130,7 +130,7 @@ pub fn model(_attr: TokenStream, item: TokenStream) -> TokenStream {
}
fn get_id(&self) -> u32 {
self.base_data.id
self.base_data.id.unwrap_or(0)
}
fn base_data_mut(&mut self) -> &mut heromodels_core::BaseModelData {

View File

@ -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,75 @@ 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 different ID configurations
// User 1: With explicit ID
let user1 = User::new(Some(1))
.username("johndoe")
.email("john.doe@example.com")
.full_name("John Doe")
.is_active(false)
.build();
let user2 = User::new(2)
// User 2: With auto-generated ID
let user2 = User::new(None)
.username("janesmith")
.email("jane.smith@example.com")
.full_name("Jane Smith")
.is_active(true)
.build();
let user3 = User::new(3)
// User 3: With explicit ID
let user3 = User::new(Some(3))
.username("willism")
.email("willis.masters@example.com")
.full_name("Willis Masters")
.is_active(true)
.build();
let user4 = User::new(4)
// User 4: With explicit ID
let user4 = User::new(Some(4))
.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
db.collection().expect("can open user collection").set(&user1).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");
// Perform an indexed lookup on the Username
// Retrieve all users from database
let db_user1 = db.collection::<User>().expect("can open user collection")
.get_by_id(user1.get_id()).expect("can load user").expect("user should exist");
let db_user2 = db.collection::<User>().expect("can open user collection")
.get_by_id(user2.get_id()).expect("can load user").expect("user should exist");
let db_user3 = db.collection::<User>().expect("can open user collection")
.get_by_id(user3.get_id()).expect("can load user").expect("user should exist");
let db_user4 = db.collection::<User>().expect("can open user collection")
.get_by_id(user4.get_id()).expect("can load user").expect("user should exist");
// Print all users retrieved from database
println!("\n--- Users Retrieved from Database ---");
println!("\n1. User with explicit ID (1):");
print_user_details(&db_user1);
println!("\n2. User with auto-generated ID:");
print_user_details(&db_user2);
println!("\n3. User with explicit ID (3):");
print_user_details(&db_user3);
println!("\n4. User with explicit ID (4):");
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 +109,105 @@ 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:");
db.collection::<User>()
.expect("can open user collection")
.delete_by_id(active_users[0].get_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(None)
.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")
.expect("can open comment collection")
.set(&comment)
.expect("can set comment");
let stored_comment = db
// 2. Retrieve the comment from database
let db_comment = db
.collection::<Comment>()
.expect("can open comment collection")
.get_by_id(5)
.expect("can load stored comment");
.get_by_id(comment.get_id())
.expect("can load comment")
.expect("comment should exist");
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());
db.collection::<User>()
.expect("can open user collection")
.set(&updated_user)
.expect("can set updated user");
// 4. Retrieve the updated user
let user_with_comment = db
.collection::<User>()
.expect("can open user collection")
.get_by_id(updated_user.get_id())
.expect("can load user")
.expect("user should exist");
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());
}

View File

@ -50,12 +50,15 @@ fn main() {
// --- Create Calendars ---
// Note: Calendar::new directly returns Calendar, no separate .build() step like the user example.
let calendar1 = Calendar::new(1, "Work Calendar")
// 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, "Personal Calendar")
// Create a calendar with explicit ID
let calendar2 = Calendar::new(Some(2), "Personal Calendar")
.add_event(event3_for_calendar2.clone());

View File

@ -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)
@ -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
@ -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

View File

@ -6,9 +6,9 @@ use heromodels::models::governance::{Proposal, ProposalStatus, VoteEventStatus};
fn main() {
println!("Governance Proposal Model Example\n");
// Create a new proposal
// Create a new proposal with auto-generated ID
let mut proposal = Proposal::new(
1, // id
None, // id (auto-generated)
"user_creator_123", // creator_id
"Community Fund Allocation for Q3", // title
"Proposal to allocate funds for community projects in the third quarter.", // description
@ -33,18 +33,18 @@ fn main() {
// 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 {
@ -68,7 +68,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:");
@ -83,7 +83,7 @@ fn main() {
// Example of a private proposal (not fully implemented in cast_vote eligibility yet)
let mut private_proposal = Proposal::new(
2,
Some(2), // explicit ID
"user_admin_001",
"Internal Team Restructure Vote",
"Vote on proposed internal team changes.",
@ -96,10 +96,10 @@ fn main() {
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 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);
println!("Private Proposal Vote Counts:");
for option in &private_proposal.options {

View File

@ -140,7 +140,11 @@ pub struct Calendar {
impl Calendar {
/// Creates a new calendar
pub fn new(id: u32, name: impl ToString) -> Self {
///
/// # Arguments
/// * `id` - Optional ID for the calendar. If None, the ID will be auto-generated.
/// * `name` - Name of the calendar
pub fn new(id: Option<u32>, name: impl ToString) -> Self {
Self {
base_data: BaseModelData::new(id),
name: name.to_string(),

View File

@ -14,7 +14,10 @@ pub struct Comment {
impl Comment {
/// Create a new comment
pub fn new(id: u32) -> Self {
///
/// # Arguments
/// * `id` - Optional ID for the comment. If None, the ID will be auto-generated.
pub fn new(id: Option<u32>) -> Self {
Self {
base_data: BaseModelData::new(id),
user_id: 0,

View File

@ -22,8 +22,17 @@ pub struct Account {
impl Account {
/// Create a new account
///
/// # Arguments
/// * `id` - Optional ID for the account. If None, the ID will be auto-generated.
/// * `name` - Name of the account
/// * `user_id` - ID of the user who owns the account
/// * `description` - Description of the account
/// * `ledger` - Ledger/blockchain where the account is located
/// * `address` - Address of the account on the blockchain
/// * `pubkey` - Public key
pub fn new(
id: u32,
id: Option<u32>,
name: impl ToString,
user_id: u32,
description: impl ToString,

View File

@ -35,7 +35,7 @@ pub struct Asset {
impl Asset {
/// Create a new asset
pub fn new(
id: u32,
id: Option<u32>,
name: impl ToString,
description: impl ToString,
amount: f64,

View File

@ -113,7 +113,7 @@ pub struct Listing {
impl Listing {
/// Create a new listing
pub fn new(
id: u32,
id: Option<u32>,
title: impl ToString,
description: impl ToString,
asset_id: impl ToString,

View File

@ -75,7 +75,14 @@ pub struct Ballot {
}
impl Ballot {
pub fn new(id: u32, user_id: u32, vote_option_id: u8, shares_count: i64) -> Self {
/// Create a new ballot
///
/// # Arguments
/// * `id` - Optional ID for the ballot. If None, the ID will be auto-generated.
/// * `user_id` - ID of the user who cast this ballot
/// * `vote_option_id` - ID of the vote option chosen
/// * `shares_count` - Number of shares/tokens/voting power
pub fn new(id: Option<u32>, user_id: u32, vote_option_id: u8, shares_count: i64) -> Self {
Self {
base_data: BaseModelData::new(id),
user_id,
@ -107,7 +114,16 @@ pub struct Proposal {
}
impl Proposal {
pub fn new(id: u32, creator_id: impl ToString, title: impl ToString, description: impl ToString, vote_start_date: DateTime<Utc>, vote_end_date: DateTime<Utc>) -> Self {
/// Create a new proposal
///
/// # Arguments
/// * `id` - Optional ID for the proposal. If None, the ID will be auto-generated.
/// * `creator_id` - ID of the user who created the proposal
/// * `title` - Title of the proposal
/// * `description` - Description of the proposal
/// * `vote_start_date` - Date when voting starts
/// * `vote_end_date` - Date when voting ends
pub fn new(id: Option<u32>, creator_id: impl ToString, title: impl ToString, description: impl ToString, vote_start_date: DateTime<Utc>, vote_end_date: DateTime<Utc>) -> Self {
Self {
base_data: BaseModelData::new(id),
creator_id: creator_id.to_string(),
@ -129,7 +145,7 @@ impl Proposal {
self
}
pub fn cast_vote(mut self, ballot_id: u32, user_id: u32, chosen_option_id: u8, shares: i64) -> Self {
pub fn cast_vote(mut self, ballot_id: Option<u32>, user_id: u32, chosen_option_id: u8, shares: i64) -> Self {
if self.vote_status != VoteEventStatus::Open {
eprintln!("Voting is not open for proposal '{}'", self.title);
return self;

View File

@ -27,7 +27,10 @@ pub struct User {
impl User {
/// Create a new user
pub fn new(id: u32) -> Self {
///
/// # Arguments
/// * `id` - Optional ID for the user. If None, the ID will be auto-generated.
pub fn new(id: Option<u32>) -> Self {
Self {
base_data: BaseModelData::new(id),
username: String::new(),

View File

@ -59,13 +59,14 @@ pub trait Model:
}
/// Get the unique ID for this model
/// Returns 0 if the ID is None
fn get_id(&self) -> u32;
/// Get a mutable reference to the base_data field
fn base_data_mut(&mut self) -> &mut BaseModelData;
/// Set the ID for this model
fn id(mut self, id: u32) -> Self
fn id(mut self, id: Option<u32>) -> Self
where
Self: Sized,
{
@ -98,7 +99,7 @@ pub trait Index {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BaseModelData {
/// Unique incremental ID per circle
pub id: u32,
pub id: Option<u32>,
/// Unix epoch timestamp for creation time
pub created_at: i64,
@ -112,7 +113,7 @@ pub struct BaseModelData {
impl BaseModelData {
/// Create a new BaseModelData instance
pub fn new(id: u32) -> Self {
pub fn new(id: Option<u32>) -> Self {
let now = chrono::Utc::now().timestamp();
Self {
id,
@ -123,7 +124,7 @@ impl BaseModelData {
}
/// Create a new BaseModelDataBuilder
pub fn builder(id: u32) -> BaseModelDataBuilder {
pub fn builder(id: Option<u32>) -> BaseModelDataBuilder {
BaseModelDataBuilder::new(id)
}
@ -147,7 +148,7 @@ impl BaseModelData {
/// Builder for BaseModelData
pub struct BaseModelDataBuilder {
id: u32,
id: Option<u32>,
created_at: Option<i64>,
modified_at: Option<i64>,
comments: Vec<u32>,
@ -155,7 +156,7 @@ pub struct BaseModelDataBuilder {
impl BaseModelDataBuilder {
/// Create a new BaseModelDataBuilder
pub fn new(id: u32) -> Self {
pub fn new(id: Option<u32>) -> Self {
Self {
id,
created_at: None,

290
rhai_client_macros/Cargo.lock generated Normal file
View File

@ -0,0 +1,290 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "ahash"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75"
dependencies = [
"cfg-if",
"const-random",
"getrandom 0.3.3",
"once_cell",
"version_check",
"zerocopy",
]
[[package]]
name = "autocfg"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "bitflags"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "const-random"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87e00182fe74b066627d63b85fd550ac2998d4b0bd86bfed477a0ae4c7c71359"
dependencies = [
"const-random-macro",
]
[[package]]
name = "const-random-macro"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e"
dependencies = [
"getrandom 0.2.16",
"once_cell",
"tiny-keccak",
]
[[package]]
name = "crunchy"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929"
[[package]]
name = "getrandom"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592"
dependencies = [
"cfg-if",
"libc",
"wasi 0.11.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
dependencies = [
"cfg-if",
"libc",
"r-efi",
"wasi 0.14.2+wasi-0.2.4",
]
[[package]]
name = "instant"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222"
dependencies = [
"cfg-if",
]
[[package]]
name = "libc"
version = "0.2.172"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
dependencies = [
"portable-atomic",
]
[[package]]
name = "portable-atomic"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e"
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "r-efi"
version = "5.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5"
[[package]]
name = "rhai"
version = "1.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce4d759a4729a655ddfdbb3ff6e77fb9eadd902dae12319455557796e435d2a6"
dependencies = [
"ahash",
"bitflags",
"instant",
"num-traits",
"once_cell",
"rhai_codegen",
"smallvec",
"smartstring",
"thin-vec",
]
[[package]]
name = "rhai_client_macros"
version = "0.1.0"
dependencies = [
"proc-macro2",
"quote",
"rhai",
"syn",
]
[[package]]
name = "rhai_codegen"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5a11a05ee1ce44058fa3d5961d05194fdbe3ad6b40f904af764d81b86450e6b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "smallvec"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8917285742e9f3e1683f0a9c4e6b57960b7314d0b08d30d1ecd426713ee2eee9"
[[package]]
name = "smartstring"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fb72c633efbaa2dd666986505016c32c3044395ceaf881518399d2f4127ee29"
dependencies = [
"autocfg",
"static_assertions",
"version_check",
]
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "syn"
version = "2.0.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thin-vec"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "144f754d318415ac792f9d69fc87abbbfc043ce2ef041c60f16ad828f638717d"
[[package]]
name = "tiny-keccak"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237"
dependencies = [
"crunchy",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasi"
version = "0.14.2+wasi-0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "wit-bindgen-rt"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [
"bitflags",
]
[[package]]
name = "zerocopy"
version = "0.8.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1702d9583232ddb9174e01bb7c15a2ab8fb1bc6f227aa1233858c351a3ba0cb"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28a6e20d751156648aa063f3800b706ee209a32c0b4d9f24be3d980b01be55ef"
dependencies = [
"proc-macro2",
"quote",
"syn",
]