Skip to content

Commit 4d2a240

Browse files
committed
feat: added new stop, return and respond logic
1 parent ca6f8cf commit 4d2a240

File tree

3 files changed

+134
-53
lines changed

3 files changed

+134
-53
lines changed

src/implementation/control.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,13 @@ use crate::context::signal::Signal;
55

66
pub fn collect_control_functions() -> Vec<(&'static str, HandlerFn)> {
77
vec![
8-
("std::control::break", r#break),
8+
("std::control::stop", stop),
99
("std::control::return", r#return),
1010
]
1111
}
1212

13-
fn r#break(values: &[Value], _ctx: &mut Context) -> Signal {
14-
let [Value { kind }] = values else {
15-
return Signal::Failure(RuntimeError::simple(
16-
"InvalidArgumentRuntimeError",
17-
format!("Expected one generic value but received {:?}", values),
18-
));
19-
};
20-
21-
Signal::Success(Value { kind: kind.clone() })
13+
fn stop(_values: &[Value], _ctx: &mut Context) -> Signal {
14+
Signal::Stop
2215
}
2316

2417
fn r#return(values: &[Value], _ctx: &mut Context) -> Signal {
@@ -29,5 +22,5 @@ fn r#return(values: &[Value], _ctx: &mut Context) -> Signal {
2922
));
3023
};
3124

32-
Signal::Success(Value { kind: kind.clone() })
25+
Signal::Return(Value { kind: kind.clone() })
3326
}

src/implementation/http.rs

Lines changed: 124 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,171 @@
1-
use tucana::shared::{Struct, Value};
2-
use tucana::shared::value::Kind;
31
use crate::context::Context;
42
use crate::context::signal::Signal;
53
use crate::error::RuntimeError;
64
use crate::registry::HandlerFn;
5+
use tucana::shared::value::Kind;
6+
use tucana::shared::{Struct, Value};
77

88
pub fn collect_http_functions() -> Vec<(&'static str, HandlerFn)> {
99
vec![
1010
("http::request::create", create_request),
1111
("http::response::create", create_response),
12+
("http::control::respond", respond),
1213
]
1314
}
1415

16+
fn respond(values: &[Value], _ctx: &mut Context) -> Signal {
17+
let [
18+
Value {
19+
kind: Some(Kind::StructValue(struct_val)),
20+
},
21+
] = values
22+
else {
23+
return Signal::Failure(RuntimeError::simple(
24+
"InvalidArgumentRuntimeError",
25+
format!("Expected exactly one response struct, got {:?}", values),
26+
));
27+
};
28+
29+
let fields = &struct_val.fields;
30+
31+
let Some(headers_val) = fields.get("headers") else {
32+
return Signal::Failure(RuntimeError::simple(
33+
"InvalidArgumentRuntimeError",
34+
"Missing 'headers' field".to_string(),
35+
));
36+
};
37+
38+
let Some(status_code_val) = fields.get("status_code") else {
39+
return Signal::Failure(RuntimeError::simple(
40+
"InvalidArgumentRuntimeError",
41+
"Missing 'status_code' field".to_string(),
42+
));
43+
};
44+
45+
let Some(payload_val) = fields.get("payload") else {
46+
return Signal::Failure(RuntimeError::simple(
47+
"InvalidArgumentRuntimeError",
48+
"Missing 'payload' field".to_string(),
49+
));
50+
};
51+
52+
let Some(Kind::StructValue(_headers_struct)) = &headers_val.kind else {
53+
return Signal::Failure(RuntimeError::simple(
54+
"InvalidArgumentRuntimeError",
55+
"Expected 'headers' to be StructValue".to_string(),
56+
));
57+
};
58+
59+
let Some(Kind::StringValue(_status_code_str)) = &status_code_val.kind else {
60+
return Signal::Failure(RuntimeError::simple(
61+
"InvalidArgumentRuntimeError",
62+
"Expected 'status_code' to be StringValue".to_string(),
63+
));
64+
};
65+
66+
let Some(_payload_kind) = &payload_val.kind else {
67+
return Signal::Failure(RuntimeError::simple(
68+
"InvalidArgumentRuntimeError",
69+
"Expected 'payload' to have a value".to_string(),
70+
));
71+
};
72+
73+
Signal::Respond(Value {
74+
kind: Some(Kind::StructValue(struct_val.clone())),
75+
})
76+
}
77+
1578
fn create_request(values: &[Value], _ctx: &mut Context) -> Signal {
16-
let [Value {
17-
kind: Some(Kind::StringValue(http_method)),
18-
},
19-
Value {
20-
kind: Some(Kind::StructValue(headers)),
21-
},
22-
Value {
23-
kind: Some(Kind::StringValue(http_url)),
24-
},
25-
payload
79+
let [
80+
Value {
81+
kind: Some(Kind::StringValue(http_method)),
82+
},
83+
Value {
84+
kind: Some(Kind::StructValue(headers)),
85+
},
86+
Value {
87+
kind: Some(Kind::StringValue(http_url)),
88+
},
89+
payload,
2690
] = values
2791
else {
2892
return Signal::Failure(RuntimeError::simple(
2993
"InvalidArgumentRuntimeError",
30-
format!("Expected [method, headers, url, payload] but received {:?}", values),
94+
format!(
95+
"Expected [method, headers, url, payload] but received {:?}",
96+
values
97+
),
3198
));
3299
};
33100

34101
let mut fields = std::collections::HashMap::new();
35102

36-
fields.insert("method".to_string(), Value {
37-
kind: Some(Kind::StringValue(http_method.clone())),
38-
});
103+
fields.insert(
104+
"method".to_string(),
105+
Value {
106+
kind: Some(Kind::StringValue(http_method.clone())),
107+
},
108+
);
39109

40-
fields.insert("url".to_string(), Value {
41-
kind: Some(Kind::StringValue(http_url.clone())),
42-
});
110+
fields.insert(
111+
"url".to_string(),
112+
Value {
113+
kind: Some(Kind::StringValue(http_url.clone())),
114+
},
115+
);
43116

44-
fields.insert("headers".to_string(),Value {
45-
kind: Some(Kind::StructValue(headers.clone())),
46-
});
117+
fields.insert(
118+
"headers".to_string(),
119+
Value {
120+
kind: Some(Kind::StructValue(headers.clone())),
121+
},
122+
);
47123
fields.insert("body".to_string(), payload.clone());
48124

49-
Signal::Success(Value {
125+
Signal::Success(Value {
50126
kind: Some(Kind::StructValue(Struct { fields })),
51127
})
52128
}
53129

54130
fn create_response(values: &[Value], _ctx: &mut Context) -> Signal {
55-
let [Value {
56-
kind: Some(Kind::NumberValue(http_status_code)),
57-
},
58-
Value {
59-
kind: Some(Kind::StructValue(headers)),
60-
},
61-
payload
131+
let [
132+
Value {
133+
kind: Some(Kind::NumberValue(http_status_code)),
134+
},
135+
Value {
136+
kind: Some(Kind::StructValue(headers)),
137+
},
138+
payload,
62139
] = values
63140
else {
64141
return Signal::Failure(RuntimeError::simple(
65142
"InvalidArgumentRuntimeError",
66-
format!("Expected [http_status_code, headers, payload] but received {:?}", values),
143+
format!(
144+
"Expected [http_status_code, headers, payload] but received {:?}",
145+
values
146+
),
67147
));
68148
};
69149

70150
let mut fields = std::collections::HashMap::new();
71151

72-
fields.insert("method".to_string(), Value {
73-
kind: Some(Kind::NumberValue(http_status_code.clone())),
74-
});
152+
fields.insert(
153+
"status_code".to_string(),
154+
Value {
155+
kind: Some(Kind::NumberValue(http_status_code.clone())),
156+
},
157+
);
75158

76-
fields.insert("headers".to_string(),Value {
77-
kind: Some(Kind::StructValue(headers.clone())),
78-
});
159+
fields.insert(
160+
"headers".to_string(),
161+
Value {
162+
kind: Some(Kind::StructValue(headers.clone())),
163+
},
164+
);
79165
fields.insert("body".to_string(), payload.clone());
80166

81167
Signal::Success(Value {
82168
kind: Some(Kind::StructValue(Struct { fields })),
83169
})
84170
}
171+

src/main.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
mod config;
12
pub mod context;
23
pub mod error;
4+
mod executor;
35
pub mod implementation;
46
pub mod registry;
5-
mod executor;
6-
mod config;
77

8+
use crate::config::Config;
89
use crate::context::signal::Signal;
910
use crate::executor::Executor;
1011
use crate::implementation::collect;
@@ -18,7 +19,6 @@ use std::collections::HashMap;
1819
use tonic_health::pb::health_server::HealthServer;
1920
use tucana::shared::value::Kind;
2021
use tucana::shared::{ExecutionFlow, NodeFunction, Value};
21-
use crate::config::Config;
2222

2323
fn handle_message(flow: ExecutionFlow, store: &FunctionStore) -> Option<Value> {
2424
let context = Context::new();
@@ -28,11 +28,12 @@ fn handle_message(flow: ExecutionFlow, store: &FunctionStore) -> Option<Value> {
2828
.into_iter()
2929
.map(|node| return (node.database_id, node))
3030
.collect();
31-
31+
3232
let mut executor = Executor::new(store, node_functions, context);
3333
match executor.execute(flow.starting_node_id) {
3434
Signal::Success(v) => Some(v.clone()),
35-
_ => None
35+
Signal::Respond(v) => Some(v.clone()),
36+
_ => None,
3637
}
3738
}
3839

0 commit comments

Comments
 (0)