start porting model specs into heromodels

This commit is contained in:
timurgordon
2025-05-13 01:58:35 +03:00
parent 52528ca99f
commit c77d08033e
16 changed files with 1817 additions and 1 deletions

View File

@@ -0,0 +1,184 @@
use chrono::{DateTime, Utc};
use heromodels_core::BaseModelData;
use heromodels_derive::model;
use serde::{Deserialize, Serialize};
use rhai_autobind_macros::rhai_model_export;
use rhai::{CustomType, TypeBuilder};
/// Represents the status of an attendee for an event
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AttendanceStatus {
Accepted,
Declined,
Tentative,
NoResponse,
}
/// Represents an attendee of an event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Attendee {
/// ID of the user attending
// Assuming user_id might be queryable
pub user_id: String, // Using String for user_id similar to potential external IDs
/// Attendance status of the user for the event
pub status: AttendanceStatus,
}
impl Attendee {
pub fn new(user_id: String) -> Self {
Self {
user_id,
status: AttendanceStatus::NoResponse,
}
}
pub fn status(mut self, status: AttendanceStatus) -> Self {
self.status = status;
self
}
}
/// Represents an event in a calendar
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Event {
/// Unique identifier for the event (e.g., could be a UUID string or u32 if internally managed)
// Events might be looked up by their ID
pub id: String,
/// Title of the event
pub title: String,
/// Optional description of the event
pub description: Option<String>,
/// Start time of the event
pub start_time: DateTime<Utc>,
/// End time of the event
pub end_time: DateTime<Utc>,
/// List of attendees for the event
pub attendees: Vec<Attendee>,
/// Optional location of the event
pub location: Option<String>,
}
impl Event {
/// Creates a new event
pub fn new(id: String, title: impl ToString, start_time: DateTime<Utc>, end_time: DateTime<Utc>) -> Self {
Self {
id,
title: title.to_string(),
description: None,
start_time,
end_time,
attendees: Vec::new(),
location: None,
}
}
/// Sets the description for the event
pub fn description(mut self, description: impl ToString) -> Self {
self.description = Some(description.to_string());
self
}
/// Sets the location for the event
pub fn location(mut self, location: impl ToString) -> Self {
self.location = Some(location.to_string());
self
}
/// Adds an attendee to the event
pub fn add_attendee(mut self, attendee: Attendee) -> Self {
// Prevent duplicate attendees by user_id
if !self.attendees.iter().any(|a| a.user_id == attendee.user_id) {
self.attendees.push(attendee);
}
self
}
/// Removes an attendee from the event by user_id
pub fn remove_attendee(mut self, user_id: &str) -> Self {
self.attendees.retain(|a| a.user_id != user_id);
self
}
/// Updates the status of an existing attendee
pub fn update_attendee_status(mut self, user_id: &str, status: AttendanceStatus) -> Self {
if let Some(attendee) = self.attendees.iter_mut().find(|a| a.user_id == user_id) {
attendee.status = status;
}
self
}
/// Reschedules the event to new start and end times
pub fn reschedule(mut self, new_start_time: DateTime<Utc>, new_end_time: DateTime<Utc>) -> Self {
// Basic validation: end_time should be after start_time
if new_end_time > new_start_time {
self.start_time = new_start_time;
self.end_time = new_end_time;
}
// Optionally, add error handling or return a Result type
self
}
}
/// Represents a calendar with events
#[rhai_model_export(db_type = "std::sync::Arc<crate::db::hero::OurDB>")]
#[model]
#[derive(Debug, Clone, Serialize, Deserialize, CustomType)]
pub struct Calendar {
/// Base model data
pub base_data: BaseModelData,
/// Name of the calendar
pub name: String,
/// Optional description of the calendar
pub description: Option<String>,
/// List of events in the calendar
// For now, events are embedded. If they become separate models, this would be Vec<[IDType]>.
pub events: Vec<Event>,
}
impl Calendar {
/// Creates a new calendar
pub fn new(id: u32, name: impl ToString) -> Self {
Self {
base_data: BaseModelData::new(id),
name: name.to_string(),
description: None,
events: Vec::new(),
}
}
/// Sets the description for the calendar
pub fn description(mut self, description: impl ToString) -> Self {
self.description = Some(description.to_string());
self
}
/// Adds an event to the calendar
pub fn add_event(mut self, event: Event) -> Self {
// Prevent duplicate events by id
if !self.events.iter().any(|e| e.id == event.id) {
self.events.push(event);
}
self
}
/// Removes an event from the calendar by its ID
pub fn remove_event(mut self, event_id: &str) -> Self {
self.events.retain(|event| event.id != event_id);
self
}
/// Finds an event by its ID and allows modification
pub fn update_event<F>(mut self, event_id: &str, update_fn: F) -> Self
where
F: FnOnce(Event) -> Event,
{
if let Some(index) = self.events.iter().position(|e| e.id == event_id) {
let event = self.events.remove(index);
self.events.insert(index, update_fn(event));
}
self
}
}

View File

@@ -0,0 +1,5 @@
// Export calendar module
pub mod calendar;
// Re-export Calendar, Event, Attendee, and AttendanceStatus from the inner calendar module (calendar.rs) within src/models/calendar/mod.rs
pub use self::calendar::{Calendar, Event, Attendee, AttendanceStatus};

View File

@@ -0,0 +1,84 @@
// heromodels/src/models/finance/account.rs
use serde::{Deserialize, Serialize};
use heromodels_derive::model;
use heromodels_core::BaseModelData;
use super::asset::Asset;
/// Account represents a financial account owned by a user
#[derive(Debug, Clone, Serialize, Deserialize)]
#[model] // Has base.Base in V spec
pub struct Account {
pub base_data: BaseModelData,
pub name: String, // internal name of the account for the user
pub user_id: u32, // user id of the owner of the account
pub description: String, // optional description of the account
pub ledger: String, // describes the ledger/blockchain where the account is located
pub address: String, // address of the account on the blockchain
pub pubkey: String, // public key
pub assets: Vec<Asset>, // list of assets in this account
}
impl Account {
/// Create a new account
pub fn new(
id: u32,
name: impl ToString,
user_id: u32,
description: impl ToString,
ledger: impl ToString,
address: impl ToString,
pubkey: impl ToString
) -> Self {
Self {
base_data: BaseModelData::new(id),
name: name.to_string(),
user_id,
description: description.to_string(),
ledger: ledger.to_string(),
address: address.to_string(),
pubkey: pubkey.to_string(),
assets: Vec::new(),
}
}
/// Add an asset to the account
pub fn add_asset(mut self, asset: Asset) -> Self {
self.assets.push(asset);
self
}
/// Get the total value of all assets in the account
pub fn total_value(&self) -> f64 {
self.assets.iter().map(|asset| asset.amount).sum()
}
/// Find an asset by name
pub fn find_asset_by_name(&self, name: &str) -> Option<&Asset> {
self.assets.iter().find(|asset| asset.name == name)
}
/// Update the account details
pub fn update_details(
mut self,
name: Option<impl ToString>,
description: Option<impl ToString>,
address: Option<impl ToString>,
pubkey: Option<impl ToString>,
) -> Self {
if let Some(name) = name {
self.name = name.to_string();
}
if let Some(description) = description {
self.description = description.to_string();
}
if let Some(address) = address {
self.address = address.to_string();
}
if let Some(pubkey) = pubkey {
self.pubkey = pubkey.to_string();
}
self
}
}

View File

@@ -0,0 +1,85 @@
// heromodels/src/models/finance/asset.rs
use serde::{Deserialize, Serialize};
use heromodels_derive::model;
use heromodels_core::BaseModelData;
/// AssetType defines the type of blockchain asset
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AssetType {
Erc20, // ERC-20 token standard
Erc721, // ERC-721 NFT standard
Erc1155, // ERC-1155 Multi-token standard
Native, // Native blockchain asset (e.g., ETH, BTC)
}
impl Default for AssetType {
fn default() -> Self {
AssetType::Native
}
}
/// Asset represents a digital asset or token
#[derive(Debug, Clone, Serialize, Deserialize)]
#[model] // Has base.Base in V spec
pub struct Asset {
pub base_data: BaseModelData,
pub name: String, // Name of the asset
pub description: String, // Description of the asset
pub amount: f64, // Amount of the asset
pub address: String, // Address of the asset on the blockchain or bank
pub asset_type: AssetType, // Type of the asset
pub decimals: u8, // Number of decimals of the asset
}
impl Asset {
/// Create a new asset
pub fn new(
id: u32,
name: impl ToString,
description: impl ToString,
amount: f64,
address: impl ToString,
asset_type: AssetType,
decimals: u8,
) -> Self {
Self {
base_data: BaseModelData::new(id),
name: name.to_string(),
description: description.to_string(),
amount,
address: address.to_string(),
asset_type,
decimals,
}
}
/// Update the asset amount
pub fn update_amount(mut self, amount: f64) -> Self {
self.amount = amount;
self
}
/// Get the formatted amount with proper decimal places
pub fn formatted_amount(&self) -> String {
let factor = 10_f64.powi(self.decimals as i32);
let formatted_amount = (self.amount * factor).round() / factor;
format!("{:.1$}", formatted_amount, self.decimals as usize)
}
/// Transfer amount to another asset
pub fn transfer_to(&mut self, target: &mut Asset, amount: f64) -> Result<(), &'static str> {
if amount <= 0.0 {
return Err("Transfer amount must be positive");
}
if self.amount < amount {
return Err("Insufficient balance for transfer");
}
self.amount -= amount;
target.amount += amount;
Ok(())
}
}

View File

@@ -0,0 +1,286 @@
// heromodels/src/models/finance/marketplace.rs
use serde::{Deserialize, Serialize};
use heromodels_derive::model;
use heromodels_core::BaseModelData;
use chrono::{DateTime, Utc};
use super::asset::AssetType;
/// ListingStatus defines the status of a marketplace listing
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ListingStatus {
Active, // Listing is active and available
Sold, // Listing has been sold
Cancelled, // Listing was cancelled by the seller
Expired, // Listing has expired
}
impl Default for ListingStatus {
fn default() -> Self {
ListingStatus::Active
}
}
/// ListingType defines the type of marketplace listing
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ListingType {
FixedPrice, // Fixed price sale
Auction, // Auction with bids
Exchange, // Exchange for other assets
}
impl Default for ListingType {
fn default() -> Self {
ListingType::FixedPrice
}
}
/// BidStatus defines the status of a bid on an auction listing
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum BidStatus {
Active, // Bid is active
Accepted, // Bid was accepted
Rejected, // Bid was rejected
Cancelled, // Bid was cancelled by the bidder
}
impl Default for BidStatus {
fn default() -> Self {
BidStatus::Active
}
}
/// Bid represents a bid on an auction listing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Bid {
pub listing_id: String, // ID of the listing this bid belongs to
pub bidder_id: u32, // ID of the user who placed the bid
pub amount: f64, // Bid amount
pub currency: String, // Currency of the bid
pub status: BidStatus, // Status of the bid
pub created_at: DateTime<Utc>, // When the bid was created
}
impl Bid {
/// Create a new bid
pub fn new(
listing_id: impl ToString,
bidder_id: u32,
amount: f64,
currency: impl ToString,
) -> Self {
Self {
listing_id: listing_id.to_string(),
bidder_id,
amount,
currency: currency.to_string(),
status: BidStatus::default(),
created_at: Utc::now(),
}
}
/// Update the status of the bid
pub fn update_status(mut self, status: BidStatus) -> Self {
self.status = status;
self
}
}
/// Listing represents a marketplace listing for an asset
#[derive(Debug, Clone, Serialize, Deserialize)]
#[model] // Has base.Base in V spec
pub struct Listing {
pub base_data: BaseModelData,
pub title: String,
pub description: String,
pub asset_id: String,
pub asset_type: AssetType,
pub seller_id: String,
pub price: f64, // Initial price for fixed price, or starting price for auction
pub currency: String,
pub listing_type: ListingType,
pub status: ListingStatus,
pub expires_at: Option<DateTime<Utc>>, // Optional expiration date
pub sold_at: Option<DateTime<Utc>>, // Optional date when the item was sold
pub buyer_id: Option<String>, // Optional buyer ID
pub sale_price: Option<f64>, // Optional final sale price
pub bids: Vec<Bid>, // List of bids for auction type listings
pub tags: Vec<String>, // Tags for the listing
pub image_url: Option<String>, // Optional image URL
}
impl Listing {
/// Create a new listing
pub fn new(
id: u32,
title: impl ToString,
description: impl ToString,
asset_id: impl ToString,
asset_type: AssetType,
seller_id: impl ToString,
price: f64,
currency: impl ToString,
listing_type: ListingType,
expires_at: Option<DateTime<Utc>>,
tags: Vec<String>,
image_url: Option<impl ToString>,
) -> Self {
Self {
base_data: BaseModelData::new(id),
title: title.to_string(),
description: description.to_string(),
asset_id: asset_id.to_string(),
asset_type,
seller_id: seller_id.to_string(),
price,
currency: currency.to_string(),
listing_type,
status: ListingStatus::default(),
expires_at,
sold_at: None,
buyer_id: None,
sale_price: None,
bids: Vec::new(),
tags,
image_url: image_url.map(|url| url.to_string()),
}
}
/// Add a bid to an auction listing
pub fn add_bid(mut self, bid: Bid) -> Result<Self, &'static str> {
// Check if listing is an auction
if self.listing_type != ListingType::Auction {
return Err("Bids can only be placed on auction listings");
}
// Check if listing is active
if self.status != ListingStatus::Active {
return Err("Cannot place bid on inactive listing");
}
// Check if bid amount is higher than current price
if bid.amount <= self.price {
return Err("Bid amount must be higher than current price");
}
// Check if there are existing bids and if the new bid is higher
if let Some(highest_bid) = self.highest_bid() {
if bid.amount <= highest_bid.amount {
return Err("Bid amount must be higher than current highest bid");
}
}
// Add the bid
self.bids.push(bid);
// Update the current price to the new highest bid
if let Some(highest_bid) = self.highest_bid() {
self.price = highest_bid.amount;
}
Ok(self)
}
/// Get the highest active bid
pub fn highest_bid(&self) -> Option<&Bid> {
self.bids
.iter()
.filter(|bid| bid.status == BidStatus::Active)
.max_by(|a, b| a.amount.partial_cmp(&b.amount).unwrap())
}
/// Complete a sale (fixed price or auction)
pub fn complete_sale(mut self, buyer_id: impl ToString, sale_price: f64) -> Result<Self, &'static str> {
if self.status != ListingStatus::Active {
return Err("Cannot complete sale for inactive listing");
}
self.status = ListingStatus::Sold;
self.buyer_id = Some(buyer_id.to_string());
self.sale_price = Some(sale_price);
self.sold_at = Some(Utc::now());
// If this was an auction, accept the winning bid and reject others
if self.listing_type == ListingType::Auction {
for bid in &mut self.bids {
if bid.bidder_id.to_string() == self.buyer_id.as_ref().unwrap().to_string() && bid.amount == sale_price {
bid.status = BidStatus::Accepted;
} else {
bid.status = BidStatus::Rejected;
}
}
}
Ok(self)
}
/// Cancel the listing
pub fn cancel(mut self) -> Result<Self, &'static str> {
if self.status != ListingStatus::Active {
return Err("Cannot cancel inactive listing");
}
self.status = ListingStatus::Cancelled;
// Cancel all active bids
for bid in &mut self.bids {
if bid.status == BidStatus::Active {
bid.status = BidStatus::Cancelled;
}
}
Ok(self)
}
/// Check if the listing has expired and update status if needed
pub fn check_expiration(mut self) -> Self {
if self.status == ListingStatus::Active {
if let Some(expires_at) = self.expires_at {
if Utc::now() > expires_at {
self.status = ListingStatus::Expired;
// Cancel all active bids
for bid in &mut self.bids {
if bid.status == BidStatus::Active {
bid.status = BidStatus::Cancelled;
}
}
}
}
}
self
}
/// Add tags to the listing
pub fn add_tags(mut self, tags: Vec<impl ToString>) -> Self {
for tag in tags {
self.tags.push(tag.to_string());
}
self
}
/// Update the listing details
pub fn update_details(
mut self,
title: Option<impl ToString>,
description: Option<impl ToString>,
price: Option<f64>,
image_url: Option<impl ToString>,
) -> Self {
if let Some(title) = title {
self.title = title.to_string();
}
if let Some(description) = description {
self.description = description.to_string();
}
if let Some(price) = price {
self.price = price;
}
if let Some(image_url) = image_url {
self.image_url = Some(image_url.to_string());
}
self
}
}

View File

@@ -0,0 +1,10 @@
// heromodels/src/models/finance/mod.rs
// This module contains finance-related models: Account, Asset, and Marketplace
pub mod account;
pub mod asset;
pub mod marketplace;
pub use self::account::Account;
pub use self::asset::{Asset, AssetType};
pub use self::marketplace::{Listing, ListingStatus, ListingType, Bid, BidStatus};

View File

@@ -0,0 +1,5 @@
// heromodels/src/models/governance/mod.rs
// This module will contain the Proposal model and related types.
pub mod proposal;
pub use self::proposal::{Proposal, Ballot, VoteOption, ProposalStatus, VoteEventStatus};

View File

@@ -0,0 +1,166 @@
// heromodels/src/models/governance/proposal.rs
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use heromodels_derive::model; // For #[model]
use rhai_autobind_macros::rhai_model_export;
use rhai::{CustomType, TypeBuilder};
use heromodels_core::BaseModelData;
// --- Enums ---
/// ProposalStatus defines the lifecycle status of a governance proposal itself
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ProposalStatus {
Draft, // Proposal is being prepared
Active, // Proposal is active
Approved, // Proposal has been formally approved
Rejected, // Proposal has been formally rejected
Cancelled,// Proposal was cancelled
}
impl Default for ProposalStatus {
fn default() -> Self {
ProposalStatus::Draft
}
}
/// VoteEventStatus represents the status of the voting process for a proposal
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum VoteEventStatus {
Open, // Voting is currently open
Closed, // Voting has finished
Cancelled, // The voting event was cancelled
}
impl Default for VoteEventStatus {
fn default() -> Self {
VoteEventStatus::Open
}
}
// --- Structs ---
/// VoteOption represents a specific choice that can be voted on
#[derive(Debug, Clone, Serialize, Deserialize, CustomType)]
pub struct VoteOption {
pub id: u8, // Simple identifier for this option
pub text: String, // Descriptive text of the option
pub count: i64, // How many votes this option has received
pub min_valid: Option<i64>, // Optional: minimum votes needed
}
impl VoteOption {
pub fn new(id: u8, text: impl ToString) -> Self {
Self {
id,
text: text.to_string(),
count: 0,
min_valid: None,
}
}
}
/// Ballot represents an individual vote cast by a user
#[derive(Debug, Clone, Serialize, Deserialize, CustomType)]
#[rhai_model_export(db_type = "std::sync::Arc<crate::db::hero::OurDB>")]
#[model] // Has base.Base in V spec
pub struct Ballot {
pub base_data: BaseModelData,
pub user_id: u32, // The ID of the user who cast this ballot
pub vote_option_id: u8, // The 'id' of the VoteOption chosen
pub shares_count: i64, // Number of shares/tokens/voting power
}
impl Ballot {
pub fn new(id: u32, user_id: u32, vote_option_id: u8, shares_count: i64) -> Self {
Self {
base_data: BaseModelData::new(id),
user_id,
vote_option_id,
shares_count,
}
}
}
/// Proposal represents a governance proposal that can be voted upon.
#[derive(Debug, Clone, Serialize, Deserialize, CustomType)]
#[rhai_model_export(db_type = "std::sync::Arc<crate::db::hero::OurDB>")]
#[model] // Has base.Base in V spec
pub struct Proposal {
pub base_data: BaseModelData,
pub creator_id: String, // User ID of the proposal creator
pub title: String,
pub description: String,
pub status: ProposalStatus,
// Voting event aspects
pub vote_start_date: DateTime<Utc>,
pub vote_end_date: DateTime<Utc>,
pub vote_status: VoteEventStatus,
pub options: Vec<VoteOption>,
pub ballots: Vec<Ballot>, // This will store actual Ballot structs
pub private_group: Option<Vec<u32>>, // Optional list of eligible user IDs
}
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 {
Self {
base_data: BaseModelData::new(id),
creator_id: creator_id.to_string(),
title: title.to_string(),
description: description.to_string(),
status: ProposalStatus::Draft,
vote_start_date,
vote_end_date,
vote_status: VoteEventStatus::Open, // Default to open when created
options: Vec::new(),
ballots: Vec::new(),
private_group: None,
}
}
pub fn add_option(mut self, option_id: u8, option_text: impl ToString) -> Self {
let new_option = VoteOption::new(option_id, option_text);
self.options.push(new_option);
self
}
pub fn cast_vote(mut self, ballot_id: 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;
}
if !self.options.iter().any(|opt| opt.id == chosen_option_id) {
eprintln!("Chosen option ID {} does not exist for proposal '{}'", chosen_option_id, self.title);
return self;
}
if let Some(group) = &self.private_group {
if !group.contains(&user_id) {
eprintln!("User {} is not eligible to vote on proposal '{}'", user_id, self.title);
return self;
}
}
let new_ballot = Ballot::new(ballot_id, user_id, chosen_option_id, shares);
self.ballots.push(new_ballot);
if let Some(option) = self.options.iter_mut().find(|opt| opt.id == chosen_option_id) {
option.count += shares;
}
self
}
pub fn change_proposal_status(mut self, new_status: ProposalStatus) -> Self {
self.status = new_status;
self
}
pub fn change_vote_event_status(mut self, new_status: VoteEventStatus) -> Self {
self.vote_status = new_status;
self
}
}

View File

@@ -1,8 +1,16 @@
// Export submodules
pub mod core;
pub mod userexample;
// pub mod productexample; // Temporarily remove as files are missing
pub mod calendar;
pub mod governance;
pub mod finance;
// Re-export key types for convenience
pub use core::Comment;
pub use userexample::User;
// pub use productexample::Product; // Temporarily remove
pub use calendar::{Calendar, Event, Attendee, AttendanceStatus};
pub use governance::{Proposal, ProposalStatus, VoteEventStatus, Ballot, VoteOption};
pub use finance::{Account, Asset, AssetType};
pub use finance::marketplace::{Listing, ListingStatus, ListingType, Bid, BidStatus};