fmt, fixes and additions
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
use rhai::plugin::*;
|
||||
use rhai::{Engine, EvalAltResult, Position, Module, INT, Dynamic, Array};
|
||||
use std::sync::Arc;
|
||||
use std::mem;
|
||||
use chrono::Utc;
|
||||
use rhai::plugin::*;
|
||||
use rhai::{Array, Dynamic, Engine, EvalAltResult, INT, Module, Position};
|
||||
use std::mem;
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::account::Account;
|
||||
use super::asset::{Asset, AssetType};
|
||||
use super::marketplace::{Listing, Bid, ListingStatus, ListingType, BidStatus};
|
||||
use super::marketplace::{Bid, BidStatus, Listing, ListingStatus, ListingType};
|
||||
|
||||
use crate::db::hero::OurDB;
|
||||
use crate::db::{Collection, Db};
|
||||
@@ -18,12 +18,12 @@ type RhaiBid = Bid;
|
||||
|
||||
// Helper to convert i64 from Rhai to u32 for IDs
|
||||
fn id_from_i64_to_u32(id_i64: i64) -> Result<u32, Box<EvalAltResult>> {
|
||||
u32::try_from(id_i64).map_err(|_|
|
||||
u32::try_from(id_i64).map_err(|_| {
|
||||
Box::new(EvalAltResult::ErrorArithmetic(
|
||||
format!("Failed to convert ID '{}' to u32", id_i64).into(),
|
||||
Position::NONE
|
||||
Position::NONE,
|
||||
))
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
// Helper functions for enum conversions
|
||||
@@ -45,8 +45,12 @@ fn string_to_asset_type(s: &str) -> Result<AssetType, Box<EvalAltResult>> {
|
||||
"Erc721" => Ok(AssetType::Erc721),
|
||||
"Erc1155" => Ok(AssetType::Erc1155),
|
||||
_ => Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Invalid asset type: '{}'. Expected one of: Native, Erc20, Erc721, Erc1155", s).into(),
|
||||
Position::NONE
|
||||
format!(
|
||||
"Invalid asset type: '{}'. Expected one of: Native, Erc20, Erc721, Erc1155",
|
||||
s
|
||||
)
|
||||
.into(),
|
||||
Position::NONE,
|
||||
))),
|
||||
}
|
||||
}
|
||||
@@ -68,8 +72,12 @@ fn string_to_listing_status(s: &str) -> Result<ListingStatus, Box<EvalAltResult>
|
||||
"Cancelled" => Ok(ListingStatus::Cancelled),
|
||||
"Expired" => Ok(ListingStatus::Expired),
|
||||
_ => Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Invalid listing status: '{}'. Expected one of: Active, Sold, Cancelled, Expired", s).into(),
|
||||
Position::NONE
|
||||
format!(
|
||||
"Invalid listing status: '{}'. Expected one of: Active, Sold, Cancelled, Expired",
|
||||
s
|
||||
)
|
||||
.into(),
|
||||
Position::NONE,
|
||||
))),
|
||||
}
|
||||
}
|
||||
@@ -89,8 +97,12 @@ fn string_to_listing_type(s: &str) -> Result<ListingType, Box<EvalAltResult>> {
|
||||
"Auction" => Ok(ListingType::Auction),
|
||||
"Exchange" => Ok(ListingType::Exchange),
|
||||
_ => Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Invalid listing type: '{}'. Expected one of: FixedPrice, Auction, Exchange", s).into(),
|
||||
Position::NONE
|
||||
format!(
|
||||
"Invalid listing type: '{}'. Expected one of: FixedPrice, Auction, Exchange",
|
||||
s
|
||||
)
|
||||
.into(),
|
||||
Position::NONE,
|
||||
))),
|
||||
}
|
||||
}
|
||||
@@ -112,8 +124,12 @@ fn string_to_bid_status(s: &str) -> Result<BidStatus, Box<EvalAltResult>> {
|
||||
"Rejected" => Ok(BidStatus::Rejected),
|
||||
"Cancelled" => Ok(BidStatus::Cancelled),
|
||||
_ => Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Invalid bid status: '{}'. Expected one of: Active, Accepted, Rejected, Cancelled", s).into(),
|
||||
Position::NONE
|
||||
format!(
|
||||
"Invalid bid status: '{}'. Expected one of: Active, Accepted, Rejected, Cancelled",
|
||||
s
|
||||
)
|
||||
.into(),
|
||||
Position::NONE,
|
||||
))),
|
||||
}
|
||||
}
|
||||
@@ -181,7 +197,10 @@ mod account_module {
|
||||
|
||||
// Setters using builder pattern with proper mutability handling
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn name(account: &mut RhaiAccount, name: String) -> Result<RhaiAccount, Box<EvalAltResult>> {
|
||||
pub fn name(
|
||||
account: &mut RhaiAccount,
|
||||
name: String,
|
||||
) -> Result<RhaiAccount, Box<EvalAltResult>> {
|
||||
let mut acc = mem::take(account);
|
||||
acc = acc.name(name);
|
||||
*account = acc;
|
||||
@@ -189,7 +208,10 @@ mod account_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn user_id(account: &mut RhaiAccount, user_id: INT) -> Result<RhaiAccount, Box<EvalAltResult>> {
|
||||
pub fn user_id(
|
||||
account: &mut RhaiAccount,
|
||||
user_id: INT,
|
||||
) -> Result<RhaiAccount, Box<EvalAltResult>> {
|
||||
let user_id = id_from_i64_to_u32(user_id)?;
|
||||
let mut acc = mem::take(account);
|
||||
acc = acc.user_id(user_id);
|
||||
@@ -198,7 +220,10 @@ mod account_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn description(account: &mut RhaiAccount, description: String) -> Result<RhaiAccount, Box<EvalAltResult>> {
|
||||
pub fn description(
|
||||
account: &mut RhaiAccount,
|
||||
description: String,
|
||||
) -> Result<RhaiAccount, Box<EvalAltResult>> {
|
||||
let mut acc = mem::take(account);
|
||||
acc = acc.description(description);
|
||||
*account = acc;
|
||||
@@ -206,7 +231,10 @@ mod account_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn ledger(account: &mut RhaiAccount, ledger: String) -> Result<RhaiAccount, Box<EvalAltResult>> {
|
||||
pub fn ledger(
|
||||
account: &mut RhaiAccount,
|
||||
ledger: String,
|
||||
) -> Result<RhaiAccount, Box<EvalAltResult>> {
|
||||
let mut acc = mem::take(account);
|
||||
acc = acc.ledger(ledger);
|
||||
*account = acc;
|
||||
@@ -214,7 +242,10 @@ mod account_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn address(account: &mut RhaiAccount, address: String) -> Result<RhaiAccount, Box<EvalAltResult>> {
|
||||
pub fn address(
|
||||
account: &mut RhaiAccount,
|
||||
address: String,
|
||||
) -> Result<RhaiAccount, Box<EvalAltResult>> {
|
||||
let mut acc = mem::take(account);
|
||||
acc = acc.address(address);
|
||||
*account = acc;
|
||||
@@ -222,7 +253,10 @@ mod account_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn pubkey(account: &mut RhaiAccount, pubkey: String) -> Result<RhaiAccount, Box<EvalAltResult>> {
|
||||
pub fn pubkey(
|
||||
account: &mut RhaiAccount,
|
||||
pubkey: String,
|
||||
) -> Result<RhaiAccount, Box<EvalAltResult>> {
|
||||
let mut acc = mem::take(account);
|
||||
acc = acc.pubkey(pubkey);
|
||||
*account = acc;
|
||||
@@ -230,7 +264,10 @@ mod account_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn add_asset(account: &mut RhaiAccount, asset_id: INT) -> Result<RhaiAccount, Box<EvalAltResult>> {
|
||||
pub fn add_asset(
|
||||
account: &mut RhaiAccount,
|
||||
asset_id: INT,
|
||||
) -> Result<RhaiAccount, Box<EvalAltResult>> {
|
||||
let asset_id = id_from_i64_to_u32(asset_id)?;
|
||||
let mut acc = mem::take(account);
|
||||
acc = acc.add_asset(asset_id);
|
||||
@@ -301,7 +338,10 @@ mod asset_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn description(asset: &mut RhaiAsset, description: String) -> Result<RhaiAsset, Box<EvalAltResult>> {
|
||||
pub fn description(
|
||||
asset: &mut RhaiAsset,
|
||||
description: String,
|
||||
) -> Result<RhaiAsset, Box<EvalAltResult>> {
|
||||
let mut ast = mem::take(asset);
|
||||
ast = ast.description(description);
|
||||
*asset = ast;
|
||||
@@ -317,7 +357,10 @@ mod asset_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn address(asset: &mut RhaiAsset, address: String) -> Result<RhaiAsset, Box<EvalAltResult>> {
|
||||
pub fn address(
|
||||
asset: &mut RhaiAsset,
|
||||
address: String,
|
||||
) -> Result<RhaiAsset, Box<EvalAltResult>> {
|
||||
let mut ast = mem::take(asset);
|
||||
ast = ast.address(address);
|
||||
*asset = ast;
|
||||
@@ -325,7 +368,10 @@ mod asset_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn asset_type(asset: &mut RhaiAsset, asset_type_str: String) -> Result<RhaiAsset, Box<EvalAltResult>> {
|
||||
pub fn asset_type(
|
||||
asset: &mut RhaiAsset,
|
||||
asset_type_str: String,
|
||||
) -> Result<RhaiAsset, Box<EvalAltResult>> {
|
||||
let asset_type_enum = string_to_asset_type(&asset_type_str)?;
|
||||
let mut ast = mem::take(asset);
|
||||
ast = ast.asset_type(asset_type_enum);
|
||||
@@ -338,7 +384,7 @@ mod asset_module {
|
||||
if decimals < 0 || decimals > 255 {
|
||||
return Err(Box::new(EvalAltResult::ErrorArithmetic(
|
||||
format!("Decimals value must be between 0 and 255, got {}", decimals).into(),
|
||||
Position::NONE
|
||||
Position::NONE,
|
||||
)));
|
||||
}
|
||||
let mut ast = mem::take(asset);
|
||||
@@ -441,7 +487,10 @@ mod listing_module {
|
||||
|
||||
// Setters using builder pattern with proper mutability handling
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn seller_id(listing: &mut RhaiListing, seller_id: INT) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
pub fn seller_id(
|
||||
listing: &mut RhaiListing,
|
||||
seller_id: INT,
|
||||
) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
let seller_id = id_from_i64_to_u32(seller_id)?;
|
||||
let mut lst = mem::take(listing);
|
||||
lst = lst.seller_id(seller_id);
|
||||
@@ -450,7 +499,10 @@ mod listing_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn asset_id(listing: &mut RhaiListing, asset_id: INT) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
pub fn asset_id(
|
||||
listing: &mut RhaiListing,
|
||||
asset_id: INT,
|
||||
) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
let asset_id = id_from_i64_to_u32(asset_id)?;
|
||||
let mut lst = mem::take(listing);
|
||||
lst = lst.asset_id(asset_id);
|
||||
@@ -467,7 +519,10 @@ mod listing_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn currency(listing: &mut RhaiListing, currency: String) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
pub fn currency(
|
||||
listing: &mut RhaiListing,
|
||||
currency: String,
|
||||
) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
let mut lst = mem::take(listing);
|
||||
lst = lst.currency(currency);
|
||||
*listing = lst;
|
||||
@@ -475,7 +530,10 @@ mod listing_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn listing_type(listing: &mut RhaiListing, listing_type_str: String) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
pub fn listing_type(
|
||||
listing: &mut RhaiListing,
|
||||
listing_type_str: String,
|
||||
) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
let listing_type_enum = string_to_listing_type(&listing_type_str)?;
|
||||
let mut lst = mem::take(listing);
|
||||
lst = lst.listing_type(listing_type_enum);
|
||||
@@ -484,7 +542,10 @@ mod listing_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn title(listing: &mut RhaiListing, title: String) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
pub fn title(
|
||||
listing: &mut RhaiListing,
|
||||
title: String,
|
||||
) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
let mut lst = mem::take(listing);
|
||||
lst = lst.title(title);
|
||||
*listing = lst;
|
||||
@@ -492,7 +553,10 @@ mod listing_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn description(listing: &mut RhaiListing, description: String) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
pub fn description(
|
||||
listing: &mut RhaiListing,
|
||||
description: String,
|
||||
) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
let mut lst = mem::take(listing);
|
||||
lst = lst.description(description);
|
||||
*listing = lst;
|
||||
@@ -500,7 +564,10 @@ mod listing_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn image_url(listing: &mut RhaiListing, image_url: String) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
pub fn image_url(
|
||||
listing: &mut RhaiListing,
|
||||
image_url: String,
|
||||
) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
let mut lst = mem::take(listing);
|
||||
lst = lst.image_url(Some(image_url));
|
||||
*listing = lst;
|
||||
@@ -508,15 +575,21 @@ mod listing_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn expires_at(listing: &mut RhaiListing, end_date_ts: INT) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
pub fn expires_at(
|
||||
listing: &mut RhaiListing,
|
||||
end_date_ts: INT,
|
||||
) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
use chrono::TimeZone;
|
||||
let end_date = chrono::Utc.timestamp_opt(end_date_ts, 0)
|
||||
let end_date = chrono::Utc
|
||||
.timestamp_opt(end_date_ts, 0)
|
||||
.single()
|
||||
.ok_or_else(|| Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Invalid timestamp: {}", end_date_ts).into(),
|
||||
Position::NONE
|
||||
)))?;
|
||||
|
||||
.ok_or_else(|| {
|
||||
Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Invalid timestamp: {}", end_date_ts).into(),
|
||||
Position::NONE,
|
||||
))
|
||||
})?;
|
||||
|
||||
let mut lst = mem::take(listing);
|
||||
lst = lst.expires_at(Some(end_date));
|
||||
*listing = lst;
|
||||
@@ -524,7 +597,10 @@ mod listing_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn add_tag(listing: &mut RhaiListing, tag: String) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
pub fn add_tag(
|
||||
listing: &mut RhaiListing,
|
||||
tag: String,
|
||||
) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
let mut lst = mem::take(listing);
|
||||
lst = lst.add_tag(tag);
|
||||
*listing = lst;
|
||||
@@ -532,44 +608,50 @@ mod listing_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn add_bid(listing: &mut RhaiListing, bid: RhaiBid) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
pub fn add_bid(
|
||||
listing: &mut RhaiListing,
|
||||
bid: RhaiBid,
|
||||
) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
let lst = mem::take(listing);
|
||||
match lst.clone().add_bid(bid) {
|
||||
Ok(updated_lst) => {
|
||||
*listing = updated_lst;
|
||||
Ok(listing.clone())
|
||||
},
|
||||
}
|
||||
Err(err) => {
|
||||
// Put back the original listing since the add_bid failed
|
||||
*listing = lst;
|
||||
Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Failed to add bid: {}", err).into(),
|
||||
Position::NONE
|
||||
Position::NONE,
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn complete_sale(listing: &mut RhaiListing, buyer_id: INT) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
pub fn complete_sale(
|
||||
listing: &mut RhaiListing,
|
||||
buyer_id: INT,
|
||||
) -> Result<RhaiListing, Box<EvalAltResult>> {
|
||||
let buyer_id = id_from_i64_to_u32(buyer_id)?;
|
||||
let lst = mem::take(listing);
|
||||
|
||||
|
||||
// First set the buyer_id and sale_price
|
||||
let lst_with_buyer = lst.clone().buyer_id(buyer_id);
|
||||
|
||||
|
||||
// Now complete the sale
|
||||
match lst_with_buyer.complete_sale() {
|
||||
Ok(updated_lst) => {
|
||||
*listing = updated_lst;
|
||||
Ok(listing.clone())
|
||||
},
|
||||
}
|
||||
Err(err) => {
|
||||
// Put back the original listing since the complete_sale failed
|
||||
*listing = lst;
|
||||
Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Failed to complete sale: {}", err).into(),
|
||||
Position::NONE
|
||||
Position::NONE,
|
||||
)))
|
||||
}
|
||||
}
|
||||
@@ -582,13 +664,13 @@ mod listing_module {
|
||||
Ok(updated_lst) => {
|
||||
*listing = updated_lst;
|
||||
Ok(listing.clone())
|
||||
},
|
||||
}
|
||||
Err(err) => {
|
||||
// Put back the original listing since the cancel failed
|
||||
*listing = lst;
|
||||
Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Failed to cancel listing: {}", err).into(),
|
||||
Position::NONE
|
||||
Position::NONE,
|
||||
)))
|
||||
}
|
||||
}
|
||||
@@ -647,7 +729,10 @@ mod bid_module {
|
||||
|
||||
// Setters using builder pattern with proper mutability handling
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn listing_id(bid: &mut RhaiBid, listing_id: String) -> Result<RhaiBid, Box<EvalAltResult>> {
|
||||
pub fn listing_id(
|
||||
bid: &mut RhaiBid,
|
||||
listing_id: String,
|
||||
) -> Result<RhaiBid, Box<EvalAltResult>> {
|
||||
let mut b = mem::take(bid);
|
||||
b = b.listing_id(listing_id);
|
||||
*bid = b;
|
||||
@@ -680,7 +765,10 @@ mod bid_module {
|
||||
}
|
||||
|
||||
#[rhai_fn(return_raw, global)]
|
||||
pub fn update_status(bid: &mut RhaiBid, status_str: String) -> Result<RhaiBid, Box<EvalAltResult>> {
|
||||
pub fn update_status(
|
||||
bid: &mut RhaiBid,
|
||||
status_str: String,
|
||||
) -> Result<RhaiBid, Box<EvalAltResult>> {
|
||||
let status = string_to_bid_status(&status_str)?;
|
||||
let mut b = mem::take(bid);
|
||||
b = b.status(status);
|
||||
@@ -706,13 +794,21 @@ pub fn register_finance_rhai_module(engine: &mut Engine, db: Arc<OurDB>) {
|
||||
engine.register_global_module(bid_module.into());
|
||||
|
||||
// --- Global Helper Functions (Enum conversions) ---
|
||||
engine.register_fn("str_to_asset_type", |s: ImmutableString| self::string_to_asset_type(s.as_str()));
|
||||
engine.register_fn("str_to_asset_type", |s: ImmutableString| {
|
||||
self::string_to_asset_type(s.as_str())
|
||||
});
|
||||
engine.register_fn("asset_type_to_str", self::asset_type_to_string);
|
||||
engine.register_fn("str_to_listing_status", |s: ImmutableString| self::string_to_listing_status(s.as_str()));
|
||||
engine.register_fn("str_to_listing_status", |s: ImmutableString| {
|
||||
self::string_to_listing_status(s.as_str())
|
||||
});
|
||||
engine.register_fn("listing_status_to_str", self::listing_status_to_string);
|
||||
engine.register_fn("str_to_listing_type", |s: ImmutableString| self::string_to_listing_type(s.as_str()));
|
||||
engine.register_fn("str_to_listing_type", |s: ImmutableString| {
|
||||
self::string_to_listing_type(s.as_str())
|
||||
});
|
||||
engine.register_fn("listing_type_to_str", self::listing_type_to_string);
|
||||
engine.register_fn("str_to_bid_status", |s: ImmutableString| self::string_to_bid_status(s.as_str()));
|
||||
engine.register_fn("str_to_bid_status", |s: ImmutableString| {
|
||||
self::string_to_bid_status(s.as_str())
|
||||
});
|
||||
engine.register_fn("bid_status_to_str", self::bid_status_to_string);
|
||||
|
||||
// --- Database interaction functions (registered in a separate db_module) ---
|
||||
@@ -720,68 +816,158 @@ pub fn register_finance_rhai_module(engine: &mut Engine, db: Arc<OurDB>) {
|
||||
|
||||
// Account DB functions
|
||||
let db_set_account = Arc::clone(&db);
|
||||
db_module.set_native_fn("set_account", move |account: Account| -> Result<INT, Box<EvalAltResult>> {
|
||||
let collection = db_set_account.collection::<Account>().map_err(|e|
|
||||
Box::new(EvalAltResult::ErrorRuntime(format!("Failed to get Account collection: {:?}", e).into(), Position::NONE)) )?;
|
||||
collection.set(&account)
|
||||
.map(|(id, _)| id as INT)
|
||||
.map_err(|e| Box::new(EvalAltResult::ErrorRuntime(format!("Failed to save Account: {:?}", e).into(), Position::NONE)))
|
||||
});
|
||||
db_module.set_native_fn(
|
||||
"set_account",
|
||||
move |account: Account| -> Result<INT, Box<EvalAltResult>> {
|
||||
let collection = db_set_account.collection::<Account>().map_err(|e| {
|
||||
Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Failed to get Account collection: {:?}", e).into(),
|
||||
Position::NONE,
|
||||
))
|
||||
})?;
|
||||
collection
|
||||
.set(&account)
|
||||
.map(|(id, _)| id as INT)
|
||||
.map_err(|e| {
|
||||
Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Failed to save Account: {:?}", e).into(),
|
||||
Position::NONE,
|
||||
))
|
||||
})
|
||||
},
|
||||
);
|
||||
|
||||
let db_get_account = Arc::clone(&db);
|
||||
db_module.set_native_fn("get_account_by_id", move |id_rhai: INT| -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
let id_u32 = id_from_i64_to_u32(id_rhai)?;
|
||||
let collection = db_get_account.collection::<Account>().map_err(|e|
|
||||
Box::new(EvalAltResult::ErrorRuntime(format!("Failed to get Account collection: {:?}", e).into(), Position::NONE)) )?;
|
||||
collection.get_by_id(id_u32)
|
||||
.map(|opt_account| opt_account.map(Dynamic::from).unwrap_or_else(|| Dynamic::UNIT))
|
||||
.map_err(|e| Box::new(EvalAltResult::ErrorRuntime(format!("Failed to get Account with ID {}: {:?}", id_rhai, e).into(), Position::NONE)))
|
||||
});
|
||||
db_module.set_native_fn(
|
||||
"get_account_by_id",
|
||||
move |id_rhai: INT| -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
let id_u32 = id_from_i64_to_u32(id_rhai)?;
|
||||
let collection = db_get_account.collection::<Account>().map_err(|e| {
|
||||
Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Failed to get Account collection: {:?}", e).into(),
|
||||
Position::NONE,
|
||||
))
|
||||
})?;
|
||||
collection
|
||||
.get_by_id(id_u32)
|
||||
.map(|opt_account| {
|
||||
opt_account
|
||||
.map(Dynamic::from)
|
||||
.unwrap_or_else(|| Dynamic::UNIT)
|
||||
})
|
||||
.map_err(|e| {
|
||||
Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Failed to get Account with ID {}: {:?}", id_rhai, e).into(),
|
||||
Position::NONE,
|
||||
))
|
||||
})
|
||||
},
|
||||
);
|
||||
|
||||
// Asset DB functions
|
||||
let db_set_asset = Arc::clone(&db);
|
||||
db_module.set_native_fn("set_asset", move |asset: Asset| -> Result<INT, Box<EvalAltResult>> {
|
||||
let collection = db_set_asset.collection::<Asset>().map_err(|e|
|
||||
Box::new(EvalAltResult::ErrorRuntime(format!("Failed to get Asset collection: {:?}", e).into(), Position::NONE)) )?;
|
||||
collection.set(&asset)
|
||||
.map(|(id, _)| id as INT)
|
||||
.map_err(|e| Box::new(EvalAltResult::ErrorRuntime(format!("Failed to save Asset: {:?}", e).into(), Position::NONE)))
|
||||
});
|
||||
db_module.set_native_fn(
|
||||
"set_asset",
|
||||
move |asset: Asset| -> Result<INT, Box<EvalAltResult>> {
|
||||
let collection = db_set_asset.collection::<Asset>().map_err(|e| {
|
||||
Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Failed to get Asset collection: {:?}", e).into(),
|
||||
Position::NONE,
|
||||
))
|
||||
})?;
|
||||
collection
|
||||
.set(&asset)
|
||||
.map(|(id, _)| id as INT)
|
||||
.map_err(|e| {
|
||||
Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Failed to save Asset: {:?}", e).into(),
|
||||
Position::NONE,
|
||||
))
|
||||
})
|
||||
},
|
||||
);
|
||||
|
||||
let db_get_asset = Arc::clone(&db);
|
||||
db_module.set_native_fn("get_asset_by_id", move |id_rhai: INT| -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
let id_u32 = id_from_i64_to_u32(id_rhai)?;
|
||||
let collection = db_get_asset.collection::<Asset>().map_err(|e|
|
||||
Box::new(EvalAltResult::ErrorRuntime(format!("Failed to get Asset collection: {:?}", e).into(), Position::NONE)) )?;
|
||||
collection.get_by_id(id_u32)
|
||||
.map(|opt_asset| opt_asset.map(Dynamic::from).unwrap_or_else(|| Dynamic::UNIT))
|
||||
.map_err(|e| Box::new(EvalAltResult::ErrorRuntime(format!("Failed to get Asset with ID {}: {:?}", id_rhai, e).into(), Position::NONE)))
|
||||
});
|
||||
db_module.set_native_fn(
|
||||
"get_asset_by_id",
|
||||
move |id_rhai: INT| -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
let id_u32 = id_from_i64_to_u32(id_rhai)?;
|
||||
let collection = db_get_asset.collection::<Asset>().map_err(|e| {
|
||||
Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Failed to get Asset collection: {:?}", e).into(),
|
||||
Position::NONE,
|
||||
))
|
||||
})?;
|
||||
collection
|
||||
.get_by_id(id_u32)
|
||||
.map(|opt_asset| {
|
||||
opt_asset
|
||||
.map(Dynamic::from)
|
||||
.unwrap_or_else(|| Dynamic::UNIT)
|
||||
})
|
||||
.map_err(|e| {
|
||||
Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Failed to get Asset with ID {}: {:?}", id_rhai, e).into(),
|
||||
Position::NONE,
|
||||
))
|
||||
})
|
||||
},
|
||||
);
|
||||
|
||||
// Listing DB functions
|
||||
let db_set_listing = Arc::clone(&db);
|
||||
db_module.set_native_fn("set_listing", move |listing: Listing| -> Result<INT, Box<EvalAltResult>> {
|
||||
let collection = db_set_listing.collection::<Listing>().map_err(|e|
|
||||
Box::new(EvalAltResult::ErrorRuntime(format!("Failed to get Listing collection: {:?}", e).into(), Position::NONE)) )?;
|
||||
collection.set(&listing)
|
||||
.map(|(id, _)| id as INT)
|
||||
.map_err(|e| Box::new(EvalAltResult::ErrorRuntime(format!("Failed to save Listing: {:?}", e).into(), Position::NONE)))
|
||||
});
|
||||
db_module.set_native_fn(
|
||||
"set_listing",
|
||||
move |listing: Listing| -> Result<INT, Box<EvalAltResult>> {
|
||||
let collection = db_set_listing.collection::<Listing>().map_err(|e| {
|
||||
Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Failed to get Listing collection: {:?}", e).into(),
|
||||
Position::NONE,
|
||||
))
|
||||
})?;
|
||||
collection
|
||||
.set(&listing)
|
||||
.map(|(id, _)| id as INT)
|
||||
.map_err(|e| {
|
||||
Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Failed to save Listing: {:?}", e).into(),
|
||||
Position::NONE,
|
||||
))
|
||||
})
|
||||
},
|
||||
);
|
||||
|
||||
let db_get_listing = Arc::clone(&db);
|
||||
db_module.set_native_fn("get_listing_by_id", move |id_rhai: INT| -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
let id_u32 = id_from_i64_to_u32(id_rhai)?;
|
||||
let collection = db_get_listing.collection::<Listing>().map_err(|e|
|
||||
Box::new(EvalAltResult::ErrorRuntime(format!("Failed to get Listing collection: {:?}", e).into(), Position::NONE)) )?;
|
||||
collection.get_by_id(id_u32)
|
||||
.map(|opt_listing| opt_listing.map(Dynamic::from).unwrap_or_else(|| Dynamic::UNIT))
|
||||
.map_err(|e| Box::new(EvalAltResult::ErrorRuntime(format!("Failed to get Listing with ID {}: {:?}", id_rhai, e).into(), Position::NONE)))
|
||||
});
|
||||
db_module.set_native_fn(
|
||||
"get_listing_by_id",
|
||||
move |id_rhai: INT| -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
let id_u32 = id_from_i64_to_u32(id_rhai)?;
|
||||
let collection = db_get_listing.collection::<Listing>().map_err(|e| {
|
||||
Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Failed to get Listing collection: {:?}", e).into(),
|
||||
Position::NONE,
|
||||
))
|
||||
})?;
|
||||
collection
|
||||
.get_by_id(id_u32)
|
||||
.map(|opt_listing| {
|
||||
opt_listing
|
||||
.map(Dynamic::from)
|
||||
.unwrap_or_else(|| Dynamic::UNIT)
|
||||
})
|
||||
.map_err(|e| {
|
||||
Box::new(EvalAltResult::ErrorRuntime(
|
||||
format!("Failed to get Listing with ID {}: {:?}", id_rhai, e).into(),
|
||||
Position::NONE,
|
||||
))
|
||||
})
|
||||
},
|
||||
);
|
||||
|
||||
engine.register_global_module(db_module.into());
|
||||
|
||||
// Global timestamp function for scripts to get current time
|
||||
engine.register_fn("timestamp", || Utc::now().timestamp());
|
||||
|
||||
|
||||
println!("Successfully registered finance Rhai module.");
|
||||
}
|
||||
|
Reference in New Issue
Block a user