Split error enum into Encode vs. Decode errors
This commit is contained in:
+19
-13
@@ -2,7 +2,7 @@ use nom::error::{ErrorKind, ParseError as NomParseError};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ParseError {
|
||||
pub enum DecodeError {
|
||||
#[error("invalid ZZT file magic")]
|
||||
InvalidMagic,
|
||||
|
||||
@@ -12,20 +12,11 @@ pub enum ParseError {
|
||||
#[error("negative stat count")]
|
||||
NegativeStatCount,
|
||||
|
||||
#[error("string too long")]
|
||||
StringTooLong { max: u8 },
|
||||
|
||||
#[error("cannot encode character: {0}")]
|
||||
EncodingError(char),
|
||||
|
||||
#[error("board data too large")]
|
||||
BoardTooLarge,
|
||||
|
||||
#[error("nom error: {0}")]
|
||||
NomError(String),
|
||||
}
|
||||
|
||||
impl<I> NomParseError<I> for ParseError {
|
||||
impl<I> NomParseError<I> for DecodeError {
|
||||
fn from_error_kind(_input: I, kind: ErrorKind) -> Self {
|
||||
Self::NomError(kind.description().to_string())
|
||||
}
|
||||
@@ -35,11 +26,26 @@ impl<I> NomParseError<I> for ParseError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<nom::Err<ParseError>> for ParseError {
|
||||
fn from(value: nom::Err<ParseError>) -> Self {
|
||||
impl From<nom::Err<DecodeError>> for DecodeError {
|
||||
fn from(value: nom::Err<DecodeError>) -> Self {
|
||||
match value {
|
||||
nom::Err::Error(e) | nom::Err::Failure(e) => e,
|
||||
nom::Err::Incomplete(needed) => Self::NomError(format!("incomplete: {:?}", needed)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum EncodeError {
|
||||
#[error("invalid tile count: {0}")]
|
||||
InvalidTileCount(usize),
|
||||
|
||||
#[error("string too long")]
|
||||
StringTooLong { max: u8 },
|
||||
|
||||
#[error("cannot encode character: {0}")]
|
||||
EncodingError(char),
|
||||
|
||||
#[error("board data too large")]
|
||||
BoardTooLarge,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user