exon_fasta/
error.rs

1// Copyright 2023 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::{error::Error, fmt::Display, str::Utf8Error};
16
17use arrow::error::ArrowError;
18
19/// An error returned when reading a FASTA file fails for some reason.
20#[derive(Debug)]
21pub enum ExonFASTAError {
22    InvalidDefinition(String),
23    InvalidRecord(String),
24    ArrowError(ArrowError),
25    IOError(std::io::Error),
26    ParseError(String),
27    ArrayBuilderError(String),
28    InvalidNucleotide(u8),
29    InvalidAminoAcid(u8),
30    InvalidSequenceDataType(String),
31}
32
33impl Display for ExonFASTAError {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        match self {
36            ExonFASTAError::InvalidDefinition(msg) => write!(f, "Invalid definition: {}", msg),
37            ExonFASTAError::InvalidRecord(msg) => write!(f, "Invalid record: {}", msg),
38            ExonFASTAError::ArrowError(error) => write!(f, "Arrow error: {}", error),
39            ExonFASTAError::IOError(error) => write!(f, "IO error: {}", error),
40            ExonFASTAError::ParseError(msg) => write!(f, "Parse error: {}", msg),
41            ExonFASTAError::ArrayBuilderError(msg) => write!(f, "Array builder error: {}", msg),
42            ExonFASTAError::InvalidNucleotide(nucleotide) => {
43                write!(f, "Invalid nucleotide: {}", nucleotide)
44            }
45            ExonFASTAError::InvalidAminoAcid(amino_acid) => {
46                write!(
47                    f,
48                    "Invalid amino acid: {}",
49                    std::char::from_u32(*amino_acid as u32).unwrap()
50                )
51            }
52            ExonFASTAError::InvalidSequenceDataType(data_type) => {
53                write!(f, "Invalid sequence data type: {}", data_type)
54            }
55        }
56    }
57}
58
59impl Error for ExonFASTAError {}
60
61impl From<std::io::Error> for ExonFASTAError {
62    fn from(error: std::io::Error) -> Self {
63        ExonFASTAError::IOError(error)
64    }
65}
66
67impl From<noodles::fasta::record::definition::ParseError> for ExonFASTAError {
68    fn from(error: noodles::fasta::record::definition::ParseError) -> Self {
69        ExonFASTAError::ParseError(error.to_string())
70    }
71}
72
73impl From<ArrowError> for ExonFASTAError {
74    fn from(error: ArrowError) -> Self {
75        ExonFASTAError::ArrowError(error)
76    }
77}
78
79impl From<ExonFASTAError> for ArrowError {
80    fn from(error: ExonFASTAError) -> Self {
81        ArrowError::ExternalError(Box::new(error))
82    }
83}
84
85impl From<Utf8Error> for ExonFASTAError {
86    fn from(error: Utf8Error) -> Self {
87        ExonFASTAError::ParseError(error.to_string())
88    }
89}
90
91pub type ExonFASTAResult<T, E = ExonFASTAError> = std::result::Result<T, E>;