use crate::error::PfcpError;
use crate::ie::pfd_contents::PfdContents;
use crate::ie::{Ie, IeType};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PfdContext {
pub pfd_contents: Vec<PfdContents>,
}
impl PfdContext {
pub fn new(pfd_contents: Vec<PfdContents>) -> Self {
PfdContext { pfd_contents }
}
pub fn marshal(&self) -> Vec<u8> {
let mut data = Vec::new();
for contents in &self.pfd_contents {
data.extend_from_slice(&contents.to_ie().marshal());
}
data
}
pub fn unmarshal(payload: &[u8]) -> Result<Self, PfcpError> {
let mut pfd_contents = Vec::new();
let mut offset = 0;
while offset < payload.len() {
let ie = Ie::unmarshal(&payload[offset..])?;
if ie.ie_type == IeType::PfdContents {
pfd_contents.push(PfdContents::unmarshal(&ie.payload)?);
}
offset += ie.len() as usize;
}
Ok(PfdContext { pfd_contents })
}
pub fn to_ie(&self) -> Ie {
Ie::new(IeType::PfdContext, self.marshal())
}
}