use crate::entities::packet::Packet;
use serde::{Serialize, Serializer};
use thiserror::Error;
#[derive(Error, Serialize, Debug)]
pub enum Error {
#[error("No available pipes to connect")]
NoAvailablePipes,
#[error("IPC is not connected or the pipe was closed")]
NoPipe,
#[error("An error occurred while trying to decode an incoming packet: {0}")]
DecodeError(String),
#[error("I/O Error: {0}")]
IoError(
#[serde(serialize_with = "io_err_to_string")]
#[from]
std::io::Error,
),
#[error("Bad response: {1}")]
BadResponse(Packet, String),
}
#[derive(Error, Serialize, Debug)]
#[error("Bad response: {message}")]
pub struct BadResponseError {
pub packet: Packet,
pub message: String,
}
impl From<BadResponseError> for Error {
fn from(e: BadResponseError) -> Self {
Error::BadResponse(e.packet, e.message)
}
}
fn io_err_to_string<S: Serializer>(e: &std::io::Error, ser: S) -> Result<S::Ok, S::Error> {
ser.serialize_str(&e.to_string())
}