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