Skip to main content

ergo_adapter/
provides.rs

1use std::collections::{HashMap, HashSet};
2
3use crate::manifest::AdapterManifest;
4use crate::provenance;
5
6/// What an adapter provides for composition validation.
7/// Built from AdapterManifest after registration-phase validation passes.
8#[derive(Debug, Clone, Default)]
9pub struct AdapterProvides {
10    pub context: HashMap<String, ContextKeyProvision>,
11    pub events: HashSet<String>,
12    pub effects: HashSet<String>,
13    pub effect_schemas: HashMap<String, serde_json::Value>,
14    pub event_schemas: HashMap<String, serde_json::Value>,
15    pub capture_format_version: String,
16    pub adapter_fingerprint: String,
17}
18
19#[derive(Debug, Clone)]
20pub struct ContextKeyProvision {
21    pub ty: String, // Keep as String; composition converts to ValueType
22    pub required: bool,
23    pub writable: bool,
24}
25
26impl AdapterProvides {
27    /// Build from a validated AdapterManifest.
28    pub fn from_manifest(manifest: &AdapterManifest) -> Self {
29        let context = manifest
30            .context_keys
31            .iter()
32            .map(|k| {
33                (
34                    k.name.clone(),
35                    ContextKeyProvision {
36                        ty: k.ty.clone(),
37                        required: k.required,
38                        writable: k.writable.unwrap_or(false),
39                    },
40                )
41            })
42            .collect();
43
44        let events = manifest
45            .event_kinds
46            .iter()
47            .map(|e| e.name.clone())
48            .collect();
49        let event_schemas = manifest
50            .event_kinds
51            .iter()
52            .map(|e| (e.name.clone(), e.payload_schema.clone()))
53            .collect();
54
55        let effects = manifest
56            .accepts
57            .as_ref()
58            .map(|a| a.effects.iter().map(|e| e.name.clone()).collect())
59            .unwrap_or_default();
60        let effect_schemas = manifest
61            .accepts
62            .as_ref()
63            .map(|a| {
64                a.effects
65                    .iter()
66                    .map(|effect| (effect.name.clone(), effect.payload_schema.clone()))
67                    .collect()
68            })
69            .unwrap_or_default();
70
71        Self {
72            context,
73            events,
74            effects,
75            effect_schemas,
76            event_schemas,
77            capture_format_version: manifest.capture.format_version.clone(),
78            adapter_fingerprint: provenance::fingerprint(manifest),
79        }
80    }
81}