use crate::error::PfcpError;
use crate::ie::{Ie, IeType};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SuggestedBufferingPacketsCount {
pub count: u16,
}
impl SuggestedBufferingPacketsCount {
pub fn new(count: u16) -> Self {
SuggestedBufferingPacketsCount { count }
}
pub fn marshal(&self) -> Vec<u8> {
self.count.to_be_bytes().to_vec()
}
pub fn unmarshal(payload: &[u8]) -> Result<Self, PfcpError> {
if payload.len() < 2 {
return Err(PfcpError::invalid_length(
"Suggested Buffering Packets Count",
IeType::DlBufferingSuggestedPacketCount,
2,
payload.len(),
));
}
Ok(SuggestedBufferingPacketsCount {
count: u16::from_be_bytes([payload[0], payload[1]]),
})
}
pub fn to_ie(&self) -> Ie {
Ie::new(IeType::DlBufferingSuggestedPacketCount, self.marshal())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_suggested_buffering_packets_count_marshal_unmarshal() {
let sbpc = SuggestedBufferingPacketsCount::new(1000);
let marshaled = sbpc.marshal();
let unmarshaled = SuggestedBufferingPacketsCount::unmarshal(&marshaled).unwrap();
assert_eq!(unmarshaled, sbpc);
}
#[test]
fn test_suggested_buffering_packets_count_unmarshal_short() {
let result = SuggestedBufferingPacketsCount::unmarshal(&[0]);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(matches!(err, PfcpError::InvalidLength { .. }));
assert!(err
.to_string()
.contains("Suggested Buffering Packets Count"));
}
}