mechutil 0.8.1

Utility structures and functions for mechatronics applications.
Documentation
//
// Copyright (C) 2024 - 2025 Automated Design Corp. All Rights Reserved.
//

//! Error types for IPC module.

use thiserror::Error;

/// Errors that can occur during IPC operations.
#[derive(Error, Debug)]
pub enum IpcError {
    /// Connection failed or was lost
    #[error("Connection error: {0}")]
    Connection(String),

    /// Failed to serialize/deserialize message
    #[error("Serialization error: {0}")]
    Serialization(String),

    /// Message framing error (invalid length, corrupted data)
    #[error("Framing error: {0}")]
    Framing(String),

    /// Timeout waiting for response
    #[error("Timeout: {0}")]
    Timeout(String),

    /// Invalid message type or format
    #[error("Invalid message: {0}")]
    InvalidMessage(String),

    /// Handler returned an error
    #[error("Handler error: {0}")]
    Handler(String),

    /// IO error from underlying transport
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    /// Channel closed or send/receive failed
    #[error("Channel error: {0}")]
    Channel(String),

    /// Module not found or not registered
    #[error("Module not found: {0}")]
    ModuleNotFound(String),

    /// Server is shutting down
    #[error("Server shutdown")]
    Shutdown,
}

impl From<postcard::Error> for IpcError {
    fn from(err: postcard::Error) -> Self {
        IpcError::Serialization(err.to_string())
    }
}

impl From<serde_json::Error> for IpcError {
    fn from(err: serde_json::Error) -> Self {
        IpcError::Serialization(err.to_string())
    }
}

impl From<tokio::sync::broadcast::error::SendError<super::command_message::CommandMessage>> for IpcError {
    fn from(err: tokio::sync::broadcast::error::SendError<super::command_message::CommandMessage>) -> Self {
        IpcError::Channel(err.to_string())
    }
}

impl<T> From<tokio::sync::mpsc::error::SendError<T>> for IpcError {
    fn from(err: tokio::sync::mpsc::error::SendError<T>) -> Self {
        IpcError::Channel(err.to_string())
    }
}