blob: ee530a02a92123c22a8ff6b05c1612dbf6a5ebb7 [file]
use crate::bytes_serializable::BytesSerializable;
use crate::command::CommandPayload;
use crate::error::IggyError;
use crate::validatable::Validatable;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
/// `GetStats` command is used to get the statistics about the system.
/// It has no additional payload.
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct GetStats {}
impl CommandPayload for GetStats {}
impl Validatable<IggyError> for GetStats {
fn validate(&self) -> Result<(), IggyError> {
Ok(())
}
}
impl BytesSerializable for GetStats {
fn as_bytes(&self) -> Vec<u8> {
Vec::with_capacity(0)
}
fn from_bytes(bytes: &[u8]) -> Result<GetStats, IggyError> {
if !bytes.is_empty() {
return Err(IggyError::InvalidCommand);
}
let command = GetStats {};
command.validate()?;
Ok(GetStats {})
}
}
impl Display for GetStats {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_be_serialized_as_empty_bytes() {
let command = GetStats {};
let bytes = command.as_bytes();
assert!(bytes.is_empty());
}
#[test]
fn should_be_deserialized_from_empty_bytes() {
let bytes: Vec<u8> = vec![];
let command = GetStats::from_bytes(&bytes);
assert!(command.is_ok());
}
#[test]
fn should_not_be_deserialized_from_empty_bytes() {
let bytes: Vec<u8> = vec![0];
let command = GetStats::from_bytes(&bytes);
assert!(command.is_err());
}
}