Skip to main content

iggy_common/commands/system/
get_stats.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, GET_STATS_CODE};
23use bytes::Bytes;
24use serde::{Deserialize, Serialize};
25use std::fmt::Display;
26
27/// `GetStats` command is used to get the statistics about the system.
28/// It has no additional payload.
29#[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}