Skip to content

Commit e77e946

Browse files
committed
feat: handled all possible signal returns
1 parent 3c71b22 commit e77e946

File tree

13 files changed

+79
-79
lines changed

13 files changed

+79
-79
lines changed

src/context/context.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
pub struct Context {
2-
3-
}
1+
pub struct Context {}

src/context/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
pub mod signal;
21
pub mod context;
2+
pub mod signal;
33

44
use crate::error::RuntimeError;
55
use std::{
@@ -26,10 +26,7 @@ pub struct ContextEntry {
2626

2727
impl ContextEntry {
2828
pub fn new(result: NodeResult, parameter: Vec<Value>) -> Self {
29-
ContextEntry {
30-
result: result,
31-
parameter: parameter,
32-
}
29+
ContextEntry { result, parameter }
3330
}
3431
}
3532

@@ -53,6 +50,12 @@ pub struct Context {
5350
context_history: VecDeque<(i32, i32)>,
5451
}
5552

53+
impl Default for Context {
54+
fn default() -> Self {
55+
Self::new()
56+
}
57+
}
58+
5659
impl Context {
5760
/// Create a new, empty context.
5861
pub fn new() -> Self {

src/context/signal.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use tucana::shared::Value;
21
use crate::error::RuntimeError;
2+
use tucana::shared::Value;
33

44
#[derive(Debug)]
55
pub enum Signal {
@@ -15,12 +15,12 @@ pub enum Signal {
1515
// - will break the current context and return the value to the upper node
1616
Return(Value),
1717
// Will be signaled if the `respond` function has been executed
18-
// - will stop the execution of the flow completly
18+
// - will stop the execution of the flow completly
1919
// - will return the value to the adapter
2020
Respond(Value),
2121
// Will be signaled if the `stop` function has been executed
22-
// - will stop the execution of the flow completly
23-
Stop
22+
// - will stop the execution of the flow completly
23+
Stop,
2424
}
2525

2626
impl PartialEq for Signal {

src/executor/mod.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ pub struct Executor<'a> {
1414
type HandleNodeParameterFn = fn(&mut Executor, node_parameter: &NodeParameter) -> Signal;
1515

1616
impl<'a> Executor<'a> {
17-
18-
pub fn new(functions: &'a FunctionStore, nodes: HashMap<i64, NodeFunction>, context: Context) -> Self {
17+
pub fn new(
18+
functions: &'a FunctionStore,
19+
nodes: HashMap<i64, NodeFunction>,
20+
context: Context,
21+
) -> Self {
1922
Executor {
2023
functions,
2124
nodes,
22-
context
25+
context,
2326
}
2427
}
2528

26-
2729
pub fn execute(&mut self, starting_node_id: i64) -> Signal {
2830
let mut current_node_id = starting_node_id;
2931

@@ -47,7 +49,7 @@ impl<'a> Executor<'a> {
4749
}
4850
Signal::Failure(error) => return Signal::Failure(error),
4951
Signal::Stop => return Signal::Stop,
50-
Signal::Respond(value) => return Signal::Respond(value)
52+
Signal::Respond(value) => return Signal::Respond(value),
5153
}
5254
}
5355

@@ -91,16 +93,22 @@ impl<'a> Executor<'a> {
9193

9294
let value = match &node_value.value {
9395
Some(v) => v,
94-
None => return Signal::Failure(RuntimeError::simple_str("NodeValueNotFound", "An error occurred while executing a flow!"))
96+
None => {
97+
return Signal::Failure(RuntimeError::simple_str(
98+
"NodeValueNotFound",
99+
"An error occurred while executing a flow!",
100+
));
101+
}
95102
};
96103

97104
match value {
98-
tucana::shared::node_value::Value::LiteralValue(value) => return Signal::Success(value.clone()),
99-
tucana::shared::node_value::Value::ReferenceValue(_reference_value) => todo!("implement reference values!"),
100-
tucana::shared::node_value::Value::NodeFunctionId(id) => {
101-
return Executor::execute(self, *id)
102-
},
105+
tucana::shared::node_value::Value::LiteralValue(value) => {
106+
Signal::Success(value.clone())
107+
}
108+
tucana::shared::node_value::Value::ReferenceValue(_reference_value) => {
109+
todo!("implement reference values!")
110+
}
111+
tucana::shared::node_value::Value::NodeFunctionId(id) => Executor::execute(self, *id),
103112
}
104113
}
105114
}
106-

src/implementation/array.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn at(values: &[Value], _ctx: &mut Context) -> Signal {
5454
));
5555
};
5656

57-
let item = array.values[index.clone() as usize].clone();
57+
let item = array.values[*index as usize].clone();
5858

5959
Signal::Success(item)
6060
}
@@ -709,7 +709,7 @@ fn min(values: &[Value], _ctx: &mut Context) -> Signal {
709709

710710
match min {
711711
Some(min) => Signal::Success(Value {
712-
kind: Some(Kind::NumberValue(min.clone())),
712+
kind: Some(Kind::NumberValue(*min)),
713713
}),
714714
None => Signal::Failure(RuntimeError::simple(
715715
"ArrayEmptyRuntimeError",
@@ -748,7 +748,7 @@ fn max(values: &[Value], _ctx: &mut Context) -> Signal {
748748

749749
match max {
750750
Some(max) => Signal::Success(Value {
751-
kind: Some(Kind::NumberValue(max.clone())),
751+
kind: Some(Kind::NumberValue(*max)),
752752
}),
753753
None => Signal::Failure(RuntimeError::simple(
754754
"ArrayEmptyRuntimeError",
@@ -817,7 +817,7 @@ fn join(values: &[Value], _ctx: &mut Context) -> Signal {
817817
}
818818
});
819819

820-
let joined = collector.join(&separator);
820+
let joined = collector.join(separator);
821821

822822
Signal::Success(Value {
823823
kind: Some(Kind::StringValue(joined)),

src/implementation/boolean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn as_number(values: &[Value], _ctx: &mut Context) -> Signal {
2727
};
2828

2929
Signal::Success(Value {
30-
kind: Some(Kind::NumberValue(value.clone() as i64 as f64)),
30+
kind: Some(Kind::NumberValue(*value as i64 as f64)),
3131
})
3232
}
3333

src/implementation/control.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use tucana::shared::Value;
22

3-
use crate::{context::Context, error::RuntimeError, registry::HandlerFn};
43
use crate::context::signal::Signal;
4+
use crate::{context::Context, error::RuntimeError, registry::HandlerFn};
55

66
pub fn collect_control_functions() -> Vec<(&'static str, HandlerFn)> {
77
vec![

src/implementation/http.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ fn create_response(values: &[Value], _ctx: &mut Context) -> Signal {
152152
fields.insert(
153153
"status_code".to_string(),
154154
Value {
155-
kind: Some(Kind::NumberValue(http_status_code.clone())),
155+
kind: Some(Kind::NumberValue(*http_status_code)),
156156
},
157157
);
158158

@@ -168,4 +168,3 @@ fn create_response(values: &[Value], _ctx: &mut Context) -> Signal {
168168
kind: Some(Kind::StructValue(Struct { fields })),
169169
})
170170
}
171-

src/implementation/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use crate::registry::HandlerFn;
33
mod array;
44
mod boolean;
55
mod control;
6+
mod http;
67
mod number;
78
mod object;
89
mod text;
9-
mod http;
1010

1111
pub fn collect() -> Vec<(&'static str, HandlerFn)> {
1212
let mut result = vec![];

src/implementation/number.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ fn exponential(values: &[Value], _ctx: &mut Context) -> Signal {
322322
};
323323

324324
Signal::Success(Value {
325-
kind: Some(Kind::NumberValue(base.powf(exponent.clone()))),
325+
kind: Some(Kind::NumberValue(base.powf(*exponent))),
326326
})
327327
}
328328

@@ -363,7 +363,7 @@ fn round_up(values: &[Value], _ctx: &mut Context) -> Signal {
363363
));
364364
};
365365

366-
let factor = 10_f64.powi(decimal_places.clone() as i32);
366+
let factor = 10_f64.powi(*decimal_places as i32);
367367

368368
Signal::Success(Value {
369369
kind: Some(Kind::NumberValue((value * factor).ceil() / factor)),
@@ -389,7 +389,7 @@ fn round_down(values: &[Value], _ctx: &mut Context) -> Signal {
389389
));
390390
};
391391

392-
let factor = 10_f64.powi(decimal_places.clone() as i32);
392+
let factor = 10_f64.powi(*decimal_places as i32);
393393

394394
Signal::Success(Value {
395395
kind: Some(Kind::NumberValue((value * factor).floor() / factor)),
@@ -415,7 +415,7 @@ fn round(values: &[Value], _ctx: &mut Context) -> Signal {
415415
));
416416
};
417417

418-
let factor = 10_f64.powi(decimal_places.clone() as i32);
418+
let factor = 10_f64.powi(*decimal_places as i32);
419419

420420
Signal::Success(Value {
421421
kind: Some(Kind::NumberValue((value * factor).round() / factor)),
@@ -460,7 +460,7 @@ fn root(values: &[Value], _ctx: &mut Context) -> Signal {
460460
};
461461

462462
Signal::Success(Value {
463-
kind: Some(Kind::NumberValue(value.powf(root.clone()))),
463+
kind: Some(Kind::NumberValue(value.powf(*root))),
464464
})
465465
}
466466

@@ -484,7 +484,7 @@ fn log(values: &[Value], _ctx: &mut Context) -> Signal {
484484
};
485485

486486
Signal::Success(Value {
487-
kind: Some(Kind::NumberValue(value.log(log.clone()))),
487+
kind: Some(Kind::NumberValue(value.log(*log))),
488488
})
489489
}
490490

@@ -572,7 +572,7 @@ fn min(values: &[Value], _ctx: &mut Context) -> Signal {
572572
};
573573

574574
Signal::Success(Value {
575-
kind: Some(Kind::NumberValue(lhs.min(rhs.clone()))),
575+
kind: Some(Kind::NumberValue(lhs.min(*rhs))),
576576
})
577577
}
578578

@@ -596,7 +596,7 @@ fn max(values: &[Value], _ctx: &mut Context) -> Signal {
596596
};
597597

598598
Signal::Success(Value {
599-
kind: Some(Kind::NumberValue(lhs.max(rhs.clone()))),
599+
kind: Some(Kind::NumberValue(lhs.max(*rhs))),
600600
})
601601
}
602602

@@ -638,9 +638,7 @@ fn random(values: &[Value], _ctx: &mut Context) -> Signal {
638638
};
639639

640640
Signal::Success(Value {
641-
kind: Some(Kind::NumberValue(rand::random_range(
642-
min.clone()..max.clone(),
643-
))),
641+
kind: Some(Kind::NumberValue(rand::random_range(*min..*max))),
644642
})
645643
}
646644

@@ -810,7 +808,7 @@ fn clamp(values: &[Value], _ctx: &mut Context) -> Signal {
810808
};
811809

812810
Signal::Success(Value {
813-
kind: Some(Kind::NumberValue(value.clamp(min.clone(), max.clone()))),
811+
kind: Some(Kind::NumberValue(value.clamp(*min, *max))),
814812
})
815813
}
816814

0 commit comments

Comments
 (0)