re-work quest errors

use individual error types for both bin and dat modules instead of
all three modules sharing the same error type. maybe marginally better
this way? at least should generally be more clear about whether the
error is related to a bin, dat or general quest processing now
This commit is contained in:
Gered 2021-05-15 15:24:49 -04:00
parent 6c672f3d27
commit c84142dfe9
3 changed files with 54 additions and 28 deletions

View file

@ -2,10 +2,8 @@ use std::path::Path;
use thiserror::Error;
use crate::bytes::{ReadBytesError, WriteBytesError};
use crate::quest::bin::QuestBin;
use crate::quest::dat::QuestDat;
use crate::text::LanguageError;
use crate::quest::bin::{QuestBin, QuestBinError};
use crate::quest::dat::{QuestDat, QuestDatError};
pub mod bin;
pub mod dat;
@ -15,14 +13,11 @@ pub enum QuestError {
#[error("I/O error reading quest")]
IoError(#[from] std::io::Error),
#[error("String encoding error during processing of quest string field")]
StringEncodingError(#[from] LanguageError),
#[error("Error processing quest bin")]
QuestBinError(#[from] QuestBinError),
#[error("Error reading quest from bytes")]
ReadFromBytesError(#[from] ReadBytesError),
#[error("Error writing quest as bytes")]
WriteAsBytesError(#[from] WriteBytesError),
#[error("Error processing quest dat")]
QuestDatError(#[from] QuestDatError),
}
pub struct Quest {

View file

@ -3,11 +3,11 @@ use std::io::{BufReader, Cursor, Read};
use std::path::Path;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use thiserror::Error;
use crate::bytes::*;
use crate::compression::{prs_compress, prs_decompress};
use crate::quest::QuestError;
use crate::text::Language;
use crate::text::{Language, LanguageError};
pub const QUEST_BIN_NAME_LENGTH: usize = 32;
pub const QUEST_BIN_SHORT_DESCRIPTION_LENGTH: usize = 128;
@ -18,6 +18,21 @@ pub const QUEST_BIN_HEADER_SIZE: usize = 20
+ QUEST_BIN_SHORT_DESCRIPTION_LENGTH
+ QUEST_BIN_LONG_DESCRIPTION_LENGTH;
#[derive(Error, Debug)]
pub enum QuestBinError {
#[error("I/O error while processing quest bin")]
IoError(#[from] std::io::Error),
#[error("String encoding error during processing of quest bin string field")]
StringEncodingError(#[from] LanguageError),
#[error("Error reading quest bin from bytes")]
ReadFromBytesError(#[from] ReadBytesError),
#[error("Error writing quest bin as bytes")]
WriteAsBytesError(#[from] WriteBytesError),
}
#[derive(Copy, Clone)]
pub struct QuestNumberAndEpisode {
pub number: u8,
@ -65,20 +80,20 @@ pub struct QuestBin {
}
impl QuestBin {
pub fn from_compressed_bytes(bytes: &[u8]) -> Result<QuestBin, QuestError> {
pub fn from_compressed_bytes(bytes: &[u8]) -> Result<QuestBin, QuestBinError> {
let decompressed = prs_decompress(&bytes);
let mut reader = Cursor::new(decompressed);
Ok(QuestBin::read_from_bytes(&mut reader)?)
}
pub fn from_compressed_file(path: &Path) -> Result<QuestBin, QuestError> {
pub fn from_compressed_file(path: &Path) -> Result<QuestBin, QuestBinError> {
let mut file = File::open(path)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
QuestBin::from_compressed_bytes(&buffer)
}
pub fn from_uncompressed_file(path: &Path) -> Result<QuestBin, QuestError> {
pub fn from_uncompressed_file(path: &Path) -> Result<QuestBin, QuestBinError> {
let file = File::open(path)?;
let mut reader = BufReader::new(file);
Ok(QuestBin::read_from_bytes(&mut reader)?)
@ -310,7 +325,7 @@ mod tests {
}
#[test]
pub fn read_compressed_quest_58_bin() -> Result<(), QuestError> {
pub fn read_compressed_quest_58_bin() -> Result<(), QuestBinError> {
let path = Path::new("assets/test/q058-ret-gc.bin");
let bin = QuestBin::from_compressed_file(&path)?;
validate_quest_58_bin(&bin);
@ -318,7 +333,7 @@ mod tests {
}
#[test]
pub fn read_uncompressed_quest_58_bin() -> Result<(), QuestError> {
pub fn read_uncompressed_quest_58_bin() -> Result<(), QuestBinError> {
let path = Path::new("assets/test/q058-ret-gc.uncompressed.bin");
let bin = QuestBin::from_uncompressed_file(&path)?;
validate_quest_58_bin(&bin);
@ -326,7 +341,7 @@ mod tests {
}
#[test]
pub fn read_compressed_quest_118_bin() -> Result<(), QuestError> {
pub fn read_compressed_quest_118_bin() -> Result<(), QuestBinError> {
let path = Path::new("assets/test/q118-vr-gc.bin");
let bin = QuestBin::from_compressed_file(&path)?;
validate_quest_118_bin(&bin);
@ -334,7 +349,7 @@ mod tests {
}
#[test]
pub fn read_uncompressed_quest_118_bin() -> Result<(), QuestError> {
pub fn read_uncompressed_quest_118_bin() -> Result<(), QuestBinError> {
let path = Path::new("assets/test/q118-vr-gc.uncompressed.bin");
let bin = QuestBin::from_uncompressed_file(&path)?;
validate_quest_118_bin(&bin);

View file

@ -4,10 +4,11 @@ use std::io::{BufReader, Cursor, Read};
use std::path::Path;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use thiserror::Error;
use crate::bytes::*;
use crate::compression::{prs_compress, prs_decompress};
use crate::quest::QuestError;
use crate::text::LanguageError;
pub const QUEST_DAT_TABLE_HEADER_SIZE: usize = 16;
@ -54,6 +55,21 @@ pub const QUEST_DAT_AREAS: [[&str; 18]; 2] = [
],
];
#[derive(Error, Debug)]
pub enum QuestDatError {
#[error("I/O error while processing quest dat")]
IoError(#[from] std::io::Error),
#[error("String encoding error during processing of quest dat string field")]
StringEncodingError(#[from] LanguageError),
#[error("Error reading quest dat from bytes")]
ReadFromBytesError(#[from] ReadBytesError),
#[error("Error writing quest dat as bytes")]
WriteAsBytesError(#[from] WriteBytesError),
}
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum QuestDatTableType {
Object,
@ -154,20 +170,20 @@ pub struct QuestDat {
}
impl QuestDat {
pub fn from_compressed_bytes(bytes: &[u8]) -> Result<QuestDat, QuestError> {
pub fn from_compressed_bytes(bytes: &[u8]) -> Result<QuestDat, QuestDatError> {
let decompressed = prs_decompress(&bytes);
let mut reader = Cursor::new(decompressed);
Ok(QuestDat::read_from_bytes(&mut reader)?)
}
pub fn from_compressed_file(path: &Path) -> Result<QuestDat, QuestError> {
pub fn from_compressed_file(path: &Path) -> Result<QuestDat, QuestDatError> {
let mut file = File::open(path)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
QuestDat::from_compressed_bytes(&buffer)
}
pub fn from_uncompressed_file(path: &Path) -> Result<QuestDat, QuestError> {
pub fn from_uncompressed_file(path: &Path) -> Result<QuestDat, QuestDatError> {
let file = File::open(path)?;
let mut reader = BufReader::new(file);
Ok(QuestDat::read_from_bytes(&mut reader)?)
@ -489,7 +505,7 @@ mod tests {
}
#[test]
pub fn read_compressed_quest_58_dat() -> Result<(), QuestError> {
pub fn read_compressed_quest_58_dat() -> Result<(), QuestDatError> {
let path = Path::new("assets/test/q058-ret-gc.dat");
let dat = QuestDat::from_compressed_file(&path)?;
validate_quest_58_dat(&dat);
@ -497,7 +513,7 @@ mod tests {
}
#[test]
pub fn read_uncompressed_quest_58_dat() -> Result<(), QuestError> {
pub fn read_uncompressed_quest_58_dat() -> Result<(), QuestDatError> {
let path = Path::new("assets/test/q058-ret-gc.uncompressed.dat");
let dat = QuestDat::from_uncompressed_file(&path)?;
validate_quest_58_dat(&dat);
@ -505,7 +521,7 @@ mod tests {
}
#[test]
pub fn read_compressed_quest_118_dat() -> Result<(), QuestError> {
pub fn read_compressed_quest_118_dat() -> Result<(), QuestDatError> {
let path = Path::new("assets/test/q118-vr-gc.dat");
let dat = QuestDat::from_compressed_file(&path)?;
validate_quest_118_dat(&dat);
@ -513,7 +529,7 @@ mod tests {
}
#[test]
pub fn read_uncompressed_quest_118_dat() -> Result<(), QuestError> {
pub fn read_uncompressed_quest_118_dat() -> Result<(), QuestDatError> {
let path = Path::new("assets/test/q118-vr-gc.uncompressed.dat");
let dat = QuestDat::from_uncompressed_file(&path)?;
validate_quest_118_dat(&dat);