mirror of
https://codeberg.org/icewind/vbsp.git
synced 2026-06-03 18:54:05 +02:00
cleanup errors
This commit is contained in:
parent
a785884bc1
commit
fc945c828b
3 changed files with 69 additions and 36 deletions
13
src/data.rs
13
src/data.rs
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::bspfile::LumpType;
|
use crate::bspfile::LumpType;
|
||||||
|
use crate::StringError;
|
||||||
use arrayvec::ArrayString;
|
use arrayvec::ArrayString;
|
||||||
use binrw::io::SeekFrom;
|
use binrw::io::SeekFrom;
|
||||||
use binrw::{BinRead, BinResult, ReadOptions};
|
use binrw::{BinRead, BinResult, ReadOptions};
|
||||||
|
|
@ -6,7 +7,6 @@ use bitflags::bitflags;
|
||||||
use bv::BitVec;
|
use bv::BitVec;
|
||||||
use parse_display::Display;
|
use parse_display::Display;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io::{Error, ErrorKind};
|
|
||||||
use std::iter::once;
|
use std::iter::once;
|
||||||
use std::mem::size_of;
|
use std::mem::size_of;
|
||||||
use std::ops::{Add, Index};
|
use std::ops::{Add, Index};
|
||||||
|
|
@ -180,14 +180,19 @@ impl BinRead for Name {
|
||||||
name_buf
|
name_buf
|
||||||
.iter()
|
.iter()
|
||||||
.position(|c| *c == 0)
|
.position(|c| *c == 0)
|
||||||
.ok_or_else(|| binrw::Error::AssertFail {
|
.ok_or_else(|| binrw::Error::Custom {
|
||||||
pos: reader.seek(SeekFrom::Current(0)).unwrap(),
|
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];
|
let name = &name_buf[..zero_pos];
|
||||||
Ok(Name(
|
Ok(Name(
|
||||||
ArrayString::from(
|
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(
|
.expect(
|
||||||
"Programmer error: it should be impossible for the string to exceed the capacity",
|
"Programmer error: it should be impossible for the string to exceed the capacity",
|
||||||
|
|
|
||||||
58
src/error.rs
Normal file
58
src/error.rs
Normal 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,
|
||||||
|
}
|
||||||
34
src/lib.rs
34
src/lib.rs
|
|
@ -1,5 +1,6 @@
|
||||||
mod bspfile;
|
mod bspfile;
|
||||||
pub mod data;
|
pub mod data;
|
||||||
|
mod error;
|
||||||
mod reader;
|
mod reader;
|
||||||
|
|
||||||
use crate::bspfile::LumpType;
|
use crate::bspfile::LumpType;
|
||||||
|
|
@ -9,41 +10,10 @@ use crate::data::*;
|
||||||
use binrw::io::Cursor;
|
use binrw::io::Cursor;
|
||||||
use binrw::BinRead;
|
use binrw::BinRead;
|
||||||
use bspfile::BspFile;
|
use bspfile::BspFile;
|
||||||
|
pub use error::{BspError, StringError};
|
||||||
use itertools::{GroupBy, Itertools};
|
use itertools::{GroupBy, Itertools};
|
||||||
use miette::Diagnostic;
|
|
||||||
use reader::LumpReader;
|
use reader::LumpReader;
|
||||||
use std::{io::Read, ops::Deref};
|
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>;
|
pub type BspResult<T> = Result<T, BspError>;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue