use std::io;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum RconError {
#[error("Failed to connect to RCON server: {0}")]
ConnectionFailed(#[source] io::Error),
#[error("Authentication failed: incorrect password")]
AuthFailed,
#[error("Operation timed out after {0}ms")]
Timeout(u64),
#[error("Connection lost: {0}")]
ConnectionLost(#[source] io::Error),
#[error("Protocol error: {0}")]
ProtocolError(String),
#[error("Invalid packet: {0}")]
InvalidPacket(String),
#[error("IO error: {0}")]
Io(#[from] io::Error),
}
pub type Result<T> = std::result::Result<T, RconError>;
#[cfg(test)]
mod tests {
use super::*;
use std::io;
#[test]
fn test_error_display() {
let err = RconError::AuthFailed;
assert_eq!(err.to_string(), "Authentication failed: incorrect password");
let err = RconError::Timeout(5000);
assert_eq!(err.to_string(), "Operation timed out after 5000ms");
let err = RconError::ProtocolError("unexpected response".into());
assert_eq!(err.to_string(), "Protocol error: unexpected response");
}
#[test]
fn test_from_io_error() {
let io_err = io::Error::new(io::ErrorKind::ConnectionRefused, "refused");
let rcon_err: RconError = io_err.into();
assert!(matches!(rcon_err, RconError::Io(_)));
}
}