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
//! Common error and result handling facilities
use std::io::{Error as IoError};
use std::num::TryFromIntError;
use nom::{error::ErrorKind, Err as NomError};
use std::borrow::Cow;

#[derive(Debug)]
pub enum FileError {
    Read(IoError),
    Seek(IoError),
    Count(TryFromIntError),
    Incomplete,
    ParseError(ErrorKind),
    ParseFailure(ErrorKind),
    StringEncoding(String),

    #[cfg(debug_assertions)]
    NotImplemented,
}

impl From<NomError<(&[u8], ErrorKind)>> for FileError {
    fn from(e: NomError<(&[u8], ErrorKind)>) -> FileError {
        match e {
            // Need to translate the error here, as this lives longer than the input
            nom::Err::Incomplete(_) => FileError::Incomplete,
            nom::Err::Error((_,k)) => FileError::ParseError(k),
            nom::Err::Failure((_,k)) => FileError::ParseFailure(k),
        }
    }
}

impl From<Cow<'_, str>> for FileError {
    fn from(e: Cow<'_, str>) -> Self {
        FileError::StringEncoding(String::from(e))
    }
}

pub type FileResult<T> = Result<T, FileError>;