Skip to main content

greentic_flow/
component_setup.rs

1use greentic_interfaces_host::component_v0_6::exports::greentic::component::node::{
2    ComponentDescriptor, SchemaSource, SetupOutput,
3};
4
5pub fn has_setup(descriptor: &ComponentDescriptor) -> bool {
6    descriptor.setup.is_some()
7}
8
9pub fn qa_spec_ref(descriptor: &ComponentDescriptor) -> Option<&SchemaSource> {
10    descriptor.setup.as_ref().map(|setup| &setup.qa_spec)
11}
12
13pub fn answers_schema_ref(descriptor: &ComponentDescriptor) -> Option<&SchemaSource> {
14    descriptor.setup.as_ref().map(|setup| &setup.answers_schema)
15}
16
17pub fn setup_outputs(descriptor: &ComponentDescriptor) -> Option<&[SetupOutput]> {
18    descriptor
19        .setup
20        .as_ref()
21        .map(|setup| setup.outputs.as_slice())
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27    use greentic_interfaces_host::component_v0_6::exports::greentic::component::node::{
28        IoSchema, Op, SchemaRef, SetupContract,
29    };
30
31    fn sample_descriptor() -> ComponentDescriptor {
32        ComponentDescriptor {
33            name: "demo".to_string(),
34            version: "0.1.0".to_string(),
35            summary: Some("demo component".to_string()),
36            capabilities: vec!["host:config".to_string()],
37            ops: vec![Op {
38                name: "setup.apply_answers".to_string(),
39                summary: None,
40                input: IoSchema {
41                    schema: SchemaSource::InlineCbor(vec![0xa0]),
42                    content_type: "application/cbor".to_string(),
43                    schema_version: None,
44                },
45                output: IoSchema {
46                    schema: SchemaSource::InlineCbor(vec![0xa0]),
47                    content_type: "application/cbor".to_string(),
48                    schema_version: None,
49                },
50                examples: Vec::new(),
51            }],
52            schemas: vec![SchemaRef {
53                id: "schema-1".to_string(),
54                content_type: "application/cbor".to_string(),
55                blake3_hash: "deadbeef".to_string(),
56                version: "1".to_string(),
57                bytes: None,
58                uri: None,
59            }],
60            setup: Some(SetupContract {
61                qa_spec: SchemaSource::InlineCbor(vec![0xa1, 0x01]),
62                answers_schema: SchemaSource::InlineCbor(vec![0xa1, 0x02]),
63                examples: Vec::new(),
64                outputs: vec![SetupOutput::ConfigOnly],
65            }),
66        }
67    }
68
69    #[test]
70    fn extracts_setup_refs() {
71        let descriptor = sample_descriptor();
72        assert!(has_setup(&descriptor));
73        assert!(matches!(
74            qa_spec_ref(&descriptor),
75            Some(SchemaSource::InlineCbor(bytes)) if bytes == &vec![0xa1, 0x01]
76        ));
77        assert!(matches!(
78            answers_schema_ref(&descriptor),
79            Some(SchemaSource::InlineCbor(bytes)) if bytes == &vec![0xa1, 0x02]
80        ));
81        assert!(matches!(
82            setup_outputs(&descriptor),
83            Some(outputs) if outputs.len() == 1
84        ));
85    }
86}