buswatch_tui/source/
mod.rs

1//! Data source abstraction for receiving monitor snapshots.
2//!
3//! This module provides a trait-based abstraction for receiving monitor data
4//! from various sources (files, message bus channels, network streams, etc.).
5
6mod channel;
7mod file;
8mod stream;
9
10pub use channel::ChannelSource;
11pub use file::FileSource;
12pub use stream::StreamSource;
13
14// Re-export types from buswatch-types
15pub use buswatch_types::{
16    Microseconds, ModuleMetrics, ReadMetrics, SchemaVersion, Snapshot, WriteMetrics,
17};
18
19use std::fmt::Debug;
20
21/// Trait for receiving monitor data from various sources.
22///
23/// Implementations of this trait provide monitor snapshots from different
24/// backends - file polling, message bus subscriptions, or in-memory channels.
25///
26/// # Example
27///
28/// ```
29/// use buswatch_tui::{FileSource, DataSource};
30///
31/// let mut source = FileSource::new("monitor.json");
32/// if let Some(snapshot) = source.poll() {
33///     println!("Got {} modules", snapshot.len());
34/// }
35/// ```
36pub trait DataSource: Send + Debug {
37    /// Poll for the latest snapshot.
38    ///
39    /// Returns `Some(snapshot)` if new data is available, `None` otherwise.
40    /// This method should be non-blocking.
41    fn poll(&mut self) -> Option<Snapshot>;
42
43    /// Returns a human-readable description of the source.
44    ///
45    /// Used for display in the TUI status bar.
46    fn description(&self) -> &str;
47
48    /// Check if the source has encountered an error.
49    ///
50    /// Returns the error message if an error occurred during the last poll.
51    fn error(&self) -> Option<&str>;
52}