off_rs/parser/
error.rs

1use std::{
2    borrow::Cow,
3    fmt::{Debug, Display, Formatter},
4};
5
6/// An error that occured while parsing the `off` string line by line.
7#[derive(Debug, Clone, PartialEq, Eq, Hash)]
8pub struct Error {
9    /// The [`Kind`] of the error.
10    pub kind: Kind,
11    /// The line number in the `off` string where the error occured.
12    pub line_index: usize,
13    /// An error message describing the problem.
14    pub message: Option<Cow<'static, str>>,
15}
16
17impl Error {
18    /// Creates a new [`Error`] with the given [`Kind`], line number and optionally a message.
19    #[must_use]
20    pub(crate) fn new(kind: Kind, line_index: usize, message: Option<Cow<'static, str>>) -> Self {
21        Self {
22            kind,
23            line_index,
24            message,
25        }
26    }
27
28    /// Creates a new [`Error`] with the given [`Kind`] and line numbber and a string as message.
29    #[must_use]
30    pub(crate) fn with_message<M: Into<Cow<'static, str>>, O: Into<Option<M>>>(
31        kind: Kind,
32        line_index: usize,
33        message: O,
34    ) -> Self {
35        Self::new(kind, line_index, message.into().map(Into::into))
36    }
37
38    /// Creates a new [`Error`] with the given [`Kind`] and line number.
39    #[must_use]
40    pub(crate) fn without_message(kind: Kind, line_index: usize) -> Self {
41        Self::new(kind, line_index, None)
42    }
43}
44
45impl std::error::Error for Error {}
46
47impl Display for Error {
48    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
49        if let Some(msg) = &self.message {
50            write!(f, "{} @ ln:{} - {}", self.kind, self.line_index + 1, msg)
51        } else {
52            write!(f, "{} @ ln:{}", self.kind, self.line_index + 1,)
53        }
54    }
55}
56
57#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
58pub enum Kind {
59    /// The `off` string is empty.
60    Empty,
61    /// An element is missing (e.g. counts, vertices, faces).
62    Missing,
63    /// A limit was exceeded.
64    LimitExceeded,
65    /// The header has a invalid format.
66    InvalidHeader,
67    /// The counts of vertices, faces and edges have an invalid format.
68    InvalidCounts,
69    /// The vertex position has an invalid format.
70    InvalidVertexPosition,
71    /// The color has an invalid format.
72    InvalidColor,
73    /// The face definition has an invalid format.
74    InvalidFace,
75    /// The face indicies have an invalid format.
76    InvalidFaceIndex,
77}
78
79impl Display for Kind {
80    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
81        Debug::fmt(self, f)
82    }
83}