Skip to main content

iii_sdk/
structs.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4
5use crate::protocol::TriggerAction;
6
7/// Input passed to the RBAC middleware function on every function invocation
8/// through the RBAC port.
9///
10/// The middleware can inspect, modify, or reject the call before it reaches
11/// the target function.
12#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
13pub struct MiddlewareFunctionInput {
14    /// ID of the function being invoked.
15    pub function_id: String,
16    /// Payload sent by the caller.
17    pub payload: Value,
18    /// Routing action, if any.
19    #[serde(default, skip_serializing_if = "Option::is_none")]
20    pub action: Option<TriggerAction>,
21    /// Auth context returned by the auth function for this session.
22    pub context: Value,
23    /// Target namespace the invoke addressed; forward the call here to stay in
24    /// the caller's namespace. Absent → the engine's default namespace.
25    #[serde(default, skip_serializing_if = "Option::is_none")]
26    pub namespace: Option<String>,
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32    use serde_json::json;
33
34    #[test]
35    fn deserializes_namespace_from_engine_middleware_input() {
36        // Mirrors the wire object the engine builds (engine/src/engine/mod.rs).
37        let input: MiddlewareFunctionInput = serde_json::from_value(json!({
38            "function_id": "orders::create",
39            "payload": {},
40            "context": {},
41            "namespace": "orders",
42        }))
43        .unwrap();
44        assert_eq!(input.namespace, Some("orders".to_string()));
45    }
46
47    #[test]
48    fn namespace_is_optional() {
49        let input: MiddlewareFunctionInput = serde_json::from_value(json!({
50            "function_id": "orders::create",
51            "payload": {},
52            "context": {},
53        }))
54        .unwrap();
55        assert_eq!(input.namespace, None);
56    }
57}