-
Notifications
You must be signed in to change notification settings - Fork 9
/
subscribe_ok.rs
56 lines (45 loc) · 1.17 KB
/
subscribe_ok.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use crate::coding::{Decode, DecodeError, Encode, EncodeError};
/// Sent by the publisher to accept a Subscribe.
#[derive(Clone, Debug)]
pub struct SubscribeOk {
/// The ID for this subscription.
pub id: u64,
/// The subscription will expire in this many milliseconds.
pub expires: Option<u64>,
/// The latest group and object for the track.
pub latest: Option<(u64, u64)>,
}
impl Decode for SubscribeOk {
fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
let id = u64::decode(r)?;
let expires = match u64::decode(r)? {
0 => None,
expires => Some(expires),
};
Self::decode_remaining(r, 1)?;
let latest = match r.get_u8() {
0 => None,
1 => Some((u64::decode(r)?, u64::decode(r)?)),
_ => return Err(DecodeError::InvalidValue),
};
Ok(Self { id, expires, latest })
}
}
impl Encode for SubscribeOk {
fn encode<W: bytes::BufMut>(&self, w: &mut W) -> Result<(), EncodeError> {
self.id.encode(w)?;
self.expires.unwrap_or(0).encode(w)?;
Self::encode_remaining(w, 1)?;
match self.latest {
Some((group, object)) => {
w.put_u8(1);
group.encode(w)?;
object.encode(w)?;
}
None => {
w.put_u8(0);
}
}
Ok(())
}
}