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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use std::collections::HashMap;
use std::io::Write;

#[cfg(test)]
use fake::Fake;

use crate::{
    codec::{Decoder, Encoder},
    error::{DecodeError, EncodeError},
    protocol::commands::COMMAND_SUBSCRIBE,
};

use super::Command;

#[cfg_attr(test, derive(fake::Dummy))]
#[derive(PartialEq, Eq, Debug)]
pub struct SubscribeCommand {
    correlation_id: u32,
    subscription_id: u8,
    stream_name: String,
    offset_specification: OffsetSpecification,
    credit: u16,
    properties: HashMap<String, String>,
}

impl SubscribeCommand {
    pub fn new(
        correlation_id: u32,
        subscription_id: u8,
        stream_name: String,
        offset_specification: OffsetSpecification,
        credit: u16,
        properties: HashMap<String, String>,
    ) -> Self {
        Self {
            correlation_id,
            subscription_id,
            stream_name,
            offset_specification,
            credit,
            properties,
        }
    }
}

impl Encoder for SubscribeCommand {
    fn encoded_size(&self) -> u32 {
        self.correlation_id.encoded_size()
            + self.subscription_id.encoded_size()
            + self.stream_name.as_str().encoded_size()
            + self.offset_specification.encoded_size()
            + self.credit.encoded_size()
            + self.properties.encoded_size()
    }

    fn encode(&self, writer: &mut impl Write) -> Result<(), EncodeError> {
        self.correlation_id.encode(writer)?;
        self.subscription_id.encode(writer)?;
        self.stream_name.as_str().encode(writer)?;
        self.offset_specification.encode(writer)?;
        self.credit.encode(writer)?;
        self.properties.encode(writer)?;
        Ok(())
    }
}

impl Decoder for SubscribeCommand {
    fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> {
        let (input, correlation_id) = u32::decode(input)?;
        let (input, subscription_id) = u8::decode(input)?;
        let (input, stream_name) = Option::decode(input)?;
        let (input, offset_specification) = OffsetSpecification::decode(input)?;
        let (input, credit) = u16::decode(input)?;
        let (input, properties) = HashMap::decode(input)?;

        Ok((
            input,
            SubscribeCommand {
                correlation_id,
                subscription_id,
                stream_name: stream_name.unwrap(),
                offset_specification,
                credit,
                properties,
            },
        ))
    }
}

impl Command for SubscribeCommand {
    fn key(&self) -> u16 {
        COMMAND_SUBSCRIBE
    }
}

#[cfg_attr(test, derive(fake::Dummy))]
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum OffsetSpecification {
    First,
    Last,
    Next,
    Offset(u64),
    Timestamp(i64),
}

impl OffsetSpecification {
    fn get_type(&self) -> u16 {
        match self {
            OffsetSpecification::First => 1,
            OffsetSpecification::Last => 2,
            OffsetSpecification::Next => 3,
            OffsetSpecification::Offset(_) => 4,
            OffsetSpecification::Timestamp(_) => 5,
        }
    }
}

impl Encoder for OffsetSpecification {
    fn encoded_size(&self) -> u32 {
        self.get_type().encoded_size()
            + match self {
                OffsetSpecification::Offset(offset) => offset.encoded_size(),
                OffsetSpecification::Timestamp(timestamp) => timestamp.encoded_size(),
                _ => 0,
            }
    }

    fn encode(&self, writer: &mut impl Write) -> Result<(), EncodeError> {
        self.get_type().encode(writer)?;
        match self {
            OffsetSpecification::Offset(offset) => offset.encode(writer),
            OffsetSpecification::Timestamp(timestamp) => timestamp.encode(writer),
            _ => Ok(()),
        }
    }
}

impl Decoder for OffsetSpecification {
    fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> {
        let (input, offset_type) = u16::decode(input)?;

        match offset_type {
            1 => Ok((input, OffsetSpecification::First)),
            2 => Ok((input, OffsetSpecification::Last)),
            3 => Ok((input, OffsetSpecification::Next)),
            4 => {
                let (input, offset) = u64::decode(input)?;
                Ok((input, OffsetSpecification::Offset(offset)))
            }
            5 => {
                let (input, timestamp) = i64::decode(input)?;
                Ok((input, OffsetSpecification::Timestamp(timestamp)))
            }
            _ => panic!("Offset type not supported"),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::commands::tests::command_encode_decode_test;

    use super::SubscribeCommand;

    #[test]
    fn subscribe_request_test() {
        command_encode_decode_test::<SubscribeCommand>();
    }
}