Skip to main content

iggy_common/commands/users/
logout_user.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::Validatable;
21use crate::error::IggyError;
22use crate::{Command, LOGOUT_USER_CODE};
23use bytes::Bytes;
24use serde::{Deserialize, Serialize};
25use std::fmt::Display;
26
27/// `LogoutUser` command is used to log out the authenticated user.
28/// It has no additional payload.
29#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
30pub struct LogoutUser {}
31
32impl Command for LogoutUser {
33    fn code(&self) -> u32 {
34        LOGOUT_USER_CODE
35    }
36}
37
38impl Validatable<IggyError> for LogoutUser {
39    fn validate(&self) -> Result<(), IggyError> {
40        Ok(())
41    }
42}
43
44impl BytesSerializable for LogoutUser {
45    fn to_bytes(&self) -> Bytes {
46        Bytes::new()
47    }
48
49    fn from_bytes(bytes: Bytes) -> Result<LogoutUser, IggyError> {
50        if !bytes.is_empty() {
51            return Err(IggyError::InvalidCommand);
52        }
53
54        let command = LogoutUser {};
55        Ok(command)
56    }
57}
58
59impl Display for LogoutUser {
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        write!(f, "")
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn should_be_serialized_as_empty_bytes() {
71        let command = LogoutUser {};
72        let bytes = command.to_bytes();
73        assert!(bytes.is_empty());
74    }
75
76    #[test]
77    fn should_be_deserialized_from_empty_bytes() {
78        let command = LogoutUser::from_bytes(Bytes::new());
79        assert!(command.is_ok());
80    }
81
82    #[test]
83    fn should_not_be_deserialized_from_empty_bytes() {
84        let command = LogoutUser::from_bytes(Bytes::from_static(&[0]));
85        assert!(command.is_err());
86    }
87}