iggy_common/commands/users/
logout_user.rs1use 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#[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}