aquarium_control/database/
thermal_set_value_updater_trait.rs

1/* Copyright 2025 Uwe Martin
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8*/
9
10//! Defines a generic trait for updating thermal set points from a data source.
11//!
12//! This module provides the `ThermalSetValueUpdaterTrait`, an abstraction that decouples
13//! the core thermal control logic (e.g., for heating or ventilation) from the specific
14//! implementation of how set point values are retrieved.
15//!
16//! ## Key Components
17//!
18//! - **`ThermalSetValueUpdaterTrait`**: A trait that defines a single method, `update_set_value`.
19//!   Any struct implementing this trait is responsible for fetching the latest "on" and "off"
20//!   temperature thresholds and updating the provided variables.
21//!
22//! ## Design and Purpose
23//!
24//! The primary purpose of this trait is to enable **Dependency Inversion** and improve
25//! the system's **testability** and **modularity**.
26//!
27//! - **Decoupling**: High-level components, like a `ThermalController`, do not need to know
28//!   *how* or *from where* the set points are fetched. They only need to interact with this
29//!   trait. This means the data could come from a SQL database, a configuration file, or
30//!   even a remote API without changing the controller's code.
31//!
32//! - **Testability**: This abstraction is critical for unit testing. In a test environment,
33//!   a mock implementation of `ThermalSetValueUpdaterTrait` can be created. This mock can
34//!   simulate various scenarios—such as providing fixed values, returning updated values,
35//!   or simulating database errors—all without requiring a live database connection. This
36//!   makes tests fast, reliable, and self-contained.
37
38use crate::database::sql_interface_error::SqlInterfaceError;
39
40/// Trait for updating the set values of either ventilation or heating with values from the database.
41/// This trait allows running the main control with a mock implementation for testing.
42pub trait ThermalSetValueUpdaterTrait {
43    fn update_set_value(
44        &mut self,
45        switch_off_temperature: &mut f32,
46        switch_on_temperature: &mut f32,
47    ) -> Result<(), SqlInterfaceError>;
48}