Skip to main content

adk_ui/model/
component.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4
5/// Protocol-neutral component payload wrapper used by canonical surface models.
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
7#[serde(transparent)]
8pub struct CanonicalComponent(pub Value);
9
10impl CanonicalComponent {
11    pub fn id(&self) -> Option<&str> {
12        self.0.get("id").and_then(Value::as_str)
13    }
14
15    pub fn component_kind(&self) -> Option<&str> {
16        self.0
17            .get("component")
18            .and_then(Value::as_str)
19            .or_else(|| self.0.get("type").and_then(Value::as_str))
20    }
21}
22
23impl From<Value> for CanonicalComponent {
24    fn from(value: Value) -> Self {
25        Self(value)
26    }
27}
28
29impl From<CanonicalComponent> for Value {
30    fn from(value: CanonicalComponent) -> Self {
31        value.0
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38    use serde_json::json;
39
40    #[test]
41    fn canonical_component_extracts_id_and_kind() {
42        let component = CanonicalComponent::from(json!({
43            "id": "root",
44            "component": "Column"
45        }));
46
47        assert_eq!(component.id(), Some("root"));
48        assert_eq!(component.component_kind(), Some("Column"));
49    }
50}