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+ }
0 commit comments