drasi_lib/sources/traits.rs
1// Copyright 2025 The Drasi Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Source trait module
16//!
17//! This module provides the core trait that all source plugins must implement.
18//! It separates the plugin contract from the source manager and implementation details.
19//!
20//! # Plugin Architecture
21//!
22//! Each source plugin:
23//! 1. Defines its own typed configuration struct with builder pattern
24//! 2. Creates a `SourceBase` instance using `SourceBaseParams`
25//! 3. Implements the `Source` trait
26//! 4. Is passed to `DrasiLib` via `add_source()` which takes ownership
27//!
28//! drasi-lib has no knowledge of which plugins exist - it only knows about this trait.
29//!
30//! # Runtime Context Initialization
31//!
32//! Sources receive all drasi-lib services through a single `initialize()` call
33//! when added to DrasiLib. The `SourceRuntimeContext` provides:
34//! - `event_tx`: Channel for component lifecycle events
35//! - `state_store`: Optional persistent state storage
36//!
37//! This replaces the previous `inject_*` methods with a cleaner single-call pattern.
38
39use anyhow::Result;
40use async_trait::async_trait;
41
42use crate::channels::*;
43use crate::context::SourceRuntimeContext;
44
45/// Trait defining the interface for all source implementations.
46///
47/// This is the core abstraction that all source plugins must implement.
48/// drasi-lib only interacts with sources through this trait - it has no
49/// knowledge of specific plugin types or their configurations.
50///
51/// # Example Implementation
52///
53/// ```ignore
54/// use drasi_lib::Source;
55/// use drasi_lib::sources::{SourceBase, SourceBaseParams};
56/// use drasi_lib::context::SourceRuntimeContext;
57///
58/// pub struct MySource {
59/// base: SourceBase,
60/// // Plugin-specific fields
61/// }
62///
63/// impl MySource {
64/// pub fn new(config: MySourceConfig) -> Result<Self> {
65/// let params = SourceBaseParams::new(&config.id)
66/// .with_dispatch_mode(config.dispatch_mode)
67/// .with_dispatch_buffer_capacity(config.buffer_capacity);
68///
69/// Ok(Self {
70/// base: SourceBase::new(params)?,
71/// })
72/// }
73/// }
74///
75/// #[async_trait]
76/// impl Source for MySource {
77/// fn id(&self) -> &str {
78/// &self.base.id
79/// }
80///
81/// fn type_name(&self) -> &str {
82/// "my-source"
83/// }
84///
85/// fn properties(&self) -> HashMap<String, Value> {
86/// // Return plugin-specific properties
87/// }
88///
89/// async fn initialize(&self, context: SourceRuntimeContext) {
90/// self.base.initialize(context).await;
91/// }
92///
93/// // ... implement other methods
94/// }
95/// ```
96#[async_trait]
97pub trait Source: Send + Sync {
98 /// Get the source's unique identifier
99 fn id(&self) -> &str;
100
101 /// Get the source type name (e.g., "postgres", "http", "mock")
102 fn type_name(&self) -> &str;
103
104 /// Get the source's configuration properties for inspection
105 ///
106 /// This returns a HashMap representation of the source's configuration
107 /// for use in APIs and inspection. The actual typed configuration is
108 /// owned by the plugin - this is just for external visibility.
109 fn properties(&self) -> std::collections::HashMap<String, serde_json::Value>;
110
111 /// Get the dispatch mode for this source (Channel or Broadcast)
112 ///
113 /// Default is Channel mode for backpressure support.
114 fn dispatch_mode(&self) -> DispatchMode {
115 DispatchMode::Channel
116 }
117
118 /// Whether this source should auto-start when DrasiLib starts
119 ///
120 /// Default is `true`. Override to return `false` if this source
121 /// should only be started manually via `start_source()`.
122 fn auto_start(&self) -> bool {
123 true
124 }
125
126 /// Start the source
127 ///
128 /// This begins data ingestion and event generation.
129 async fn start(&self) -> Result<()>;
130
131 /// Stop the source
132 ///
133 /// This stops data ingestion and cleans up resources.
134 async fn stop(&self) -> Result<()>;
135
136 /// Get the current status of the source
137 async fn status(&self) -> ComponentStatus;
138
139 /// Subscribe to this source for change events
140 ///
141 /// This is called by queries to receive data changes from this source.
142 /// The source should return a receiver for streaming events and optionally
143 /// a bootstrap receiver for initial data.
144 ///
145 /// # Arguments
146 /// * `settings` - Subscription settings including query ID, text, and labels of interest
147 ///
148 /// # Returns
149 /// A SubscriptionResponse containing:
150 /// * A receiver for streaming source events
151 /// * Optionally a bootstrap receiver for initial data
152 async fn subscribe(
153 &self,
154 settings: crate::config::SourceSubscriptionSettings,
155 ) -> Result<SubscriptionResponse>;
156
157 /// Downcast helper for testing - allows access to concrete types
158 fn as_any(&self) -> &dyn std::any::Any;
159
160 /// Initialize the source with runtime context.
161 ///
162 /// This method is called automatically by DrasiLib when the source is added
163 /// via `add_source()`. Plugin developers do not need to call this directly.
164 ///
165 /// The context provides access to:
166 /// - `source_id`: The source's unique identifier
167 /// - `event_tx`: Channel for reporting component lifecycle events
168 /// - `state_store`: Optional persistent state storage
169 ///
170 /// Implementation should delegate to `self.base.initialize(context).await`.
171 async fn initialize(&self, context: SourceRuntimeContext);
172
173 /// Set the bootstrap provider for this source
174 ///
175 /// This method allows setting a bootstrap provider after source construction.
176 /// It is optional - sources without a bootstrap provider will report that
177 /// bootstrap is not available.
178 ///
179 /// Implementation should delegate to `self.base.set_bootstrap_provider(provider).await`.
180 async fn set_bootstrap_provider(
181 &self,
182 _provider: Box<dyn crate::bootstrap::BootstrapProvider + 'static>,
183 ) {
184 // Default implementation does nothing - sources that support bootstrap
185 // should override this to delegate to their SourceBase
186 }
187}
188
189/// Blanket implementation of Source for `Box<dyn Source>`
190///
191/// This allows boxed trait objects to be used with methods expecting `impl Source`.
192#[async_trait]
193impl Source for Box<dyn Source + 'static> {
194 fn id(&self) -> &str {
195 (**self).id()
196 }
197
198 fn type_name(&self) -> &str {
199 (**self).type_name()
200 }
201
202 fn properties(&self) -> std::collections::HashMap<String, serde_json::Value> {
203 (**self).properties()
204 }
205
206 fn dispatch_mode(&self) -> DispatchMode {
207 (**self).dispatch_mode()
208 }
209
210 fn auto_start(&self) -> bool {
211 (**self).auto_start()
212 }
213
214 async fn start(&self) -> Result<()> {
215 (**self).start().await
216 }
217
218 async fn stop(&self) -> Result<()> {
219 (**self).stop().await
220 }
221
222 async fn status(&self) -> ComponentStatus {
223 (**self).status().await
224 }
225
226 async fn subscribe(
227 &self,
228 settings: crate::config::SourceSubscriptionSettings,
229 ) -> Result<SubscriptionResponse> {
230 (**self).subscribe(settings).await
231 }
232
233 fn as_any(&self) -> &dyn std::any::Any {
234 (**self).as_any()
235 }
236
237 async fn initialize(&self, context: SourceRuntimeContext) {
238 (**self).initialize(context).await
239 }
240
241 async fn set_bootstrap_provider(
242 &self,
243 provider: Box<dyn crate::bootstrap::BootstrapProvider + 'static>,
244 ) {
245 (**self).set_bootstrap_provider(provider).await
246 }
247}