2021-11-18 15:11:56 -08:00
|
|
|
use num_enum::TryFromPrimitiveError;
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
use crate::parse::{self, Op};
|
|
|
|
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum Error {
|
|
|
|
#[error("i/o error: {0}")]
|
|
|
|
Io(#[from] std::io::Error),
|
|
|
|
#[error("{0}")]
|
|
|
|
Parse(parse::Error<parse::InputOwned>),
|
|
|
|
#[error("unsupported version: {0}")]
|
|
|
|
UnsupportedVersion(parse::Version),
|
|
|
|
#[error("unsupported encryptor: {0}")]
|
|
|
|
UnsupportedEncryptor(String),
|
|
|
|
#[error("unexpected EOF")]
|
|
|
|
Eof,
|
|
|
|
#[error("invalid header op: {0}")]
|
|
|
|
InvalidOp(#[from] TryFromPrimitiveError<Op>),
|
|
|
|
#[error("missing field: {0:?}")]
|
|
|
|
MissingField(String),
|
|
|
|
#[error("bag is unindexed")]
|
|
|
|
Unindexed,
|
|
|
|
#[error("{0}")]
|
|
|
|
Other(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<parse::Error<parse::Input<'a>>> for Error {
|
|
|
|
fn from(err: parse::Error<parse::Input>) -> Self {
|
|
|
|
Error::Parse(err.into_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<nom::Err<parse::Error<parse::Input<'a>>>> for Error {
|
|
|
|
fn from(err: nom::Err<parse::Error<parse::Input<'a>>>) -> Self {
|
|
|
|
match err {
|
|
|
|
nom::Err::Error(e) | nom::Err::Failure(e) => e.into(),
|
|
|
|
nom::Err::Incomplete(_) => panic!("incomplete error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error {
|
2021-11-18 15:56:02 -08:00
|
|
|
pub fn other<S: ToString>(message: S) -> Self {
|
|
|
|
Error::Other(message.to_string())
|
2021-11-18 15:11:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Result<T, E = Error> = core::result::Result<T, E>;
|