rscon 0.1.0

Simple general RCON client
Documentation
// Copyright (C) 2024  Rafael Carvalho <contact@rafaelrc.com>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as published by
// the Free Software Foundation.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-only

use std::{
    error::Error,
    fmt::{self, Display},
};

#[derive(Debug)]
pub enum PacketBuildError {
    BodyTooBig(usize),
}

impl Error for PacketBuildError {}

impl Display for PacketBuildError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let error_msg = match self {
            Self::BodyTooBig(size) => format!(
                "Body size '{}' bytes exceeds the maximum of 4086 bytes.",
                size
            ),
        };

        write!(f, "{}", error_msg)
    }
}

#[derive(Debug)]
pub enum PacketDeserialiseError {
    InsuficientSize(i32),
    WrongSize { expected: i32, got: i32 },
}

impl Error for PacketDeserialiseError {}

impl Display for PacketDeserialiseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let error_msg = match self {
            Self::InsuficientSize(size) => format!(
                "Insuficient size '{}' bytes to build RCON Packet, that has a minimum size of 14 bytes.",
                size
            ),

            Self::WrongSize { expected, got } => format!(
                "Packet has a size attribute of '{}' bytes. However it actually has '{}' bytes.",
                expected, got),
        };

        write!(f, "{}", error_msg)
    }
}

#[derive(Debug)]
pub struct PacketTypeError(pub i32);

impl Error for PacketTypeError {}

impl Display for PacketTypeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
        let Self(value) = self;
        write!(f, "'{value}' represents an invalid packet type")
    }
}