Skip to main content

adk_ui/model/
action.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4
5/// Canonical representation for UI actions emitted by components.
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
7#[serde(rename_all = "camelCase")]
8pub struct CanonicalAction {
9    pub name: String,
10    #[serde(default, skip_serializing_if = "Option::is_none")]
11    pub context: Option<Value>,
12}
13
14impl CanonicalAction {
15    pub fn new(name: impl Into<String>) -> Self {
16        Self {
17            name: name.into(),
18            context: None,
19        }
20    }
21
22    pub fn with_context(mut self, context: Option<Value>) -> Self {
23        self.context = context;
24        self
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31    use serde_json::json;
32
33    #[test]
34    fn canonical_action_serializes_with_optional_context() {
35        let action = CanonicalAction::new("submit").with_context(Some(json!({ "k": "v" })));
36        let value = serde_json::to_value(action).expect("serialize canonical action");
37        assert_eq!(value["name"], "submit");
38        assert_eq!(value["context"]["k"], "v");
39    }
40}