discordipc 0.1.1

A Rust crate that enables connection and interaction with Discord's IPC, allowing you to set custom activities for your project.
Documentation
// SPDX-License-Identifier: MIT OR Apache-2.0

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),
}

/// Represents a bad response from Discord.
///
/// ## Fields
/// - `packet`: The packet with an erroneous payload.
/// - `message`: The error message extracted from the erroneous payload.
#[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())
}