1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
use crate::{CyphalError, CyphalResult};
/// The priority level of a transmission
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub enum Priority {
/// Exceptional is the highest priority level and should only be sent when a total system failure has occurred.
Exceptional = 0,
/// Immediate is a high priority message.
Immediate = 1,
/// Fast is high priority messages but have looser latency requirements than `Immediate` messages.
Fast = 2,
/// High priority messages are more important than `Nominal` messages but have looser latency requirements than `Fast` messages.
High = 3,
/// This is what all messages should use by default. Specifically the heartbeat messages should use this priority.
Nominal = 4,
/// Low priority messages are expected to be sent on a bus under all conditions but cannot prevent the delivery of nominal messages.
Low = 5,
/// Slow messages are low priority messages that have no time sensitivity at all.
Slow = 6,
/// These messages might never be sent (theoretically) for some possible system states.
Optional = 7,
}
impl From<Priority> for u8 {
fn from(priority: Priority) -> Self {
priority as u8
}
}
impl TryFrom<u8> for Priority {
type Error = CyphalError;
fn try_from(value: u8) -> CyphalResult<Self> {
match value {
0 => Ok(Priority::Exceptional),
1 => Ok(Priority::Immediate),
2 => Ok(Priority::Fast),
3 => Ok(Priority::High),
4 => Ok(Priority::Nominal),
5 => Ok(Priority::Low),
6 => Ok(Priority::Slow),
7 => Ok(Priority::Optional),
_ => Err(CyphalError::OutOfRange),
}
}
}
#[cfg(test)]
mod test {
extern crate std;
use std::collections::HashMap;
use super::Priority;
fn create_priorities_hashmap() -> HashMap<Priority, u8> {
// Arrange
let mut values: HashMap<Priority, u8> = HashMap::new();
values.insert(Priority::Exceptional, 0);
values.insert(Priority::Immediate, 1);
values.insert(Priority::Fast, 2);
values.insert(Priority::High, 3);
values.insert(Priority::Nominal, 4);
values.insert(Priority::Low, 5);
values.insert(Priority::Slow, 6);
values.insert(Priority::Optional, 7);
values
}
#[test]
fn u8_from_priority() {
// Arrange
let values = create_priorities_hashmap();
for (priority, value) in values.iter() {
// Act
let target = u8::from(*priority);
// Assert
assert_eq!(target, *value);
}
}
#[test]
fn priority_from_u8_valid() {
// Arrange
let values = create_priorities_hashmap();
for (priority, value) in values.iter() {
// Act
let target = Priority::try_from(*value);
// Assert
assert!(target.is_ok());
assert_eq!(target.unwrap(), *priority);
}
}
}