#[rustfmt::skip]
use std::{
error as std_error,
fmt as std_fmt,
result as std_result,
};
pub type Result<T> = std_result::Result<T, Error>;
#[derive(Clone)]
#[derive(Copy)]
#[derive(Debug)]
#[derive(Eq, PartialEq)]
pub enum ParseErrorKind {
RecordSeparatorInContinuation,
UnfinishedLine,
UnfinishedField,
UnfinishedRecord,
InvalidFieldName,
}
#[derive(Clone)]
#[derive(Debug)]
#[derive(Eq, PartialEq)]
pub struct ParseErrorDetail {
pub line : u32,
pub column : u32,
pub kind : ParseErrorKind,
}
#[derive(Clone)]
#[derive(Debug)]
#[derive(Eq, PartialEq)]
pub enum Error {
CannotOpenJarFile,
NoRecords,
OutOfMemory,
BadFileRead,
ParseError {
detail : ParseErrorDetail,
},
InvalidIndex,
Unexpected,
InvalidContent,
}
impl ParseErrorKind {
pub fn message(self) -> &'static str {
match self {
Self::RecordSeparatorInContinuation => {
"A record separator was encountered during a content line \
continuation"
},
Self::UnfinishedLine => {
"The last line in the database was not terminated by a \
line-feed"
},
Self::UnfinishedField => "The last field in the database file was not well-formed",
Self::UnfinishedRecord => {
"The last record in the database file was not terminated \
by a record separator"
},
Self::InvalidFieldName => "The field name was not valid",
}
}
}
impl std_fmt::Display for ParseErrorKind {
fn fmt(
&self,
f : &mut std_fmt::Formatter<'_>,
) -> std_fmt::Result {
f.write_str(self.message())
}
}
impl std_fmt::Display for Error {
fn fmt(
&self,
f : &mut std_fmt::Formatter<'_>,
) -> std_fmt::Result {
let message = match self {
Self::CannotOpenJarFile => "The given file does not exist, or cannot be accessed",
Self::NoRecords => "The database file contained no records",
Self::OutOfMemory => "The API suffered memory exhaustion",
Self::BadFileRead => "A read operation failed",
Self::ParseError {
detail,
} => {
return write!(
f,
"Parsing of the database file failed due to a syntax \
error: {}",
detail.kind,
);
},
Self::InvalidIndex => "An invalid index was specified",
Self::Unexpected => "An unexpected condition was encountered",
Self::InvalidContent => "The database file contained invalid content",
};
f.write_str(message)
}
}
impl std_error::Error for Error {
}