moq_transport/message/
subscribe_update.rsuse crate::coding::{Decode, DecodeError, Encode, EncodeError, Params};
use crate::message::subscribe::{SubscribeLocation, SubscribePair};
use crate::message::FilterType;
use crate::message::GroupOrder;
#[derive(Clone, Debug)]
pub struct SubscribeUpdate {
pub id: u64,
pub track_alias: u64, pub track_namespace: String,
pub track_name: String,
pub subscriber_priority: u8,
pub group_order: GroupOrder,
pub filter_type: FilterType,
pub start: Option<SubscribePair>, pub end: Option<SubscribePair>, pub params: Params,
}
impl Decode for SubscribeUpdate {
fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
let id = u64::decode(r)?;
let track_alias = u64::decode(r)?;
let track_namespace = String::decode(r)?;
let track_name = String::decode(r)?;
let subscriber_priority = u8::decode(r)?;
let group_order = GroupOrder::decode(r)?;
let filter_type = FilterType::decode(r)?;
let start: Option<SubscribePair>;
let end: Option<SubscribePair>;
match filter_type {
FilterType::AbsoluteStart => {
if r.remaining() < 2 {
return Err(DecodeError::MissingField);
}
start = Some(SubscribePair::decode(r)?);
end = None;
}
FilterType::AbsoluteRange => {
if r.remaining() < 4 {
return Err(DecodeError::MissingField);
}
start = Some(SubscribePair::decode(r)?);
end = Some(SubscribePair::decode(r)?);
}
_ => {
start = None;
end = None;
}
}
if let Some(s) = &start {
if s.group == SubscribeLocation::None && s.object != SubscribeLocation::None {
return Err(DecodeError::InvalidSubscribeLocation);
}
}
if let Some(e) = &end {
if e.group == SubscribeLocation::None && e.object != SubscribeLocation::None {
return Err(DecodeError::InvalidSubscribeLocation);
}
}
let params = Params::decode(r)?;
Ok(Self {
id,
track_alias,
track_namespace,
track_name,
subscriber_priority,
group_order,
filter_type,
start,
end,
params,
})
}
}
impl Encode for SubscribeUpdate {
fn encode<W: bytes::BufMut>(&self, w: &mut W) -> Result<(), EncodeError> {
self.id.encode(w)?;
self.track_alias.encode(w)?;
self.track_namespace.encode(w)?;
self.track_name.encode(w)?;
self.subscriber_priority.encode(w)?;
self.group_order.encode(w)?;
self.filter_type.encode(w)?;
if self.filter_type == FilterType::AbsoluteStart || self.filter_type == FilterType::AbsoluteRange {
if self.start.is_none() || self.end.is_none() {
return Err(EncodeError::MissingField);
}
if let Some(start) = &self.start {
start.encode(w)?;
}
if let Some(end) = &self.end {
end.encode(w)?;
}
}
self.params.encode(w)?;
Ok(())
}
}