bio_seq/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use core::fmt;
use core::result;

#[derive(Debug, PartialEq)]
pub enum ParseBioError {
    UnrecognisedBase(u8),
    MismatchedLength(usize, usize),
    SequenceTooLong(usize, usize),
}

pub type Result<T> = result::Result<T, ParseBioError>;

impl fmt::Display for ParseBioError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ParseBioError::UnrecognisedBase(byte) => {
                if byte.is_ascii_alphanumeric() {
                    write!(
                        f,
                        "Unrecognised character: '{}' ({byte:#04X?})",
                        *byte as char,
                    )
                } else {
                    write!(f, "Unrecognised character: {byte:#04X?}")
                }
            }

            ParseBioError::MismatchedLength(got, expected) => {
                write!(f, "Expected length {expected}, got {got}")
            }

            ParseBioError::SequenceTooLong(got, expected) => {
                write!(f, "Expected length <= {expected}, got {got}")
            }
        }
    }
}

impl From<ParseBioError> for std::io::Error {
    fn from(err: ParseBioError) -> Self {
        std::io::Error::new(std::io::ErrorKind::Other, err)
    }
}

// #![feature(error_in_core)
impl std::error::Error for ParseBioError {}