Skip to main content

moq_transport/data/
fetch.rs

1use crate::coding::{Decode, DecodeError, Encode, EncodeError, KeyValuePairs};
2use crate::data::{ObjectStatus, StreamHeaderType};
3
4#[derive(Debug, Clone, Eq, PartialEq)]
5pub struct FetchHeader {
6    /// Subgroup Header Type
7    pub header_type: StreamHeaderType,
8
9    /// The fetch request Id number
10    pub request_id: u64,
11}
12
13// Note:  Not using the Decode trait, since we need to know the header_type to properly parse this, and it
14//        is read before knowing we need to decode this.
15impl FetchHeader {
16    pub fn decode<R: bytes::Buf>(
17        header_type: StreamHeaderType,
18        r: &mut R,
19    ) -> Result<Self, DecodeError> {
20        let request_id = u64::decode(r)?;
21
22        Ok(Self {
23            header_type,
24            request_id,
25        })
26    }
27}
28
29impl Encode for FetchHeader {
30    fn encode<W: bytes::BufMut>(&self, w: &mut W) -> Result<(), EncodeError> {
31        self.header_type.encode(w)?;
32        self.request_id.encode(w)?;
33
34        Ok(())
35    }
36}
37
38#[derive(Debug, Clone, Eq, PartialEq)]
39pub struct FetchObject {
40    /// The group sequence number
41    pub group_id: u64,
42
43    /// The subgroup sequence number
44    pub subgroup_id: u64,
45
46    /// The object sequence number
47    pub object_id: u64,
48
49    /// Publisher priority, where **smaller** values are sent first.
50    pub publisher_priority: u8,
51
52    pub extension_headers: KeyValuePairs,
53
54    pub payload_length: usize,
55
56    pub status: Option<ObjectStatus>,
57    //pub payload: bytes::Bytes,  // TODO SLG - payload is sent outside this right now - decide which way to go
58}
59
60impl Decode for FetchObject {
61    fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
62        let group_id = u64::decode(r)?;
63        let subgroup_id = u64::decode(r)?;
64        let object_id = u64::decode(r)?;
65        let publisher_priority = u8::decode(r)?;
66        let extension_headers = KeyValuePairs::decode(r)?;
67        let payload_length = usize::decode(r)?;
68        let status = match payload_length {
69            0 => Some(ObjectStatus::decode(r)?),
70            _ => None,
71        };
72
73        //Self::decode_remaining(r, payload_length);
74        //let payload = r.copy_to_bytes(payload_length);
75
76        Ok(Self {
77            group_id,
78            subgroup_id,
79            object_id,
80            publisher_priority,
81            extension_headers,
82            payload_length,
83            status,
84            //payload,
85        })
86    }
87}
88
89impl Encode for FetchObject {
90    fn encode<W: bytes::BufMut>(&self, w: &mut W) -> Result<(), EncodeError> {
91        self.group_id.encode(w)?;
92        self.subgroup_id.encode(w)?;
93        self.object_id.encode(w)?;
94        self.publisher_priority.encode(w)?;
95        self.extension_headers.encode(w)?;
96        self.payload_length.encode(w)?;
97        if self.payload_length == 0 {
98            if let Some(status) = self.status {
99                status.encode(w)?;
100            } else {
101                return Err(EncodeError::MissingField("Status".to_string()));
102            }
103        }
104        //Self::encode_remaining(w, self.payload.len())?;
105        //w.put_slice(&self.payload);
106
107        Ok(())
108    }
109}
110
111// TODO SLG - add unit tests