genomicframe_core/
error.rs1use std::fmt;
4use std::io;
5
6pub type Result<T> = std::result::Result<T, Error>;
8
9#[derive(Debug)]
11pub enum Error {
12 Io(io::Error),
14
15 Parse(String),
17
18 Format(String),
20
21 InvalidInput(String),
23
24 NotImplemented(String),
26
27 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 pub fn invalid_input<S: Into<String>>(msg: S) -> Self {
56 Error::InvalidInput(msg.into())
57 }
58
59 pub fn parse<S: Into<String>>(msg: S) -> Self {
61 Error::Parse(msg.into())
62 }
63
64 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}