use proto::Retcode;
use thiserror::Error;

use crate::logic::game::LogicError;

pub type NetResult<T> = Result<T, NetError>;

#[derive(Error, Debug)]
pub enum PacketHandlingError {
    #[error("protobuf decode failed: {0}")]
    DecodeFailed(#[from] prost::DecodeError),
    #[error("I/O error: {0}")]
    IoError(#[from] std::io::Error),
    #[error("net error: {0}")]
    Net(#[from] NetError),
    #[error("soft logout performed")]
    Logout,
}

#[derive(Error, Debug)]
pub enum NetError {
    #[error("I/O error: {0}")]
    IoError(#[from] std::io::Error),
    #[error("logic error: {0}")]
    LogicError(#[from] LogicError),
    #[error("retcode: {0:?}")]
    ErrCode(Retcode),
    #[error("soft logout performed")]
    Logout,
}

impl From<Retcode> for NetError {
    fn from(value: Retcode) -> Self {
        Self::ErrCode(value)
    }
}
