use serde::Serialize;
use tari_bor::to_value;
use tari_template_abi::{EngineOp, call_engine, rust::prelude::*};
use tari_template_lib_types::{ComponentAddress, LogLevel, OwnerRule, access_rules::ComponentAccessRules};
use crate::{
args::{ComponentAction, ComponentInvokeArg, ComponentRef, CreateComponentArg, EmitLogArg, InvokeResult},
component::ComponentManager,
models::ComponentAddressAllocation,
};
pub fn engine() -> TariEngine {
TariEngine::new()
}
#[derive(Debug, Default)]
pub struct TariEngine {}
impl TariEngine {
fn new() -> Self {
Self {}
}
pub fn create_component<T: Serialize>(
&self,
initial_state: T,
owner_rule: OwnerRule,
access_rules: ComponentAccessRules,
address_allocation: Option<ComponentAddressAllocation>,
) -> ComponentAddress {
let encoded_state = to_value(&initial_state).unwrap();
let result = call_engine::<_, InvokeResult>(EngineOp::ComponentInvoke, &ComponentInvokeArg {
component_ref: ComponentRef::Component,
action: ComponentAction::Create,
args: invoke_args![CreateComponentArg {
encoded_state,
owner_rule,
access_rules,
address_allocation
}],
});
result.decode().expect("failed to decode component address")
}
pub fn emit_log<T: Into<String>>(&self, level: LogLevel, msg: T) {
call_engine::<_, ()>(EngineOp::EmitLog, &EmitLogArg {
level,
message: msg.into(),
});
}
pub fn component_manager(&self, component_address: ComponentAddress) -> ComponentManager {
ComponentManager::new(component_address)
}
}