Skip to content

Commit 7050c89

Browse files
committed
feat: used new macro to match input arguments
1 parent ea3ae26 commit 7050c89

File tree

11 files changed

+1775
-5942
lines changed

11 files changed

+1775
-5942
lines changed

taurus/src/context/argument.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
use std::convert::Infallible;
2+
use tucana::shared::{ListValue, Struct, Value};
3+
use tucana::shared::value::Kind;
4+
use crate::context::signal::Signal;
5+
use crate::error::RuntimeError;
6+
17
#[derive(Clone, Debug)]
28
pub enum Argument {
39
// Eval => Evaluated Value
@@ -12,4 +18,75 @@ pub enum Argument {
1218
pub enum ParameterNode {
1319
Eager,
1420
Lazy
21+
}
22+
23+
pub trait TryFromArgument: Sized {
24+
fn try_from_argument(a: &Argument) -> Result<Self, Signal>;
25+
}
26+
27+
fn type_err(msg: &str) -> Signal {
28+
Signal::Failure(RuntimeError::simple("InvalidArgumentRuntimeError", msg.to_string()))
29+
}
30+
31+
impl TryFromArgument for Value {
32+
fn try_from_argument(a: &Argument) -> Result<Self, Signal> {
33+
match a {
34+
Argument::Eval(v) => Ok(v.clone()),
35+
_ => Err(type_err("Expected evaluated value but got lazy thunk")),
36+
}
37+
}
38+
}
39+
40+
impl TryFromArgument for f64 {
41+
fn try_from_argument(a: &Argument) -> Result<Self, Signal> {
42+
match a {
43+
Argument::Eval(Value { kind: Some(Kind::NumberValue(n)) }) => Ok(*n),
44+
_ => Err(type_err("Expected number")),
45+
}
46+
}
47+
}
48+
49+
impl TryFromArgument for bool {
50+
fn try_from_argument(a: &Argument) -> Result<Self, Signal> {
51+
match a {
52+
Argument::Eval(Value { kind: Some(Kind::BoolValue(b)) }) => Ok(*b),
53+
_ => Err(type_err("Expected boolean")),
54+
}
55+
}
56+
}
57+
58+
impl TryFromArgument for String {
59+
fn try_from_argument(a: &Argument) -> Result<Self, Signal> {
60+
match a {
61+
Argument::Eval(Value { kind: Some(Kind::StringValue(s)) }) => Ok(s.clone()),
62+
_ => Err(type_err("Expected string")),
63+
}
64+
}
65+
}
66+
67+
impl TryFromArgument for Struct {
68+
fn try_from_argument(a: &Argument) -> Result<Self, Signal> {
69+
match a {
70+
Argument::Eval(Value { kind: Some(Kind::StructValue(s)) }) => Ok(s.clone()),
71+
_ => Err(type_err("Expected struct")),
72+
}
73+
}
74+
}
75+
76+
impl TryFromArgument for ListValue {
77+
fn try_from_argument(a: &Argument) -> Result<Self, Signal> {
78+
match a {
79+
Argument::Eval(Value { kind: Some(Kind::ListValue(list)) }) => Ok(list.clone()),
80+
_ => Err(Signal::Failure(RuntimeError::simple_str(
81+
"InvalidArgumentRuntimeError",
82+
"Expected array (ListValue)",
83+
))),
84+
}
85+
}
86+
}
87+
88+
impl From<Infallible> for RuntimeError {
89+
fn from(never: Infallible) -> Self {
90+
match never {}
91+
}
1592
}

taurus/src/context/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod signal;
33
pub mod argument;
44
pub mod executor;
55
pub mod registry;
6+
pub mod macros;
67

78
use crate::error::RuntimeError;
89
use std::{

taurus/src/context/registry.rs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ use std::collections::HashMap;
77
/// - For eager params, the executor will already convert them to Argument::Eval(Value).
88
/// - For lazy params, the executor will pass Argument::Thunk(node_id).
99
/// - If a handler wants to execute a lazy arg, it calls run(node_id).
10-
pub type HandlerFn = fn(
11-
args: &[Argument],
12-
ctx: &mut Context,
13-
run: &mut dyn FnMut(i64) -> Signal,
14-
) -> Signal;
10+
pub type HandlerFn =
11+
fn(args: &[Argument], ctx: &mut Context, run: &mut dyn FnMut(i64) -> Signal) -> Signal;
1512

1613
pub struct HandlerFunctionEntry {
1714
pub handler: HandlerFn,
@@ -29,6 +26,47 @@ impl Default for FunctionStore {
2926
}
3027
}
3128

29+
pub trait IntoFunctionEntry {
30+
fn into_function_entry(self, param: Vec<ParameterNode>) -> HandlerFunctionEntry;
31+
fn eager(self, param_amount: i8) -> HandlerFunctionEntry;
32+
fn lazy(self, param_amount: i8) -> HandlerFunctionEntry;
33+
}
34+
35+
impl IntoFunctionEntry for HandlerFn {
36+
fn into_function_entry(self, param: Vec<ParameterNode>) -> HandlerFunctionEntry {
37+
HandlerFunctionEntry {
38+
handler: self,
39+
param_modes: param,
40+
}
41+
}
42+
43+
fn eager(self, param_amount: i8) -> HandlerFunctionEntry {
44+
let mut params = vec![];
45+
46+
for _ in 0..param_amount {
47+
params.push(ParameterNode::Eager)
48+
}
49+
50+
HandlerFunctionEntry {
51+
handler: self,
52+
param_modes: params,
53+
}
54+
}
55+
56+
fn lazy(self, param_amount: i8) -> HandlerFunctionEntry {
57+
let mut params = vec![];
58+
59+
for _ in 0..param_amount {
60+
params.push(ParameterNode::Lazy)
61+
}
62+
63+
HandlerFunctionEntry {
64+
handler: self,
65+
param_modes: params,
66+
}
67+
}
68+
}
69+
3270
impl FunctionStore {
3371
/// Create a new, empty store.
3472
pub fn new() -> Self {
@@ -48,4 +86,4 @@ impl FunctionStore {
4886
self.functions.insert(id.to_string(), func);
4987
}
5088
}
51-
}
89+
}

0 commit comments

Comments
 (0)