update to follow rhailib

This commit is contained in:
Timur Gordon
2025-06-21 09:23:22 +02:00
parent e91a44ce37
commit e2640b9421
16 changed files with 1500 additions and 24 deletions

View File

@@ -0,0 +1,222 @@
// Governance Example - Demonstrates using the governance models with embedded types
// This example shows how to create, retrieve, and manipulate governance entities with embedded types
// Get the database instance
let db = get_db();
println("Hero Models - Governance Example");
println("================================");
// Create a company with a business type using the fluent builder pattern
println("Creating a company...");
// Create a business type directly
let business_type = #{
type_name: "Corporation",
description: "A corporation is a legal entity that is separate and distinct from its owners"
};
let company = company__builder(1)
.name("Acme Corporation")
.registration_number("REG123456")
.incorporation_date(now())
.fiscal_year_end("December 31")
.email("info@acme.com")
.phone("+1 555-123-4567")
.website("https://acme.com")
.address("123 Main St, Anytown, USA")
.business_type(business_type)
.industry("Technology")
.description("A leading technology company")
.status("Active")
.build();
// Save the company to the database
set_company(db, company);
println("Company created successfully!");
println("");
// Create a committee with members using the fluent builder pattern
println("Creating a committee...");
let committee = committee__builder(1)
.company_id(1)
.name("Board of Directors")
.description("The main governing body of the company")
.add_member(
committee_member()
.id(1)
.user_id(101)
.name("John Smith")
.role(CommitteeRole::Chair)
)
.add_member(
committee_member()
.id(2)
.user_id(102)
.name("Jane Doe")
.role(CommitteeRole::Secretary)
)
.add_member(
committee_member()
.id(3)
.user_id(103)
.name("Bob Johnson")
.role(CommitteeRole::Member)
)
.build();
// Save the committee to the database
set_committee(db, committee);
println("Committee created successfully!");
println("");
// Create a meeting with attendees using the fluent builder pattern
println("Creating a meeting...");
let meeting = meeting__builder(1)
.company_id(1)
.title("Q2 Board Meeting")
.date(now() + days(7))
.location("Conference Room A")
.description("Quarterly board meeting to review financial performance")
.add_attendee(
attendee()
.id(1)
.user_id(101)
.name("John Smith")
.role(AttendeeRole::Coordinator)
.status(AttendeeStatus::Confirmed)
)
.add_attendee(
attendee()
.id(2)
.user_id(102)
.name("Jane Doe")
.role(AttendeeRole::Secretary)
.status(AttendeeStatus::Confirmed)
)
.add_attendee(
attendee()
.id(3)
.user_id(103)
.name("Bob Johnson")
.role(AttendeeRole::Member)
.status(AttendeeStatus::Pending)
)
.build();
// Save the meeting to the database
set_meeting(db, meeting);
println("Meeting created successfully!");
println("");
// Create a resolution with approvals using the fluent builder pattern
println("Creating a resolution...");
let resolution = resolution__builder(1)
.company_id(1)
.title("New Product Line Resolution")
.description("Resolution to approve the development of a new product line")
.resolution_type(ResolutionType::Ordinary)
.status(ResolutionStatus::Proposed)
.proposed_date(now())
.effective_date(now() + days(30))
.expiry_date(now() + days(365))
.add_approval("John Smith")
.add_approval("Jane Doe")
.build();
// Save the resolution to the database
set_resolution(db, resolution);
println("Resolution created successfully!");
println("");
// Create a vote with ballots using the fluent builder pattern
println("Creating a vote...");
let vote = vote__builder(1)
.company_id(1)
.resolution_id(1)
.title("Vote on New Product Line")
.description("Vote to approve the development of a new product line")
.status(VoteStatus::Open)
.add_ballot(
ballot()
.id(1)
.user_id(101)
.user_name("John Smith")
.option(VoteOption::Yes)
.comments("Strongly support this initiative")
)
.add_ballot(
ballot()
.id(2)
.user_id(102)
.user_name("Jane Doe")
.option(VoteOption::Yes)
.comments("Agree with the proposal")
)
.add_ballot(
ballot()
.id(3)
.user_id(103)
.user_name("Bob Johnson")
.option(VoteOption::Abstain)
.comments("Need more information")
)
.start_date(now())
.end_date(now() + days(7))
.build();
// Save the vote to the database
set_vote(db, vote);
println("Vote created successfully!");
println("");
// Retrieve and display the company
println("Retrieving company...");
let retrieved_company = get_company_by_id(db, 1);
println("Company: " + retrieved_company.name);
println("Business Type: " + retrieved_company.business_type.type_name);
println("");
// Retrieve and display the committee
println("Retrieving committee...");
let retrieved_committee = get_committee_by_id(db, 1);
println("Committee: " + retrieved_committee.name);
println("Members: " + retrieved_committee.members.len());
for member in retrieved_committee.members {
println("- " + member.name + " (" + member.role + ")");
}
println("");
// Retrieve and display the meeting
println("Retrieving meeting...");
let retrieved_meeting = get_meeting_by_id(db, 1);
println("Meeting: " + retrieved_meeting.title);
println("Date: " + retrieved_meeting.date);
println("Attendees: " + retrieved_meeting.attendees.len());
for attendee in retrieved_meeting.attendees {
println("- " + attendee.name + " (" + attendee.role + ", " + attendee.status + ")");
}
println("");
// Retrieve and display the resolution
println("Retrieving resolution...");
let retrieved_resolution = get_resolution_by_id(db, 1);
println("Resolution: " + retrieved_resolution.title);
println("Status: " + retrieved_resolution.status);
println("Approvals: " + retrieved_resolution.approvals.len());
for approval in retrieved_resolution.approvals {
println("- " + approval);
}
println("");
// Retrieve and display the vote
println("Retrieving vote...");
let retrieved_vote = get_vote_by_id(db, 1);
println("Vote: " + retrieved_vote.title);
println("Status: " + retrieved_vote.status);
println("Ballots: " + retrieved_vote.ballots.len());
for ballot in retrieved_vote.ballots {
println("- " + ballot.user_name + ": " + ballot.option);
}
println("");
println("Governance example completed successfully!");
()

View File

@@ -0,0 +1,57 @@
use heromodels::db::hero::OurDB;
use heromodels::register_db_functions;
use rhai::Engine;
use std::sync::Arc;
use std::fs;
use std::path::Path;
use chrono::{DateTime, Utc, Duration};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a temporary directory for the database
let temp_dir = std::env::temp_dir().join("heromodels_gov_example");
if temp_dir.exists() {
fs::remove_dir_all(&temp_dir)?;
}
fs::create_dir_all(&temp_dir)?;
println!("Using temporary database at: {}", temp_dir.display());
// Create a new OurDB instance
let db = OurDB::new(&temp_dir, true)?;
let db = Arc::new(db);
// Create a new Rhai engine
let mut engine = Engine::new();
// Register the auto-generated DB functions with the engine
register_db_functions(&mut engine);
// Register print functions
engine.register_fn("println", |s: &str| println!("{}", s));
engine.register_fn("print", |s: &str| print!("{}", s));
// Register date/time functions
engine.register_fn("now", || Utc::now());
engine.register_fn("days", |n: i64| Duration::days(n));
engine.register_fn("+", |dt: DateTime<Utc>, duration: Duration| dt + duration);
// Add the DB instance to the engine's scope
engine.register_fn("get_db", move || -> Arc<OurDB> { db.clone() });
// Load and execute the Rhai script
let script_path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("examples")
.join("gov_example.rhai");
println!("Loading Rhai script from: {}", script_path.display());
match engine.eval_file::<()>(script_path) {
Ok(_) => println!("Script executed successfully"),
Err(e) => eprintln!("Error executing script: {}", e),
}
// Clean up the temporary directory
fs::remove_dir_all(&temp_dir)?;
Ok(())
}