Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions taurus/src/context/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ pub trait TryFromArgument: Sized {
fn try_from_argument(a: &Argument) -> Result<Self, Signal>;
}

fn type_err(msg: &str) -> Signal {
fn type_err(msg: &str, a: &Argument) -> Signal {
Signal::Failure(RuntimeError::simple(
"InvalidArgumentRuntimeError",
msg.to_string(),
format!("{} but it was the arugment: {:?}", msg, a),
))
}

impl TryFromArgument for Value {
fn try_from_argument(a: &Argument) -> Result<Self, Signal> {
match a {
Argument::Eval(v) => Ok(v.clone()),
_ => Err(type_err("Expected evaluated value but got lazy thunk")),
_ => Err(type_err("Expected evaluated value but got lazy thunk", a)),
}
}
}
Expand All @@ -46,7 +46,7 @@ impl TryFromArgument for f64 {
Argument::Eval(Value {
kind: Some(Kind::NumberValue(n)),
}) => Ok(*n),
_ => Err(type_err("Expected number")),
_ => Err(type_err("Expected number", a)),
}
}
}
Expand All @@ -57,7 +57,7 @@ impl TryFromArgument for bool {
Argument::Eval(Value {
kind: Some(Kind::BoolValue(b)),
}) => Ok(*b),
_ => Err(type_err("Expected boolean")),
_ => Err(type_err("Expected boolean", a)),
}
}
}
Expand All @@ -68,7 +68,7 @@ impl TryFromArgument for String {
Argument::Eval(Value {
kind: Some(Kind::StringValue(s)),
}) => Ok(s.clone()),
_ => Err(type_err("Expected string")),
_ => Err(type_err("Expected string", a)),
}
}
}
Expand All @@ -79,7 +79,7 @@ impl TryFromArgument for Struct {
Argument::Eval(Value {
kind: Some(Kind::StructValue(s)),
}) => Ok(s.clone()),
_ => Err(type_err("Expected struct")),
_ => Err(type_err("Expected struct", a)),
}
}
}
Expand All @@ -90,9 +90,9 @@ impl TryFromArgument for ListValue {
Argument::Eval(Value {
kind: Some(Kind::ListValue(list)),
}) => Ok(list.clone()),
_ => Err(Signal::Failure(RuntimeError::simple_str(
_ => Err(Signal::Failure(RuntimeError::simple(
"InvalidArgumentRuntimeError",
"Expected array (ListValue)",
format!("Expected array (ListValue) but it was: {:?}", a),
))),
}
}
Expand Down
63 changes: 35 additions & 28 deletions taurus/src/context/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,44 @@ use crate::context::context::{Context, ContextResult};
use crate::context::registry::FunctionStore;
use crate::context::signal::Signal;
use crate::error::RuntimeError;
use std::cell::RefCell;
use std::collections::HashMap;
use tucana::shared::NodeFunction;

pub struct Executor<'a> {
functions: &'a FunctionStore,
nodes: HashMap<i64, NodeFunction>,
context: RefCell<Context>,
}

impl<'a> Executor<'a> {
pub fn new(
functions: &'a FunctionStore,
nodes: HashMap<i64, NodeFunction>,
context: Context,
) -> Self {
Executor {
functions,
nodes,
context: RefCell::new(context),
}
pub fn new(functions: &'a FunctionStore, nodes: HashMap<i64, NodeFunction>) -> Self {
Executor { functions, nodes }
}

pub fn execute(&self, starting_node_id: i64) -> Signal {
pub fn execute(&self, starting_node_id: i64, ctx: &mut Context) -> Signal {
let mut current_node_id = starting_node_id;

loop {
let node = match self.nodes.get(&current_node_id) {
None => {
return Signal::Failure(RuntimeError::simple_str(
return Signal::Failure(RuntimeError::simple(
"NodeNotFound",
"The node with the id was not found",
format!(
"The node with the database id: {} was not found",
current_node_id
),
));
}
Some(n) => n.clone(),
};

let entry = match self.functions.get(node.runtime_function_id.as_str()) {
None => {
return Signal::Failure(RuntimeError::simple_str(
return Signal::Failure(RuntimeError::simple(
"FunctionNotFound",
"The function was not found",
format!(
"The function {} (database id: {}) was not found",
node.runtime_function_id, node.database_id
),
));
}
Some(f) => f,
Expand All @@ -57,7 +53,7 @@ impl<'a> Executor<'a> {
None => {
return Signal::Failure(RuntimeError::simple_str(
"NodeValueNotFound",
"Missing parameter value",
"Missing parameter value: {}",
));
}
};
Expand All @@ -76,7 +72,6 @@ impl<'a> Executor<'a> {
args.push(Argument::Eval(val.clone()))
}
tucana::shared::node_value::Value::ReferenceValue(reference) => {
let mut ctx = self.context.borrow_mut();
let value = ctx.get(reference.node_id);
match value {
ContextResult::Error(runtime_error) => {
Expand Down Expand Up @@ -108,26 +103,38 @@ impl<'a> Executor<'a> {
if matches!(mode, ParameterNode::Eager)
&& let Argument::Thunk(id) = *a
{
match self.execute(id) {
Signal::Success(v) => *a = Argument::Eval(v),
s @ (Signal::Failure(_)
| Signal::Return(_)
| Signal::Respond(_)
| Signal::Stop) => return s,
match self.execute(id, ctx) {
Signal::Success(v) => {
log::debug!(
"Successfully executed node with database id {}, resulted in value: {:?}",
id,
a
);
*a = Argument::Eval(v)
}
Signal::Failure(err) => {
log::error!("Failed to execute node with database id: {}", id);
return Signal::Failure(err);
}
s @ (Signal::Return(_) | Signal::Respond(_) | Signal::Stop) => return s,
}
}
}

let mut run = |node_id: i64| self.execute(node_id);
let mut ctx = self.context.borrow_mut();
let result = (entry.handler)(&args, &mut ctx, &mut run);
let mut run = |node_id: i64, ctx: &mut Context| self.execute(node_id, ctx);
let result = (entry.handler)(&args, ctx, &mut run);

match result {
Signal::Success(value) => {
if let Some(next_node_id) = node.next_node_id {
current_node_id = next_node_id;
continue;
} else {
log::debug!(
"Successfully executed node with database id {}, resulted in value: {:?}",
current_node_id,
value
);
return Signal::Success(value);
}
}
Expand Down
10 changes: 9 additions & 1 deletion taurus/src/context/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ macro_rules! args {
$ty as $crate::context::argument::TryFromArgument
>::try_from_argument(& $args_ident[__i]) {
Ok(v) => v,
Err(sig) => return sig,
Err(sig) => {
log::debug!(
"Failed to parse argument '{}' (index {}, type {})",
stringify!($name),
__i,
::core::any::type_name::<$ty>(),
);
return sig;
}
};
__i += 1;
)+
Expand Down
7 changes: 5 additions & 2 deletions taurus/src/context/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ use std::collections::HashMap;
/// - For eager params, the executor will already convert them to Argument::Eval(Value).
/// - For lazy params, the executor will pass Argument::Thunk(node_id).
/// - If a handler wants to execute a lazy arg, it calls run(node_id).
pub type HandlerFn =
fn(args: &[Argument], ctx: &mut Context, run: &mut dyn FnMut(i64) -> Signal) -> Signal;
pub type HandlerFn = fn(
args: &[Argument],
ctx: &mut Context,
run: &mut dyn FnMut(i64, &mut Context) -> Signal,
) -> Signal;

pub struct HandlerFunctionEntry {
pub handler: HandlerFn,
Expand Down
Loading