|
| 1 | +//! HTTP and REST-oriented helper handlers. |
| 2 | +//! |
| 3 | +//! These functions build/validate plain struct payloads that the runtime treats as regular values. |
| 4 | +
|
| 5 | +use crate::handler::argument::Argument; |
| 6 | +use crate::handler::macros::args; |
| 7 | +use crate::handler::registry::FunctionRegistration; |
| 8 | +use crate::runtime::execution::value_store::ValueStore; |
| 9 | +use crate::types::errors::runtime_error::RuntimeError; |
| 10 | +use crate::types::signal::Signal; |
| 11 | +use tucana::shared::helper::value::ToValue; |
| 12 | +use tucana::shared::value::Kind; |
| 13 | +use tucana::shared::{Struct, Value}; |
| 14 | + |
| 15 | +pub(crate) const FUNCTIONS: &[FunctionRegistration] = &[ |
| 16 | + FunctionRegistration::eager("http::request::create", create_request, 4), |
| 17 | + FunctionRegistration::eager("http::response::create", create_response, 3), |
| 18 | + FunctionRegistration::eager("rest::control::respond", respond, 1), |
| 19 | +]; |
| 20 | + |
| 21 | +fn fail(category: &str, message: impl Into<String>) -> Signal { |
| 22 | + Signal::Failure(RuntimeError::new("T-HTTP-000000", category, message)) |
| 23 | +} |
| 24 | + |
| 25 | +fn respond( |
| 26 | + args: &[Argument], |
| 27 | + _ctx: &mut ValueStore, |
| 28 | + _run: &mut dyn FnMut(i64, &mut ValueStore) -> Signal, |
| 29 | +) -> Signal { |
| 30 | + args!(args => struct_val: Struct); |
| 31 | + |
| 32 | + let fields = &struct_val.fields; |
| 33 | + |
| 34 | + let Some(headers_val) = fields.get("headers") else { |
| 35 | + return Signal::Failure(RuntimeError::new( |
| 36 | + "T-RT-000000", |
| 37 | + "InvalidArgumentRuntimeError", |
| 38 | + "Missing 'headers' field".to_string(), |
| 39 | + )); |
| 40 | + }; |
| 41 | + |
| 42 | + let Some(status_code_val) = fields.get("http_status_code") else { |
| 43 | + return fail( |
| 44 | + "InvalidArgumentRuntimeError", |
| 45 | + "Missing 'http_status_code' field", |
| 46 | + ); |
| 47 | + }; |
| 48 | + |
| 49 | + let Some(payload_val) = fields.get("payload") else { |
| 50 | + return Signal::Failure(RuntimeError::new( |
| 51 | + "T-RT-000000", |
| 52 | + "InvalidArgumentRuntimeError", |
| 53 | + "Missing 'payload' field".to_string(), |
| 54 | + )); |
| 55 | + }; |
| 56 | + |
| 57 | + let Some(Kind::StructValue(_headers_struct)) = &headers_val.kind else { |
| 58 | + return fail( |
| 59 | + "InvalidArgumentRuntimeError", |
| 60 | + "Expected 'headers' to be StructValue", |
| 61 | + ); |
| 62 | + }; |
| 63 | + |
| 64 | + let Some(Kind::NumberValue(_status_code_str)) = &status_code_val.kind else { |
| 65 | + return Signal::Failure(RuntimeError::new( |
| 66 | + "T-RT-000000", |
| 67 | + "InvalidArgumentRuntimeError", |
| 68 | + "Expected 'status_code' to be NumberValue".to_string(), |
| 69 | + )); |
| 70 | + }; |
| 71 | + |
| 72 | + let Some(_payload_kind) = &payload_val.kind else { |
| 73 | + return Signal::Failure(RuntimeError::new( |
| 74 | + "T-RT-000000", |
| 75 | + "InvalidArgumentRuntimeError", |
| 76 | + "Expected 'payload' to have a value".to_string(), |
| 77 | + )); |
| 78 | + }; |
| 79 | + |
| 80 | + // `Respond` is a control signal; the executor can still continue with `next` if present. |
| 81 | + Signal::Respond(Value { |
| 82 | + kind: Some(Kind::StructValue(struct_val.clone())), |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +fn create_request( |
| 87 | + args: &[Argument], |
| 88 | + _ctx: &mut ValueStore, |
| 89 | + _run: &mut dyn FnMut(i64, &mut ValueStore) -> Signal, |
| 90 | +) -> Signal { |
| 91 | + args!(args => http_method: String, headers: Struct, http_url: String, payload: Value); |
| 92 | + let mut fields = std::collections::HashMap::new(); |
| 93 | + |
| 94 | + fields.insert(String::from("http_method"), http_method.to_value()); |
| 95 | + fields.insert(String::from("url"), http_url.to_value()); |
| 96 | + fields.insert(String::from("payload"), payload.clone()); |
| 97 | + fields.insert( |
| 98 | + String::from("headers"), |
| 99 | + Value { |
| 100 | + kind: Some(Kind::StructValue(headers.clone())), |
| 101 | + }, |
| 102 | + ); |
| 103 | + |
| 104 | + Signal::Success(Value { |
| 105 | + kind: Some(Kind::StructValue(Struct { fields })), |
| 106 | + }) |
| 107 | +} |
| 108 | + |
| 109 | +fn create_response( |
| 110 | + args: &[Argument], |
| 111 | + _ctx: &mut ValueStore, |
| 112 | + _run: &mut dyn FnMut(i64, &mut ValueStore) -> Signal, |
| 113 | +) -> Signal { |
| 114 | + args!(args => http_status_code: i64, headers: Struct, payload: Value); |
| 115 | + let mut fields = std::collections::HashMap::new(); |
| 116 | + |
| 117 | + fields.insert( |
| 118 | + String::from("http_status_code"), |
| 119 | + http_status_code.to_value(), |
| 120 | + ); |
| 121 | + fields.insert(String::from("payload"), payload.clone()); |
| 122 | + |
| 123 | + fields.insert( |
| 124 | + String::from("headers"), |
| 125 | + Value { |
| 126 | + kind: Some(Kind::StructValue(headers.clone())), |
| 127 | + }, |
| 128 | + ); |
| 129 | + |
| 130 | + Signal::Success(Value { |
| 131 | + kind: Some(Kind::StructValue(Struct { fields })), |
| 132 | + }) |
| 133 | +} |
0 commit comments