aquarium_control/water/refill_error_states.rs
1/* Copyright 2024 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*/
9use std::fmt;
10
11/// Contains the error flags for the refill component.
12pub struct RefillErrorStates {
13 pub error_switch_stuck_high: bool,
14 pub error_switch_stuck_low: bool,
15 pub error_receive_tank_level_switch_position: bool,
16 pub error_sql_update_failed: bool,
17 pub error_sql_get_historic_data_failed: bool,
18}
19
20impl RefillErrorStates {
21 /// Resets all internal error flags to `false`.
22 ///
23 /// This method clears all error conditions within the `RefillErrors` struct,
24 /// effectively setting the refill system back to a state where no current
25 /// errors are actively reported. This is typically invoked after the user having addressed
26 /// the root cause of an error.
27 pub fn reset_all_errors(&mut self) {
28 self.error_switch_stuck_high = false;
29 self.error_switch_stuck_low = false;
30 self.error_receive_tank_level_switch_position = false;
31 self.error_sql_update_failed = false;
32 self.error_sql_get_historic_data_failed = false;
33 }
34
35 #[cfg(test)]
36 // Checks if any error flag within the struct is currently set to `true`.
37 //
38 // This helper function is used exclusively in test environments to quickly
39 // determine if the `Refill` module has encountered any error conditions.
40 //
41 // # Returns
42 // `true` if at least one error flag (e.g., `error_switch_stuck_high`, `error_sql_update_failed`)
43 // is set to `true`; otherwise, returns `false`.
44 pub fn has_error(&mut self) -> bool {
45 self.error_switch_stuck_high
46 || self.error_switch_stuck_low
47 || self.error_receive_tank_level_switch_position
48 || self.error_sql_update_failed
49 || self.error_sql_get_historic_data_failed
50 }
51}
52
53impl fmt::Display for RefillErrorStates {
54 /// Formats the `RefillErrorStates` struct into a concise, single-line string for development purposes.
55 ///
56 /// This implementation is designed for quick inspection of the refill control's
57 /// error states. It displays the boolean value of each error flag, making it
58 /// easy to see which specific error conditions are currently active.
59 ///
60 /// # Arguments
61 /// * `f` - A mutable reference to the formatter, as required by the `fmt::Display` trait.
62 ///
63 /// # Returns
64 /// An empty `Result` (`Ok(())`) on successful formatting.
65 ///
66 /// # Errors
67 /// Returns a `fmt::Error` if an I/O error occurs while writing to the formatter.
68 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69 write!(f,
70 "error_switch_stuck_high: {}, error_switch_stuck_low: {}, error_receive_tank_level_switch_position: {}, error_sql_update_failed: {}, error_sql_get_historic_data_failed: {}",
71 self.error_switch_stuck_high,
72 self.error_switch_stuck_low,
73 self.error_receive_tank_level_switch_position,
74 self.error_sql_update_failed,
75 self.error_sql_get_historic_data_failed,
76 )
77 }
78}