feat(domain): implement RelayController trait and error handling
Add RelayController async trait (T030) defining interface for Modbus relay operations with methods for read/write state, bulk operations, connection checks, and firmware queries. Implement ControllerError enum (T031) with variants for connection failures, timeouts, Modbus exceptions, and invalid relay IDs. Provide MockRelayController (T029) in-memory implementation using Arc<Mutex<HashMap>> for thread-safe state storage with timeout simulation for error handling tests. Add RelayLabelRepository trait abstraction. Ref: T029 T030 T031 (specs/001-modbus-relay-control/tasks.md)
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
use super::types::RelayId;
|
||||
use async_trait::async_trait;
|
||||
|
||||
use super::types::{RelayId, RelayLabel};
|
||||
|
||||
/// Errors that can occur during repository operations.
|
||||
///
|
||||
@@ -13,3 +15,39 @@ pub enum RepositoryError {
|
||||
#[error("Relay not found: {0}")]
|
||||
NotFound(RelayId),
|
||||
}
|
||||
|
||||
/// Repository trait for persisting and retrieving relay labels.
|
||||
///
|
||||
/// This trait abstracts data persistence operations for relay labels,
|
||||
/// enabling different storage implementations (e.g., `SQLite`, `PostgreSQL`, in-memory).
|
||||
/// Implementations must be thread-safe (`Send + Sync`) for use in async contexts.
|
||||
#[async_trait]
|
||||
pub trait RelayLabelRepository: Send + Sync {
|
||||
/// Retrieves the label for a specific relay.
|
||||
///
|
||||
/// Returns `None` if no label has been set for the relay.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns `RepositoryError::DatabaseError` if the database operation fails.
|
||||
async fn get_label(&self, id: RelayId) -> Result<Option<RelayLabel>, RepositoryError>;
|
||||
|
||||
/// Saves or updates the label for a specific relay.
|
||||
///
|
||||
/// If a label already exists for the relay, it will be overwritten.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns `RepositoryError::DatabaseError` if the database operation fails.
|
||||
async fn save_label(&self, id: RelayId, label: RelayLabel) -> Result<(), RepositoryError>;
|
||||
|
||||
/// Retrieves all relay labels from the repository.
|
||||
///
|
||||
/// Returns a vector of tuples containing relay IDs and their corresponding labels.
|
||||
/// Relays without labels are not included in the result.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns `RepositoryError::DatabaseError` if the database operation fails.
|
||||
async fn get_all_labels(&self) -> Result<Vec<(RelayId, RelayLabel)>, RepositoryError>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user