gemla 0.1.32

Using evolutionary computation to generate machine learning algorithms
Documentation
//! Error handling utilities and error type for the Gemla crate.
//!
//! This module defines a unified [`enum@Error`] enum for representing errors that can occur throughout the crate,
//! including I/O errors, errors from the `file_linked` crate, and general errors using `anyhow`.
//!
//! It also provides conversion implementations and a helper function for logging errors.

use log::error;
use thiserror::Error;

/// The main error type for the Gemla crate.
///
/// This enum wraps errors from dependencies and provides a unified error type for use throughout the crate.
#[derive(Error, Debug)]
pub enum Error {
    /// An error originating from the `file_linked` crate.
    #[error(transparent)]
    FileLinked(file_linked::error::Error),
    /// An I/O error.
    #[error(transparent)]
    IO(std::io::Error),
    /// Any other error, wrapped using `anyhow`.
    #[error(transparent)]
    Other(#[from] anyhow::Error),
}

/// Converts a `file_linked::error::Error` into a unified [`enum@Error`].
impl From<file_linked::error::Error> for Error {
    fn from(error: file_linked::error::Error) -> Error {
        match error {
            file_linked::error::Error::Other(e) => Error::Other(e),
            _ => Error::FileLinked(error),
        }
    }
}

/// Converts a standard I/O error into a unified [`enum@Error`].
impl From<std::io::Error> for Error {
    fn from(error: std::io::Error) -> Error {
        Error::IO(error)
    }
}

/// Logs an error using the `log` crate and returns the error.
///
/// This helper is useful for logging errors at the point of handling while still propagating them.
///
/// # Example
/// ```rust
/// use gemla::error::{log_error, Error};
/// let result: Result<(), Error> = log_error(Err(Error::Other(anyhow::anyhow!("Some error"))));
/// ```
pub fn log_error<T>(result: Result<T, Error>) -> Result<T, Error> {
    result.map_err(|e| {
        error!("{}", e);
        e
    })
}