allora_runtime/spec/
channel_spec.rs

1//! ChannelSpec: immutable, format-agnostic description of a Channel (pure pipe).
2//!
3//! # Purpose
4//! Represents the minimal set of attributes required to instantiate a Channel.
5//! Parsing (YAML / future JSON / XML) is delegated to format-specific parsers in
6//! `channel_spec_yaml.rs` (and future siblings). Instantiation is handled by DSL builders.
7//!
8//! # Invariants
9//! * `id` is optional; if omitted a runtime builder will generate a unique id.
10//! * If `id` is present it must not be the empty string (builders enforce).
11//! * `kind` enumerates supported implementations; currently only `queue`.
12//!
13//! # Adding New Channel Kinds
14//! 1. Extend `ChannelKindSpec` enum (e.g. `Kafka`, `Redis`).
15//! 2. Add validation + mapping logic inside each format parser.
16//! 3. Update the component builder to match on the new variant.
17//! 4. Provide user-facing documentation and tests (spec + builder).
18//!
19//! # Programmatic Example
20//! ```rust
21//! use allora_runtime::spec::ChannelSpec;
22//! let spec = ChannelSpec::queue().id("orders-events");
23//! assert_eq!(spec.channel_id(), Some("orders-events"));
24//! ```
25//!
26//! Combined Channel specification & YAML translator (v1, pure pipe).
27//! This file merges previous channel_dsl.rs (YAML DSL) and channel_spec.rs (programmatic spec).
28//! See top-level docs in spec/mod.rs for overarching concepts.
29
30use serde::Deserialize;
31
32/// Programmatic Channel specification (pure pipe).
33#[derive(Debug, Clone, Default)]
34pub struct ChannelSpec {
35    pub(crate) id: Option<String>,
36    pub(crate) kind: ChannelKindSpec,
37}
38
39impl ChannelSpec {
40    pub fn direct() -> Self {
41        Self {
42            id: None,
43            kind: ChannelKindSpec::Direct,
44        }
45    }
46    pub fn queue() -> Self {
47        Self {
48            id: None,
49            kind: ChannelKindSpec::Queue,
50        }
51    }
52    /// Set the channel identifier (required by some builders, ignored by others).
53    pub fn id(mut self, id: impl Into<String>) -> Self {
54        self.id = Some(id.into());
55        self
56    }
57    /// Return the channel implementation kind captured by this spec.
58    pub fn kind(&self) -> ChannelKindSpec {
59        self.kind
60    }
61    /// Get the channel identifier, if one was set.
62    pub fn channel_id(&self) -> Option<&str> {
63        self.id.as_deref()
64    }
65}
66
67/// Supported channel kinds (extend as more implementations are introduced).
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
69#[serde(rename_all = "snake_case")]
70pub enum ChannelKindSpec {
71    Direct,
72    Queue,
73}
74
75impl Default for ChannelKindSpec {
76    fn default() -> Self {
77        ChannelKindSpec::Direct
78    }
79}