procref 0.1.0

Cross-platform process reference counting for shared service lifecycle management
Documentation
//! Error types for procref.

use std::fmt;

/// Result type alias for procref operations.
pub type Result<T> = std::result::Result<T, Error>;

/// Errors that can occur in procref operations.
#[derive(Debug)]
pub enum Error {
    /// Failed to create or access the reference counter.
    RefCounterInit(String),

    /// Failed to acquire a reference.
    Acquire(String),

    /// Failed to release a reference.
    Release(String),

    /// Failed to get the current count.
    Count(String),

    /// Failed to acquire or release startup lock.
    Lock(String),

    /// Service startup failed.
    ServiceStart(String),

    /// Service is not healthy.
    ServiceUnhealthy(String),

    /// Service recovery failed.
    ServiceRecovery(String),

    /// Service info file I/O error.
    ServiceInfo(String),

    /// Platform-specific error.
    Platform(String),

    /// Generic I/O error.
    Io(std::io::Error),

    /// Other errors.
    Other(String),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::RefCounterInit(msg) => write!(f, "RefCounter initialization failed: {}", msg),
            Error::Acquire(msg) => write!(f, "Failed to acquire reference: {}", msg),
            Error::Release(msg) => write!(f, "Failed to release reference: {}", msg),
            Error::Count(msg) => write!(f, "Failed to get count: {}", msg),
            Error::Lock(msg) => write!(f, "Lock operation failed: {}", msg),
            Error::ServiceStart(msg) => write!(f, "Service startup failed: {}", msg),
            Error::ServiceUnhealthy(msg) => write!(f, "Service is unhealthy: {}", msg),
            Error::ServiceRecovery(msg) => write!(f, "Service recovery failed: {}", msg),
            Error::ServiceInfo(msg) => write!(f, "Service info error: {}", msg),
            Error::Platform(msg) => write!(f, "Platform error: {}", msg),
            Error::Io(e) => write!(f, "I/O error: {}", e),
            Error::Other(msg) => write!(f, "{}", msg),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Io(e) => Some(e),
            _ => None,
        }
    }
}

impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Error::Io(e)
    }
}

impl From<String> for Error {
    fn from(s: String) -> Self {
        Error::Other(s)
    }
}

impl From<&str> for Error {
    fn from(s: &str) -> Self {
        Error::Other(s.to_string())
    }
}