Lines
88.82 %
Functions
53.85 %
Branches
100 %
//! Account-domain natives. Wraps `server::command::{CreateAccount, ListAccounts,
//! ListAccountsForManage, GetAccountForManage, SetAccountTag, GetAccount,
//! GetAccountCommodities, GetBalance, GetBalances}` as nomiscript-callable host fns.
use scripting::runtime::{alloc_commodity_ref, alloc_ratio_ref, alloc_string_ref, read_string_arg};
use server::command::account::{ListAccounts, ListAccountsForManage};
use wasmtime::{ArrayRef, Caller, Linker, Rooted, StructRef};
use crate::session::SessionData;
mod balance;
mod create;
mod list;
#[cfg(test)]
mod tests;
pub const REGISTERED_COMMANDS: &[&str] = &[
"create-account",
"list-accounts",
"list-accounts-for-manage",
"get-account-for-manage",
"set-account-tag",
"get-account",
"get-account-commodities",
"get-balance",
"get-balances",
];
pub fn register(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
register_readonly(linker)?;
register_mutators(linker)?;
Ok(())
}
pub fn register_readonly(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
linker.func_wrap_async(
"nomi",
"account_list_accounts",
|mut caller: Caller<'_, SessionData>,
()|
-> Box<
dyn std::future::Future<Output = wasmtime::Result<Option<Rooted<StructRef>>>> + Send,
> {
Box::new(async move {
let user_id = caller.data().ctx().user_id;
let result = ListAccounts::new().user_id(user_id).run().await;
let entries = list::list_account_entries("list-accounts", result)?;
list::alloc_account_chain(&mut caller, entries).await
})
},
)?;
"account_get_account",
(key_arg,): (Option<Rooted<ArrayRef>>,)|
let key = read_string_arg(&mut caller, key_arg)?;
list::run_get_account(&mut caller, user_id, key).await
"account_get_balance",
(id_arg,): (Option<Rooted<ArrayRef>>,)|
let id = read_string_arg(&mut caller, id_arg)?;
let (numer, denom) = balance::run_get_balance_single(user_id, id).await?;
Ok(Some(alloc_ratio_ref(&mut caller, numer, denom)?))
"account_get_balances",
let lines = balance::run_get_balances(user_id, id).await?;
balance::alloc_string_pair_chain(&mut caller, lines).await
"account_get_account_commodities",
list::run_get_account_commodities(&mut caller, user_id, id).await
"account_list_accounts_for_manage",
let result = ListAccountsForManage::new().user_id(user_id).run().await;
let entries = list::list_account_entries("list-accounts-for-manage", result)?;
"account_get_account_for_manage",
list::run_get_account_for_manage(&mut caller, user_id, id).await
"account_account_count",
|caller: Caller<'_, SessionData>,
-> Box<dyn std::future::Future<Output = i32> + Send> {
balance::count_accounts(user_id).await
"account_account_balance",
let (numer, denom, commodity_id) = balance::resolve_balance(user_id, id).await?;
let ref_ = alloc_commodity_ref(&mut caller, numer, denom, commodity_id).await?;
Ok(Some(ref_))
pub fn register_mutators(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
"account_set_account_tag",
(id_arg, name_arg, value_arg): super::StringArgTriple|
-> Box<dyn std::future::Future<Output = wasmtime::Result<i32>> + Send> {
let name = read_string_arg(&mut caller, name_arg)?;
let value = read_string_arg(&mut caller, value_arg)?;
create::run_set_account_tag(user_id, id, name, value).await
"account_create_account",
(name_arg, parent_arg): (Option<Rooted<ArrayRef>>, Option<Rooted<ArrayRef>>)|
dyn std::future::Future<Output = wasmtime::Result<Option<Rooted<ArrayRef>>>> + Send,
let parent = read_string_arg(&mut caller, parent_arg)?;
let id = create::run_create_account(user_id, name, parent).await?;
Ok(Some(alloc_string_ref(&mut caller, id.as_bytes())?))
pub(super) fn quote_string(s: &str) -> String {
let mut q = String::with_capacity(s.len() + 2);
q.push('"');
for ch in s.chars() {
match ch {
'"' => q.push_str("\\\""),
'\\' => q.push_str("\\\\"),
other => q.push(other),
q