Skip to main content

iggy_common/commands/streams/
delete_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 */
18
19use crate::BytesSerializable;
20use crate::Identifier;
21use crate::Validatable;
22use crate::error::IggyError;
23use crate::{Command, DELETE_STREAM_CODE};
24use bytes::Bytes;
25use serde::{Deserialize, Serialize};
26use std::fmt::Display;
27
28/// `DeleteStream` command is used to delete an existing stream.
29/// It has additional payload:
30/// - `stream_id` - unique stream ID (numeric or name).
31#[derive(Debug, Serialize, Deserialize, PartialEq, Default, Clone)]
32pub struct DeleteStream {
33    /// Unique stream ID (numeric or name).
34    #[serde(skip)]
35    pub stream_id: Identifier,
36}
37
38impl Command for DeleteStream {
39    fn code(&self) -> u32 {
40        DELETE_STREAM_CODE
41    }
42}
43
44impl Validatable<IggyError> for DeleteStream {
45    fn validate(&self) -> Result<(), IggyError> {
46        Ok(())
47    }
48}
49
50impl BytesSerializable for DeleteStream {
51    fn to_bytes(&self) -> Bytes {
52        self.stream_id.to_bytes()
53    }
54
55    fn from_bytes(bytes: Bytes) -> std::result::Result<DeleteStream, IggyError> {
56        if bytes.len() < 3 {
57            return Err(IggyError::InvalidCommand);
58        }
59
60        let stream_id = Identifier::from_bytes(bytes)?;
61        let command = DeleteStream { stream_id };
62        Ok(command)
63    }
64}
65
66impl Display for DeleteStream {
67    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68        write!(f, "{}", self.stream_id)
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn should_be_serialized_as_bytes() {
78        let command = DeleteStream {
79            stream_id: Identifier::numeric(1).unwrap(),
80        };
81
82        let bytes = command.to_bytes();
83        let stream_id = Identifier::from_bytes(bytes.clone()).unwrap();
84
85        assert!(!bytes.is_empty());
86        assert_eq!(stream_id, command.stream_id);
87    }
88
89    #[test]
90    fn should_be_deserialized_from_bytes() {
91        let stream_id = Identifier::numeric(1).unwrap();
92        let bytes = stream_id.to_bytes();
93        let command = DeleteStream::from_bytes(bytes);
94        assert!(command.is_ok());
95
96        let command = command.unwrap();
97        assert_eq!(command.stream_id, stream_id);
98    }
99}