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
23/// alongside 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.
27/// <!-- docs:internal -->
28pub type RemoteFunctionHandlerWithMetadata =
29    Arc<dyn Fn(Value, Option<Value>) -> BoxFuture<'static, Result<Value, Error>> + Send + Sync>;
30
31#[derive(Clone)]
32pub struct RemoteFunctionData {
33    pub message: RegisterFunctionMessage,
34    pub handler: Option<RemoteFunctionHandlerWithMetadata>,
35}
36
37#[derive(Clone)]
38pub struct RemoteTriggerTypeData {
39    pub message: RegisterTriggerTypeMessage,
40    pub handler: Arc<dyn TriggerHandler>,
41}
42
43/// Incoming streaming request received by a function registered with a stream trigger.
44///
45/// Alias of [`iii_helpers::http::HttpRequest`].
46pub type StreamRequest<T = Value> = iii_helpers::http::HttpRequest<T>;
47
48/// Streaming response type, mirroring the Node and Python `StreamResponse`.
49///
50/// Alias of [`iii_helpers::http::HttpResponse`]; added for cross-language parity.
51pub type StreamResponse<T = Value> = iii_helpers::http::HttpResponse<T>;
52
53/// A streaming channel pair for worker-to-worker data transfer.
54pub struct Channel {
55    pub writer: ChannelWriter,
56    pub reader: ChannelReader,
57    pub writer_ref: StreamChannelRef,
58    pub reader_ref: StreamChannelRef,
59}
60
61#[cfg(test)]
62mod tests {
63    #[test]
64    fn http_request_defaults_when_missing_fields() {
65        let request: iii_helpers::http::HttpRequest = serde_json::from_str("{}").unwrap();
66
67        assert!(request.query_params.is_empty());
68        assert!(request.path_params.is_empty());
69        assert!(request.headers.is_empty());
70        assert_eq!(request.path, "");
71        assert_eq!(request.method, "");
72        assert!(request.body.is_null());
73    }
74}