use crate::error::PfcpError;
use crate::ie::IeType;
use bitflags::bitflags;
bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct CPFunctionFeatures: u8 {
const LOAD = 1 << 0; const OVRL = 1 << 1; const EPCO = 1 << 2; const DDEX = 1 << 3; const PFDL = 1 << 4; const APDP = 1 << 5; const PFDC = 1 << 6; }
}
impl CPFunctionFeatures {
pub fn new(features: u8) -> Self {
CPFunctionFeatures::from_bits_truncate(features)
}
pub fn marshal(&self) -> [u8; 1] {
self.bits().to_be_bytes()
}
pub fn unmarshal(data: &[u8]) -> Result<Self, PfcpError> {
if data.is_empty() {
return Err(PfcpError::invalid_length(
"CP Function Features",
IeType::CpFunctionFeatures,
1,
0,
));
}
Ok(CPFunctionFeatures::from_bits_truncate(data[0]))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cp_function_features_marshal_unmarshal() {
let features = CPFunctionFeatures::LOAD | CPFunctionFeatures::EPCO;
let marshaled = features.marshal();
let unmarshaled = CPFunctionFeatures::unmarshal(&marshaled).unwrap();
assert_eq!(features, unmarshaled);
}
#[test]
fn test_cp_function_features_unmarshal_empty() {
let result = CPFunctionFeatures::unmarshal(&[]);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(matches!(err, PfcpError::InvalidLength { .. }));
assert!(err.to_string().contains("CP Function Features"));
}
}