exon-fasta 0.23.0

A library for reading and writing FASTA files with Exon.
Documentation
// Copyright 2023 WHERE TRUE Technologies.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{error::Error, fmt::Display, str::Utf8Error};

use arrow::error::ArrowError;

/// An error returned when reading a FASTA file fails for some reason.
#[derive(Debug)]
pub enum ExonFastaError {
    InvalidDefinition(String),
    InvalidRecord(String),
    ArrowError(ArrowError),
    IOError(std::io::Error),
    ParseError(String),
    ArrayBuilderError(String),
    InvalidNucleotide(u8),
    InvalidAminoAcid(u8),
}

impl Display for ExonFastaError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ExonFastaError::InvalidDefinition(msg) => write!(f, "Invalid definition: {}", msg),
            ExonFastaError::InvalidRecord(msg) => write!(f, "Invalid record: {}", msg),
            ExonFastaError::ArrowError(error) => write!(f, "Arrow error: {}", error),
            ExonFastaError::IOError(error) => write!(f, "IO error: {}", error),
            ExonFastaError::ParseError(msg) => write!(f, "Parse error: {}", msg),
            ExonFastaError::ArrayBuilderError(msg) => write!(f, "Array builder error: {}", msg),
            ExonFastaError::InvalidNucleotide(nucleotide) => {
                write!(f, "Invalid nucleotide: {}", nucleotide)
            }
            ExonFastaError::InvalidAminoAcid(amino_acid) => {
                write!(
                    f,
                    "Invalid amino acid: {}",
                    std::char::from_u32(*amino_acid as u32).unwrap()
                )
            }
        }
    }
}

impl Error for ExonFastaError {}

impl From<std::io::Error> for ExonFastaError {
    fn from(error: std::io::Error) -> Self {
        ExonFastaError::IOError(error)
    }
}

impl From<noodles::fasta::record::definition::ParseError> for ExonFastaError {
    fn from(error: noodles::fasta::record::definition::ParseError) -> Self {
        ExonFastaError::ParseError(error.to_string())
    }
}

impl From<ArrowError> for ExonFastaError {
    fn from(error: ArrowError) -> Self {
        ExonFastaError::ArrowError(error)
    }
}

impl From<ExonFastaError> for ArrowError {
    fn from(error: ExonFastaError) -> Self {
        ArrowError::ExternalError(Box::new(error))
    }
}

impl From<Utf8Error> for ExonFastaError {
    fn from(error: Utf8Error) -> Self {
        ExonFastaError::ParseError(error.to_string())
    }
}

pub type ExonFastaResult<T, E = ExonFastaError> = std::result::Result<T, E>;