Skip to main content

iii_sdk/
types.rs

1use std::sync::Arc;
2
3use futures_util::future::BoxFuture;
4use serde_json::Value;
5
6use crate::{
7    channels::{ChannelReader, ChannelWriter, StreamChannelRef},
8    error::Error,
9    protocol::{RegisterFunctionMessage, RegisterTriggerTypeMessage},
10    triggers::TriggerHandler,
11};
12
13/// A dispatchable function handler. Receives the invocation payload.
14///
15/// Handlers that also want the optional per-invocation `metadata` sidecar use
16/// [`RemoteFunctionHandlerWithMetadata`]; this single-argument shape is kept
17/// for backward compatibility.
18pub type RemoteFunctionHandler =
19    Arc<dyn Fn(Value) -> BoxFuture<'static, Result<Value, Error>> + Send + Sync>;
20
21/// A dispatchable function handler that also receives the optional
22/// per-invocation `metadata` sidecar (delivered as a distinct argument rather
23/// than folded into the payload; `None` when the caller attached none).
24///
25/// This is the SDK's internal dispatch shape: handlers built from
26/// metadata-unaware functions ignore the second argument.
27pub type RemoteFunctionHandlerWithMetadata =
28    Arc<dyn Fn(Value, Option<Value>) -> BoxFuture<'static, Result<Value, Error>> + Send + Sync>;
29
30#[derive(Clone)]
31pub struct RemoteFunctionData {
32    pub message: RegisterFunctionMessage,
33    pub handler: Option<RemoteFunctionHandlerWithMetadata>,
34}
35
36#[derive(Clone)]
37pub struct RemoteTriggerTypeData {
38    pub message: RegisterTriggerTypeMessage,
39    pub handler: Arc<dyn TriggerHandler>,
40}
41
42/// Streaming request type, mirroring the Node and Python `StreamRequest`.
43///
44/// Alias of [`iii_helpers::http::HttpRequest`]; added for cross-language parity.
45pub type StreamRequest<T = Value> = iii_helpers::http::HttpRequest<T>;
46
47/// Streaming response type, mirroring the Node and Python `StreamResponse`.
48///
49/// Alias of [`iii_helpers::http::HttpResponse`]; added for cross-language parity.
50pub type StreamResponse<T = Value> = iii_helpers::http::HttpResponse<T>;
51
52/// A streaming channel pair for worker-to-worker data transfer.
53pub struct Channel {
54    pub writer: ChannelWriter,
55    pub reader: ChannelReader,
56    pub writer_ref: StreamChannelRef,
57    pub reader_ref: StreamChannelRef,
58}
59
60#[cfg(test)]
61mod tests {
62    #[test]
63    fn http_request_defaults_when_missing_fields() {
64        let request: iii_helpers::http::HttpRequest = serde_json::from_str("{}").unwrap();
65
66        assert!(request.query_params.is_empty());
67        assert!(request.path_params.is_empty());
68        assert!(request.headers.is_empty());
69        assert_eq!(request.path, "");
70        assert_eq!(request.method, "");
71        assert!(request.body.is_null());
72    }
73}