exon_sdf/
error.rs

1// Copyright 2024 WHERE TRUE Technologies.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::{
16    error::Error,
17    fmt::Display,
18    num::{ParseFloatError, ParseIntError},
19    str::Utf8Error,
20};
21
22#[derive(Debug)]
23pub enum ExonSDFError {
24    InvalidInput(String),
25    MissingDataField,
26    MissingDataFieldInSchema(String),
27    Internal(String),
28    IoError(std::io::Error),
29    ArrowError(arrow::error::ArrowError),
30    UnexpectedEndofAtomBlock,
31    FailedToParseAtom(String),
32    UnexpectedEndofBondBlock,
33    FailedToParseBond(String),
34    ParseError(String),
35    InvalidColumnIndex(usize),
36}
37
38impl Display for ExonSDFError {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        match self {
41            ExonSDFError::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
42            ExonSDFError::MissingDataField => write!(f, "Missing data field"),
43            ExonSDFError::MissingDataFieldInSchema(msg) => {
44                write!(f, "Missing data field in schema: {}", msg)
45            }
46            ExonSDFError::Internal(msg) => {
47                write!(f, "Internal error (please contact the developers): {}", msg)
48            }
49            ExonSDFError::IoError(err) => write!(f, "I/O error: {}", err),
50            ExonSDFError::ArrowError(err) => write!(f, "Arrow error: {}", err),
51            ExonSDFError::UnexpectedEndofAtomBlock => write!(f, "Unexpected end of atom block"),
52            ExonSDFError::FailedToParseAtom(msg) => write!(f, "Failed to parse atom: {}", msg),
53            ExonSDFError::UnexpectedEndofBondBlock => write!(f, "Unexpected end of bond block"),
54            ExonSDFError::FailedToParseBond(msg) => write!(f, "Failed to parse bond: {}", msg),
55            ExonSDFError::ParseError(msg) => write!(f, "Parse error: {}", msg),
56            ExonSDFError::InvalidColumnIndex(idx) => write!(f, "Invalid column index: {}", idx),
57        }
58    }
59}
60
61impl Error for ExonSDFError {}
62
63pub type Result<T> = std::result::Result<T, ExonSDFError>;
64
65impl From<std::io::Error> for ExonSDFError {
66    fn from(err: std::io::Error) -> Self {
67        ExonSDFError::Internal(err.to_string())
68    }
69}
70
71impl From<arrow::error::ArrowError> for ExonSDFError {
72    fn from(err: arrow::error::ArrowError) -> Self {
73        ExonSDFError::ArrowError(err)
74    }
75}
76
77impl From<Utf8Error> for ExonSDFError {
78    fn from(err: Utf8Error) -> Self {
79        ExonSDFError::ParseError(err.to_string())
80    }
81}
82
83impl From<ExonSDFError> for arrow::error::ArrowError {
84    fn from(err: ExonSDFError) -> Self {
85        arrow::error::ArrowError::ExternalError(Box::new(err))
86    }
87}
88
89impl From<ParseFloatError> for ExonSDFError {
90    fn from(err: ParseFloatError) -> Self {
91        ExonSDFError::ParseError(err.to_string())
92    }
93}
94
95impl From<ParseIntError> for ExonSDFError {
96    fn from(err: ParseIntError) -> Self {
97        ExonSDFError::ParseError(err.to_string())
98    }
99}