Skip to main content

allora_runtime/spec/
allora_spec.rs

1//! AlloraSpec: top-level configuration spec aggregating inner component specs (currently channels only).
2//!
3//! # Responsibilities
4//! * Capture version once for all nested component collections.
5//! * Provide structured access to nested specs (`channels_spec`).
6//! * Act as stable expansion point for future components (endpoints, filters, adapters) without
7//!   changing the external DSL facade return type (`AlloraRuntime`).
8//!
9//! # Not Responsible For
10//! * Parsing (handled by `AlloraSpecYamlParser`).
11//! * Runtime instantiation (handled by `dsl/component_builders.rs` via `build_channels_from_spec`).
12//! * Validation beyond version presence (nested parsers validate their own structures).
13//!
14//! # Future Extensions
15//! Additional fields (e.g. `endpoints: EndpointsSpec`, `filters: FiltersSpec`) can be added while
16//! preserving existing methods. Accessor naming should stay consistent (`*_spec()` for borrowing,
17//! `into_*_spec()` for ownership transfer).
18//!
19//! # Example
20//! ```rust
21//! use allora_runtime::spec::{AlloraSpec, ChannelsSpec, ChannelSpec};
22//! // Programmatic construction
23//! let channels = ChannelsSpec::new(1).add(ChannelSpec::queue().id("orders"));
24//! let all = AlloraSpec::new(1, channels);
25//! assert_eq!(all.version(), 1);
26//! assert_eq!(all.channels_spec().channels().len(), 1);
27//! ```
28//!
29
30use crate::spec::{
31    AggregatorsSpec, ChannelsSpec, FiltersSpec, HttpInboundAdaptersSpec, HttpOutboundAdaptersSpec,
32    ServiceActivatorsSpec,
33};
34
35#[derive(Debug, Clone)]
36pub struct AlloraSpec {
37    version: u32,
38    channels: ChannelsSpec,
39    filters: Option<FiltersSpec>,
40    services: Option<ServiceActivatorsSpec>,
41    http_inbound_adapters: Option<HttpInboundAdaptersSpec>,
42    http_outbound_adapters: Option<HttpOutboundAdaptersSpec>,
43    aggregators: Option<AggregatorsSpec>,
44}
45
46impl AlloraSpec {
47    pub fn new(version: u32, channels: ChannelsSpec) -> Self {
48        Self {
49            version,
50            channels,
51            filters: None,
52            services: None,
53            http_inbound_adapters: None,
54            http_outbound_adapters: None,
55            aggregators: None,
56        }
57    }
58    /// Construct with an optional filters collection in a single call (ergonomic alternative to chaining `with_filters`).
59    /// When `filters` is `None`, behaves the same as `new`.
60    pub fn new_with_filters(
61        version: u32,
62        channels: ChannelsSpec,
63        filters: Option<FiltersSpec>,
64    ) -> Self {
65        Self {
66            version,
67            channels,
68            filters,
69            services: None,
70            http_inbound_adapters: None,
71            http_outbound_adapters: None,
72            aggregators: None,
73        }
74    }
75    pub fn with_filters(mut self, filters: FiltersSpec) -> Self {
76        self.filters = Some(filters);
77        self
78    }
79    pub fn with_services(mut self, services: ServiceActivatorsSpec) -> Self {
80        self.services = Some(services);
81        self
82    }
83    pub fn with_http_inbound_adapters(mut self, adapters: HttpInboundAdaptersSpec) -> Self {
84        self.http_inbound_adapters = Some(adapters);
85        self
86    }
87    pub fn with_http_outbound_adapters(mut self, adapters: HttpOutboundAdaptersSpec) -> Self {
88        self.http_outbound_adapters = Some(adapters);
89        self
90    }
91    pub fn with_aggregators(mut self, aggregators: AggregatorsSpec) -> Self {
92        self.aggregators = Some(aggregators);
93        self
94    }
95    pub fn version(&self) -> u32 {
96        self.version
97    }
98    pub fn channels_spec(&self) -> &ChannelsSpec {
99        &self.channels
100    }
101    pub fn filters_spec(&self) -> Option<&FiltersSpec> {
102        self.filters.as_ref()
103    }
104    pub fn services_spec(&self) -> Option<&ServiceActivatorsSpec> {
105        self.services.as_ref()
106    }
107    pub fn http_inbound_adapters_spec(&self) -> Option<&HttpInboundAdaptersSpec> {
108        self.http_inbound_adapters.as_ref()
109    }
110    pub fn http_outbound_adapters_spec(&self) -> Option<&HttpOutboundAdaptersSpec> {
111        self.http_outbound_adapters.as_ref()
112    }
113    pub fn aggregators_spec(&self) -> Option<&AggregatorsSpec> {
114        self.aggregators.as_ref()
115    }
116    pub fn into_channels_spec(self) -> ChannelsSpec {
117        self.channels
118    }
119    pub fn into_filters_spec(self) -> Option<FiltersSpec> {
120        self.filters
121    }
122    pub fn into_services_spec(self) -> Option<ServiceActivatorsSpec> {
123        self.services
124    }
125    pub fn into_http_inbound_adapters_spec(self) -> Option<HttpInboundAdaptersSpec> {
126        self.http_inbound_adapters
127    }
128    pub fn into_http_outbound_adapters_spec(self) -> Option<HttpOutboundAdaptersSpec> {
129        self.http_outbound_adapters
130    }
131    pub fn into_aggregators_spec(self) -> Option<AggregatorsSpec> {
132        self.aggregators
133    }
134}