genomicframe-core 0.2.0

High-performance genomics I/O and interoperability layer
Documentation
//! Error types for genomicframe-core

use std::fmt;
use std::io;

/// Result type alias for genomicframe-core operations
pub type Result<T> = std::result::Result<T, Error>;

/// Unified error type for all genomics operations
#[derive(Debug)]
pub enum Error {
    /// I/O errors (file not found, permission denied, etc.)
    Io(io::Error),

    /// Parse errors (malformed VCF, invalid FASTA, etc.)
    Parse(String),

    /// Format-specific errors
    Format(String),

    /// Invalid input or parameter
    InvalidInput(String),

    /// Feature not yet implemented
    NotImplemented(String),

    /// Other errors with custom message
    Other(String),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Io(err) => write!(f, "I/O error: {}", err),
            Error::Parse(msg) => write!(f, "Parse error: {}", msg),
            Error::Format(msg) => write!(f, "Format error: {}", msg),
            Error::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
            Error::NotImplemented(msg) => write!(f, "Not implemented: {}", msg),
            Error::Other(msg) => write!(f, "Error: {}", msg),
        }
    }
}

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

impl Error {
    /// Create an invalid input error
    pub fn invalid_input<S: Into<String>>(msg: S) -> Self {
        Error::InvalidInput(msg.into())
    }

    /// Create a parse error
    pub fn parse<S: Into<String>>(msg: S) -> Self {
        Error::Parse(msg.into())
    }

    /// Create a format error
    pub fn format<S: Into<String>>(msg: S) -> Self {
        Error::Format(msg.into())
    }
}

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

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

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