use crate::error::PfcpError;
use crate::ie::{Ie, IeType};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PortManagementInformationContainer {
pub value: Vec<u8>,
}
impl PortManagementInformationContainer {
pub fn new(value: Vec<u8>) -> Self {
Self { value }
}
pub fn marshal(&self) -> Vec<u8> {
self.value.clone()
}
pub fn unmarshal(data: &[u8]) -> Result<Self, PfcpError> {
if data.is_empty() {
return Err(PfcpError::invalid_length(
"Port Management Information Container",
IeType::PortManagementInformationContainer,
1,
0,
));
}
Ok(Self {
value: data.to_vec(),
})
}
pub fn to_ie(&self) -> Ie {
Ie::new(IeType::PortManagementInformationContainer, self.marshal())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_marshal_unmarshal() {
let ie = PortManagementInformationContainer::new(vec![0x01, 0x02, 0x03]);
let parsed = PortManagementInformationContainer::unmarshal(&ie.marshal()).unwrap();
assert_eq!(parsed, ie);
}
#[test]
fn test_unmarshal_empty() {
assert!(matches!(
PortManagementInformationContainer::unmarshal(&[]),
Err(PfcpError::InvalidLength { .. })
));
}
#[test]
fn test_to_ie() {
let ie = PortManagementInformationContainer::new(vec![0xAB]).to_ie();
assert_eq!(ie.ie_type, IeType::PortManagementInformationContainer);
assert_eq!(ie.payload, vec![0xAB]);
}
#[test]
fn test_round_trip_various() {
for data in [vec![0x00], vec![0xFF; 10], vec![0x12, 0x34, 0x56, 0x78]] {
let original = PortManagementInformationContainer::new(data.clone());
let parsed =
PortManagementInformationContainer::unmarshal(&original.marshal()).unwrap();
assert_eq!(original, parsed);
}
}
}