a653rs_linux_core/
health_event.rs

1//! Fetch information from a partition
2use a653rs::prelude::OperatingMode;
3use log::Level;
4use serde::{Deserialize, Serialize};
5
6use crate::error::SystemError;
7
8#[derive(Debug, Clone, Deserialize, Serialize)]
9/// The core unit for communication in that module
10pub enum PartitionCall {
11    /// The status of the partition
12    Transition(OperatingMode),
13    /// Potential errors
14    Error(SystemError),
15    /// Potential messages
16    Message(String),
17}
18
19impl PartitionCall {
20    /// Prints debugs, warnings, traces and errors to their accompanying streams
21    // TODO: Somehow comment what is going on inside this beast
22    pub fn print_partition_log(&self, name: &str) {
23        let name = &format!("Partition: {name}");
24        match self {
25            PartitionCall::Error(e) => error!(target: name, "{e:?}"),
26            PartitionCall::Message(msg) => {
27                let mut msg_chars = msg.chars();
28                if let Some(level) = msg_chars.next() {
29                    let msg = msg_chars.collect::<String>();
30                    if let Ok(level) = level.to_string().parse::<usize>() {
31                        return match level {
32                            l if l == Level::Debug as usize => debug!(target: name, "{msg}"),
33                            l if l == Level::Warn as usize => warn!(target: name, "{msg}"),
34                            l if l == Level::Trace as usize => trace!(target: name, "{msg}"),
35                            l if l == Level::Error as usize => error!(target: name, "{msg}"),
36                            _ => info!(target: name, "{msg}"),
37                        };
38                    }
39                }
40                info!(target: name, "{msg}")
41            }
42            PartitionCall::Transition(mode) => {
43                debug!(target: name, "Received Transition Request: {mode:?}")
44            }
45        }
46    }
47}