mesh_loader/obj/
error.rs

1use std::{fmt, io, path::Path, str};
2
3#[cfg_attr(test, derive(Debug))]
4pub(super) enum ErrorKind {
5    ExpectedSpace(&'static str, usize),
6    ExpectedNewline(&'static str, usize),
7    Expected(&'static str, usize),
8    Float(usize),
9    Int(usize),
10    InvalidW(usize),
11    InvalidFaceIndex(usize),
12    Oob(usize, usize),
13    Io(io::Error),
14}
15
16impl ErrorKind {
17    #[cold]
18    #[inline(never)]
19    pub(super) fn into_io_error(self, start: &[u8], path: Option<&Path>) -> io::Error {
20        let remaining = match self {
21            Self::Expected(.., n)
22            | Self::ExpectedNewline(.., n)
23            | Self::ExpectedSpace(.., n)
24            | Self::Float(n)
25            | Self::Int(n)
26            | Self::InvalidW(n)
27            | Self::InvalidFaceIndex(n)
28            | Self::Oob(.., n) => n,
29            Self::Io(e) => return e,
30        };
31        crate::error::with_location(
32            &crate::error::invalid_data(self.to_string()),
33            &crate::error::Location::find(remaining, start, path),
34        )
35    }
36}
37
38impl fmt::Display for ErrorKind {
39    #[cold]
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match *self {
42            Self::ExpectedSpace(msg, ..) => write!(f, "expected space after {msg}"),
43            Self::ExpectedNewline(msg, ..) => write!(f, "expected newline after {msg}"),
44            Self::Expected(msg, ..) => write!(f, "expected {msg}"),
45            Self::InvalidW(..) => f.write_str("w in homogeneous vector must not zero"),
46            Self::InvalidFaceIndex(..) => f.write_str("invalid face index"),
47            Self::Float(..) => f.write_str("error while parsing a float"),
48            Self::Int(..) => f.write_str("error while parsing an integer"),
49            Self::Oob(i, ..) => write!(f, "face index out of bounds ({i})"),
50            Self::Io(ref e) => fmt::Display::fmt(e, f),
51        }
52    }
53}