Skip to main content

iggy_binary_protocol/requests/streams/
get_stream.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::WireError;
19use crate::WireIdentifier;
20use crate::codec::{WireDecode, WireEncode};
21use bytes::BytesMut;
22
23/// `GetStream` request. Wire format: `[identifier]`
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct GetStreamRequest {
26    pub stream_id: WireIdentifier,
27}
28
29impl WireEncode for GetStreamRequest {
30    fn encoded_size(&self) -> usize {
31        self.stream_id.encoded_size()
32    }
33
34    fn encode(&self, buf: &mut BytesMut) {
35        self.stream_id.encode(buf);
36    }
37}
38
39impl WireDecode for GetStreamRequest {
40    fn decode(buf: &[u8]) -> Result<(Self, usize), WireError> {
41        let (stream_id, consumed) = WireIdentifier::decode(buf)?;
42        Ok((Self { stream_id }, consumed))
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn roundtrip_numeric() {
52        let req = GetStreamRequest {
53            stream_id: WireIdentifier::numeric(42),
54        };
55        let bytes = req.to_bytes();
56        let (decoded, consumed) = GetStreamRequest::decode(&bytes).unwrap();
57        assert_eq!(consumed, bytes.len());
58        assert_eq!(decoded, req);
59    }
60
61    #[test]
62    fn roundtrip_named() {
63        let req = GetStreamRequest {
64            stream_id: WireIdentifier::named("my-stream").unwrap(),
65        };
66        let bytes = req.to_bytes();
67        let (decoded, consumed) = GetStreamRequest::decode(&bytes).unwrap();
68        assert_eq!(consumed, bytes.len());
69        assert_eq!(decoded, req);
70    }
71
72    #[test]
73    fn wire_compat_numeric_byte_layout() {
74        let req = GetStreamRequest {
75            stream_id: WireIdentifier::numeric(1),
76        };
77        let bytes = req.to_bytes();
78        assert_eq!(&bytes[..], &[1, 4, 1, 0, 0, 0]);
79    }
80}