use crate::error::PfcpError;
use crate::ie::{Ie, IeType};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum Priority {
Active = 0,
Standby = 1,
NoPriority = 2,
High = 3,
Low = 4,
Primary = 5,
Secondary = 6,
}
impl Priority {
pub fn marshal(&self) -> [u8; 1] {
[*self as u8]
}
pub fn unmarshal(data: &[u8]) -> Result<Self, PfcpError> {
if data.is_empty() {
return Err(PfcpError::invalid_length(
"Priority",
IeType::Priority,
1,
0,
));
}
match data[0] & 0x0F {
0 => Ok(Priority::Active),
1 => Ok(Priority::Standby),
2 => Ok(Priority::NoPriority),
3 => Ok(Priority::High),
4 => Ok(Priority::Low),
5 => Ok(Priority::Primary),
6 => Ok(Priority::Secondary),
v => Err(PfcpError::invalid_value(
"Priority",
v.to_string(),
"must be 0-6",
)),
}
}
pub fn to_ie(&self) -> Ie {
Ie::new(IeType::Priority, self.marshal().to_vec())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_marshal_unmarshal() {
for p in [
Priority::Active,
Priority::Standby,
Priority::NoPriority,
Priority::High,
Priority::Low,
Priority::Primary,
Priority::Secondary,
] {
let parsed = Priority::unmarshal(&p.marshal()).unwrap();
assert_eq!(parsed, p);
}
}
#[test]
fn test_unmarshal_empty() {
assert!(matches!(
Priority::unmarshal(&[]),
Err(PfcpError::InvalidLength { .. })
));
}
#[test]
fn test_unmarshal_invalid() {
assert!(matches!(
Priority::unmarshal(&[10]),
Err(PfcpError::InvalidValue { .. })
));
}
#[test]
fn test_to_ie() {
assert_eq!(Priority::Active.to_ie().ie_type, IeType::Priority);
}
}