Skip to content

Commit 3642ee2

Browse files
committed
feat: added http request/response create functions
1 parent 28402c1 commit 3642ee2

File tree

2 files changed

+90
-5
lines changed

2 files changed

+90
-5
lines changed

src/implementation/http.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use tucana::shared::{Struct, Value};
2+
use tucana::shared::value::Kind;
3+
use crate::context::Context;
4+
use crate::error::RuntimeError;
5+
use crate::registry::HandlerFn;
6+
7+
pub fn collect_http_functions() -> Vec<(&'static str, HandlerFn)> {
8+
vec![
9+
("http::request::create", create_request),
10+
("http::response::create", create_response),
11+
]
12+
}
13+
14+
fn create_request(values: &[Value], _ctx: &mut Context) -> Result<Value, RuntimeError> {
15+
let [Value {
16+
kind: Some(Kind::StringValue(http_method)),
17+
},
18+
Value {
19+
kind: Some(Kind::StructValue(headers)),
20+
},
21+
Value {
22+
kind: Some(Kind::StringValue(http_url)),
23+
},
24+
payload
25+
] = values
26+
else {
27+
return Err(RuntimeError::simple(
28+
"InvalidArgumentRuntimeError",
29+
format!("Expected [method, headers, url, payload] but received {:?}", values),
30+
));
31+
};
32+
33+
let mut fields = std::collections::HashMap::new();
34+
35+
fields.insert("method".to_string(), Value {
36+
kind: Some(Kind::StringValue(http_method.clone())),
37+
});
38+
39+
fields.insert("url".to_string(), Value {
40+
kind: Some(Kind::StringValue(http_url.clone())),
41+
});
42+
43+
fields.insert("headers".to_string(),Value {
44+
kind: Some(Kind::StructValue(headers.clone())),
45+
});
46+
fields.insert("body".to_string(), payload.clone());
47+
48+
Ok(Value {
49+
kind: Some(Kind::StructValue(Struct { fields })),
50+
})
51+
}
52+
53+
fn create_response(values: &[Value], _ctx: &mut Context) -> Result<Value, RuntimeError> {
54+
let [Value {
55+
kind: Some(Kind::NumberValue(http_status_code)),
56+
},
57+
Value {
58+
kind: Some(Kind::StructValue(headers)),
59+
},
60+
payload
61+
] = values
62+
else {
63+
return Err(RuntimeError::simple(
64+
"InvalidArgumentRuntimeError",
65+
format!("Expected [http_status_code, headers, payload] but received {:?}", values),
66+
));
67+
};
68+
69+
let mut fields = std::collections::HashMap::new();
70+
71+
fields.insert("method".to_string(), Value {
72+
kind: Some(Kind::NumberValue(http_status_code.clone())),
73+
});
74+
75+
fields.insert("headers".to_string(),Value {
76+
kind: Some(Kind::StructValue(headers.clone())),
77+
});
78+
fields.insert("body".to_string(), payload.clone());
79+
80+
Ok(Value {
81+
kind: Some(Kind::StructValue(Struct { fields })),
82+
})
83+
}

src/implementation/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use crate::registry::HandlerFn;
22

3-
pub mod array;
4-
pub mod boolean;
3+
mod array;
4+
mod boolean;
55
mod control;
6-
pub mod number;
7-
pub mod object;
8-
pub mod text;
6+
mod number;
7+
mod object;
8+
mod text;
9+
mod http;
910

1011
pub fn collect() -> Vec<(&'static str, HandlerFn)> {
1112
let mut result = vec![];
@@ -16,6 +17,7 @@ pub fn collect() -> Vec<(&'static str, HandlerFn)> {
1617
result.extend(text::collect_text_functions());
1718
result.extend(object::collect_object_functions());
1819
result.extend(control::collect_control_functions());
20+
result.extend(http::collect_http_functions());
1921

2022
result
2123
}

0 commit comments

Comments
 (0)