allora_runtime/spec/channels_spec.rs
1//! ChannelsSpec: collection of `ChannelSpec` entries under one version (schema v1).
2//!
3//! # Semantics
4//! * Represents intent for multiple channels declared together sharing a version.
5//! * Order of insertion is preserved (stable iteration order).
6//! * Does not enforce uniqueness or non-empty ids (delegated to builders).
7//! * `ChannelsSpec::default()` now yields version=1 (matches parser expectations) and an empty channel list.
8//!
9//! # Construction Patterns
10//! * Immutable-style chaining via `add` (returns Self for fluent building).
11//! * Mutable push via `push` when incremental assembly is preferred.
12//!
13//! # Example
14//! ```rust
15//! use allora_runtime::spec::{ChannelsSpec, ChannelSpec};
16//! let spec = ChannelsSpec::new(1)
17//! .add(ChannelSpec::queue().id("orders"))
18//! .add(ChannelSpec::queue().id("payments"));
19//! assert_eq!(spec.channels().len(), 2);
20//! ```
21//!
22//! # Deferred Validation
23//! Structural and kind validation occur during YAML parsing (`channels_spec_yaml.rs`). Uniqueness
24//! and deterministic id generation for missing ids occur during runtime build (`build_channels_from_spec`).
25
26use crate::spec::ChannelSpec;
27
28#[derive(Debug, Clone)]
29pub struct ChannelsSpec {
30 version: u32,
31 channels: Vec<ChannelSpec>,
32}
33// Explicit Default to avoid version=0 (parser requires version==1)
34impl Default for ChannelsSpec {
35 fn default() -> Self {
36 ChannelsSpec::new(1)
37 }
38}
39
40impl ChannelsSpec {
41 pub fn new(version: u32) -> Self {
42 Self {
43 version,
44 channels: Vec::new(),
45 }
46 }
47 pub fn add(mut self, ch: ChannelSpec) -> Self {
48 self.channels.push(ch);
49 self
50 }
51 pub fn push(&mut self, ch: ChannelSpec) {
52 self.channels.push(ch);
53 }
54 pub fn version(&self) -> u32 {
55 self.version
56 }
57 pub fn channels(&self) -> &[ChannelSpec] {
58 &self.channels
59 }
60 pub fn into_channels(self) -> Vec<ChannelSpec> {
61 self.channels
62 }
63}