Skip to content

Commit

Permalink
Adds additional context to "Unable to convert the index from usize"
Browse files Browse the repository at this point in the history
Closes #72
  • Loading branch information
nshyrei authored and simnalamburt committed Sep 28, 2024
1 parent b1e75fd commit ad0fe29
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
13 changes: 12 additions & 1 deletion obj-rs/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ implmnt!(Load, LoadError);
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct LoadError {
kind: LoadErrorKind,
message: &'static str,
message: String,
}

impl LoadError {
Expand Down Expand Up @@ -101,6 +101,7 @@ pub enum LoadErrorKind {
impl LoadError {
/// Creates a new custom error from a specified kind and message.
pub fn new(kind: LoadErrorKind, message: &'static str) -> Self {
let message = message.to_string();
LoadError { kind, message }
}
}
Expand Down Expand Up @@ -136,3 +137,13 @@ macro_rules! make_error {
}

pub(crate) use make_error;

pub(super) fn index_out_of_range<T, I>(index: usize) -> ObjResult<T> {
let name = std::any::type_name::<I>();
Err(ObjError::Load(LoadError {
kind: LoadErrorKind::IndexOutOfRange,
message: format!(
"Given index type '{name}' is not large enough to contain the index '{index}'"
),
}))
}
14 changes: 4 additions & 10 deletions obj-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub mod raw;

pub use crate::error::{LoadError, LoadErrorKind, ObjError, ObjResult};

use crate::error::make_error;
use crate::error::{index_out_of_range, make_error};
use crate::raw::object::Polygon;
use num_traits::FromPrimitive;
use std::collections::hash_map::{Entry, HashMap};
Expand Down Expand Up @@ -130,10 +130,7 @@ impl<I: FromPrimitive + Copy> FromRawVertex<I> for Vertex {
};
let index = match I::from_usize(vb.len()) {
Some(val) => val,
None => make_error!(
IndexOutOfRange,
"Unable to convert the index from usize"
),
None => return index_out_of_range::<_, I>(vb.len()),
};
vb.push(vertex);
entry.insert(index);
Expand Down Expand Up @@ -207,7 +204,7 @@ impl<I: FromPrimitive> FromRawVertex<I> for Position {
let mut map = |pi: usize| -> ObjResult<()> {
ib.push(match I::from_usize(pi) {
Some(val) => val,
None => make_error!(IndexOutOfRange, "Unable to convert the index from usize"),
None => return index_out_of_range::<_, I>(pi),
});
Ok(())
};
Expand Down Expand Up @@ -285,10 +282,7 @@ impl<I: FromPrimitive + Copy> FromRawVertex<I> for TexturedVertex {
};
let index = match I::from_usize(vb.len()) {
Some(val) => val,
None => make_error!(
IndexOutOfRange,
"Unable to convert the index from usize"
),
None => return index_out_of_range::<_, I>(vb.len()),
};
vb.push(vertex);
entry.insert(index);
Expand Down
6 changes: 1 addition & 5 deletions obj-rs/tests/issue-63.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ fn do_test<V: obj::FromRawVertex<u8> + std::fmt::Debug>(test_case: &str) {
let err = load_obj::<V, _, _>(Cursor::new(test_case))
.expect_err("Should error out due to index out of bounds");
if let obj::ObjError::Load(err) = err {
let expected_error = obj::LoadError::new(
obj::LoadErrorKind::IndexOutOfRange,
"Unable to convert the index from usize",
);
assert_eq!(err.to_string(), expected_error.to_string());
assert_eq!(*err.kind(), obj::LoadErrorKind::IndexOutOfRange);
} else {
panic!("Expected a LoadError");
}
Expand Down

0 comments on commit ad0fe29

Please sign in to comment.