1
0
Fork 0
mirror of https://codeberg.org/icewind/vbsp.git synced 2026-06-03 18:54:05 +02:00

cleanup errors

This commit is contained in:
Robin Appelman 2022-02-17 22:10:55 +01:00
commit fc945c828b
3 changed files with 69 additions and 36 deletions

View file

@ -1,4 +1,5 @@
use crate::bspfile::LumpType;
use crate::StringError;
use arrayvec::ArrayString;
use binrw::io::SeekFrom;
use binrw::{BinRead, BinResult, ReadOptions};
@ -6,7 +7,6 @@ use bitflags::bitflags;
use bv::BitVec;
use parse_display::Display;
use std::fmt;
use std::io::{Error, ErrorKind};
use std::iter::once;
use std::mem::size_of;
use std::ops::{Add, Index};
@ -180,14 +180,19 @@ impl BinRead for Name {
name_buf
.iter()
.position(|c| *c == 0)
.ok_or_else(|| binrw::Error::AssertFail {
.ok_or_else(|| binrw::Error::Custom {
pos: reader.seek(SeekFrom::Current(0)).unwrap(),
message: "Name not null terminated".to_string(),
err: Box::new(StringError::NotNullTerminated),
})?;
let name = &name_buf[..zero_pos];
Ok(Name(
ArrayString::from(
str::from_utf8(name).map_err(|err| Error::new(ErrorKind::InvalidData, err))?,
str::from_utf8(name)
.map_err(StringError::NonUTF8)
.map_err(|e| binrw::Error::Custom {
pos: reader.seek(SeekFrom::Current(0)).unwrap(),
err: Box::new(e),
})?,
)
.expect(
"Programmer error: it should be impossible for the string to exceed the capacity",

58
src/error.rs Normal file
View file

@ -0,0 +1,58 @@
use crate::data::*;
use miette::Diagnostic;
use thiserror::Error;
#[derive(Debug, Error, Diagnostic)]
pub enum BspError {
#[error("unexpected magic numbers or version")]
#[diagnostic(help("Ensure the loaded file is a valve bsp"))]
UnexpectedHeader(Header),
#[error("bsp lump is out of bounds of the bsp file")]
LumpOutOfBounds(LumpEntry),
#[error("unexpected length of uncompressed lump, got {got} but expected {expected}")]
UnexpectedUncompressedLumpSize { got: u32, expected: u32 },
#[error("error while decompressing lump")]
LumpDecompressError(lzma_rs::error::Error),
#[error("io error while reading data: {0}")]
IO(#[from] std::io::Error),
#[error(transparent)]
String(#[from] StringError),
}
impl From<binrw::Error> for BspError {
fn from(e: binrw::Error) -> Self {
use binrw::Error;
// only a few error types should be generated by our code
match e {
Error::Io(e) => BspError::IO(e),
Error::Custom { err, .. } => {
if let Ok(string_error) = err.downcast::<StringError>() {
BspError::String(*string_error)
} else {
panic!("unexpected custom error")
}
}
e => panic!("unexpected no variable match: {:?}", e),
}
}
}
impl From<lzma_rs::error::Error> for BspError {
fn from(e: lzma_rs::error::Error) -> Self {
use lzma_rs::error::Error;
match e {
Error::IoError(e) => BspError::IO(e),
e => BspError::LumpDecompressError(e),
}
}
}
#[derive(Debug, Error, Diagnostic)]
pub enum StringError {
#[error(transparent)]
NonUTF8(#[from] std::str::Utf8Error),
#[error("String is not null-terminated")]
NotNullTerminated,
}

View file

@ -1,5 +1,6 @@
mod bspfile;
pub mod data;
mod error;
mod reader;
use crate::bspfile::LumpType;
@ -9,41 +10,10 @@ use crate::data::*;
use binrw::io::Cursor;
use binrw::BinRead;
use bspfile::BspFile;
pub use error::{BspError, StringError};
use itertools::{GroupBy, Itertools};
use miette::Diagnostic;
use reader::LumpReader;
use std::{io::Read, ops::Deref};
use thiserror::Error;
#[derive(Debug, Error, Diagnostic)]
pub enum BspError {
#[error("unexpected magic numbers or version, is this a valve bsp?")]
UnexpectedHeader(Header),
#[error("bsp lump is out of bounds of the bsp file")]
LumpOutOfBounds(LumpEntry),
#[error("unexpected length of uncompressed lump, got {got} but expected {expected}")]
UnexpectedUncompressedLumpSize { got: u32, expected: u32 },
#[error("error while decompressing lump")]
LumpDecompressError(lzma_rs::error::Error),
#[error("malformed utf8 data")]
Utf8Error(#[from] std::string::FromUtf8Error),
#[error("Directory entry length isn't a multiple of element size")]
MalformedLump,
#[error("invalid surface flag in {0}")]
InvalidSurfaceFlag(Name),
#[error("invalid content flag in {0}")]
InvalidContentFlag(Name),
#[error("non null-terminated name")]
InvalidName,
#[error("unexpected eof while reading data")]
UnexpectedEOF,
#[error("extra data at the end of the lump")]
UnexpectedExtraData,
#[error("error while reading data: {0}")]
ReadError(#[from] std::io::Error),
#[error("error while reading data: {0}")]
BinReadError(#[from] binrw::Error),
}
pub type BspResult<T> = Result<T, BspError>;