moq_transfork/message/
subscribe.rsuse std::time;
use crate::{
coding::{Decode, DecodeError, Encode},
message::group,
Path,
};
#[derive(Clone, Debug)]
pub struct Subscribe {
pub id: u64,
pub path: Path,
pub priority: i8,
pub group_order: group::GroupOrder,
pub group_expires: time::Duration,
pub group_min: Option<u64>,
pub group_max: Option<u64>,
}
impl Decode for Subscribe {
fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
let id = u64::decode_more(r, 6)?;
let path = Path::decode_more(r, 5)?;
let priority = i8::decode_more(r, 4)?;
let group_order = group::GroupOrder::decode_more(r, 3)?;
let group_expires = time::Duration::decode_more(r, 2)?;
let group_min = match u64::decode_more(r, 1)? {
0 => None,
n => Some(n - 1),
};
let group_max = match u64::decode(r)? {
0 => None,
n => Some(n - 1),
};
Ok(Self {
id,
path,
priority,
group_order,
group_expires,
group_min,
group_max,
})
}
}
impl Encode for Subscribe {
fn encode<W: bytes::BufMut>(&self, w: &mut W) {
self.id.encode(w);
self.path.encode(w);
self.priority.encode(w);
self.group_order.encode(w);
self.group_expires.encode(w);
self.group_min.map(|v| v + 1).unwrap_or(0).encode(w);
self.group_max.map(|v| v + 1).unwrap_or(0).encode(w);
}
}
#[derive(Clone, Debug)]
pub struct SubscribeUpdate {
pub priority: u64,
pub group_order: group::GroupOrder,
pub group_expires: time::Duration,
pub group_min: Option<u64>,
pub group_max: Option<u64>,
}
impl Decode for SubscribeUpdate {
fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
let priority = u64::decode_more(r, 4)?;
let group_order = group::GroupOrder::decode_more(r, 3)?;
let group_expires = time::Duration::decode_more(r, 2)?;
let group_min = match u64::decode_more(r, 1)? {
0 => None,
n => Some(n - 1),
};
let group_max = match u64::decode(r)? {
0 => None,
n => Some(n - 1),
};
Ok(Self {
priority,
group_order,
group_expires,
group_min,
group_max,
})
}
}
impl Encode for SubscribeUpdate {
fn encode<W: bytes::BufMut>(&self, w: &mut W) {
self.priority.encode(w);
self.group_order.encode(w);
self.group_min.map(|v| v + 1).unwrap_or(0).encode(w);
self.group_max.map(|v| v + 1).unwrap_or(0).encode(w);
}
}