Skip to main content

genomicframe_core/
error.rs

1//! Error types for genomicframe-core
2
3use std::fmt;
4use std::io;
5
6/// Result type alias for genomicframe-core operations
7pub type Result<T> = std::result::Result<T, Error>;
8
9/// Unified error type for all genomics operations
10#[derive(Debug)]
11pub enum Error {
12    /// I/O errors (file not found, permission denied, etc.)
13    Io(io::Error),
14
15    /// Parse errors (malformed VCF, invalid FASTA, etc.)
16    Parse(String),
17
18    /// Format-specific errors
19    Format(String),
20
21    /// Invalid input or parameter
22    InvalidInput(String),
23
24    /// Feature not yet implemented
25    NotImplemented(String),
26
27    /// Other errors with custom message
28    Other(String),
29}
30
31impl fmt::Display for Error {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            Error::Io(err) => write!(f, "I/O error: {}", err),
35            Error::Parse(msg) => write!(f, "Parse error: {}", msg),
36            Error::Format(msg) => write!(f, "Format error: {}", msg),
37            Error::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
38            Error::NotImplemented(msg) => write!(f, "Not implemented: {}", msg),
39            Error::Other(msg) => write!(f, "Error: {}", msg),
40        }
41    }
42}
43
44impl std::error::Error for Error {
45    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
46        match self {
47            Error::Io(err) => Some(err),
48            _ => None,
49        }
50    }
51}
52
53impl Error {
54    /// Create an invalid input error
55    pub fn invalid_input<S: Into<String>>(msg: S) -> Self {
56        Error::InvalidInput(msg.into())
57    }
58
59    /// Create a parse error
60    pub fn parse<S: Into<String>>(msg: S) -> Self {
61        Error::Parse(msg.into())
62    }
63
64    /// Create a format error
65    pub fn format<S: Into<String>>(msg: S) -> Self {
66        Error::Format(msg.into())
67    }
68}
69
70impl From<io::Error> for Error {
71    fn from(err: io::Error) -> Self {
72        Error::Io(err)
73    }
74}
75
76impl From<String> for Error {
77    fn from(msg: String) -> Self {
78        Error::Other(msg)
79    }
80}
81
82impl From<&str> for Error {
83    fn from(msg: &str) -> Self {
84        Error::Other(msg.to_string())
85    }
86}