sat-rs/src/core/events.rs

114 lines
3.3 KiB
Rust
Raw Normal View History

2022-06-09 16:21:07 +02:00
use num::pow;
2022-06-09 16:21:18 +02:00
#[derive(Copy, Clone, PartialEq, Debug)]
2022-06-09 16:21:07 +02:00
pub enum Severity {
INFO = 1,
LOW = 2,
MEDIUM = 3,
2022-06-09 16:21:18 +02:00
HIGH = 4,
2022-06-09 16:21:07 +02:00
}
impl TryFrom<u8> for Severity {
type Error = ();
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
x if x == Severity::INFO as u8 => Ok(Severity::INFO),
x if x == Severity::LOW as u8 => Ok(Severity::LOW),
x if x == Severity::MEDIUM as u8 => Ok(Severity::MEDIUM),
x if x == Severity::HIGH as u8 => Ok(Severity::HIGH),
_ => Err(()),
}
}
}
2022-06-09 16:21:18 +02:00
#[derive(Copy, Clone, Debug)]
2022-05-15 19:17:07 +02:00
pub struct Event {
2022-06-09 16:21:07 +02:00
severity: Severity,
group_id: u16,
unique_id: u16,
}
impl Event {
/// Generate an event. The raw representation of an event has 32 bits.
/// If the passed group ID is invalid (too large), None wil be returned
///
/// # Parameter
///
/// `severity`: Each event has a [severity][Severity]. The raw value of the severity will
/// be stored inside the uppermost 3 bits of the raw event ID
/// `group_id`: Related events can be grouped using a group ID. The group ID will occupy the
/// next 13 bits after the severity. Therefore, the size is limited by dec 8191 hex 0x1FFF.
/// `unique_id`: Each event has a unique 16 bit ID occupying the last 16 bits of the
/// raw event ID
pub fn new(severity: Severity, group_id: u16, unique_id: u16) -> Option<Event> {
if group_id > (pow::pow(2u8 as u16, 13) - 1) {
2022-06-09 16:21:18 +02:00
return None;
2022-06-09 16:21:07 +02:00
}
Some(Event {
severity,
group_id,
2022-06-09 16:21:18 +02:00
unique_id,
2022-06-09 16:21:07 +02:00
})
}
/// Retrieve the severity of an event. Returns None if that severity bit field of the raw event
/// ID is invalid
pub fn severity(&self) -> Severity {
self.severity
}
pub fn group_id(&self) -> u16 {
self.group_id
}
pub fn unique_id(&self) -> u16 {
self.unique_id
}
pub fn raw(&self) -> u32 {
2022-06-09 16:21:18 +02:00
(((self.severity as u32) << 29) as u32
| ((self.group_id as u32) << 16) as u32
| self.unique_id as u32) as u32
2022-06-09 16:21:07 +02:00
}
}
impl TryFrom<u32> for Event {
type Error = ();
fn try_from(raw: u32) -> Result<Self, Self::Error> {
let severity: Option<Severity> = (((raw >> 29) & 0b111) as u8).try_into().ok();
if severity.is_none() {
2022-06-09 16:21:18 +02:00
return Err(());
2022-06-09 16:21:07 +02:00
}
let group_id = ((raw >> 16) & 0x1FFF) as u16;
let unique_id = (raw & 0xFFFF) as u16;
Event::new(severity.unwrap(), group_id, unique_id).ok_or(())
}
2022-05-15 19:17:07 +02:00
}
2022-06-09 16:21:07 +02:00
#[cfg(test)]
mod tests {
use super::Event;
2022-06-09 16:21:18 +02:00
use crate::core::events::Severity;
2022-06-09 16:21:07 +02:00
#[test]
fn test_events() {
let event = Event::new(Severity::INFO, 0, 0).unwrap();
assert_eq!(event.severity(), Severity::INFO);
assert_eq!(event.unique_id(), 0);
assert_eq!(event.group_id(), 0);
let raw_event = event.raw();
assert_eq!(raw_event, 0x20000000);
let conv_from_raw = Event::try_from(raw_event);
assert!(conv_from_raw.is_ok());
let opt_event = conv_from_raw.ok();
assert!(opt_event.is_some());
let event_conv_back = opt_event.unwrap();
assert_eq!(event_conv_back.severity(), Severity::INFO);
assert_eq!(event_conv_back.unique_id(), 0);
assert_eq!(event_conv_back.group_id(), 0);
}
2022-06-09 16:21:18 +02:00
}