truck_polymesh/
errors.rs

1use thiserror::Error;
2
3/// Errors occurred by polygon mesh handling
4#[derive(Debug, Error)]
5pub enum Error<V: std::fmt::Debug = crate::StandardVertex> {
6    /// There is an index in out of range.
7    /// # Examples
8    /// ```
9    /// use truck_polymesh::*;
10    /// use errors::Error;
11    ///
12    /// let positions = vec![
13    ///     Point3::new(0.0, 0.0, 0.0),
14    ///     Point3::new(1.0, 0.0, 0.0),
15    ///     Point3::new(0.0, 1.0, 0.0),
16    /// ];
17    /// let faces = Faces::from_iter(&[
18    ///     &[0, 1, 2],
19    ///     &[1, 2, 4],
20    /// ]);
21    ///
22    /// let res = PolygonMesh::try_new(
23    ///     StandardAttributes {
24    ///         positions,
25    ///         ..Default::default()
26    ///     },
27    ///     faces,
28    /// );
29    /// match res {
30    ///     Err(Error::OutOfRange(vertex)) => {
31    ///         assert_eq!(vertex.pos, 4);
32    ///     }
33    ///     _ => panic!("wrong result!"),
34    /// }
35    /// ```
36    #[error("The index {0:?} is out of range.")]
37    OutOfRange(V),
38    /// There are no normal in polygon mesh.
39    #[error("This mesh has no normal vectors.")]
40    NoNormal,
41    /// The length of arrays of `StructuredMesh` is incorrect.
42    #[error("The lengths of point vector, uvdivisions, normal vector are incompatible.")]
43    DifferentLengthArrays,
44    /// The length of arrays of `StructuredMesh` is incorrect.
45    /// # Examples
46    /// ```
47    /// use truck_polymesh::*;
48    /// use errors::Error;
49    ///
50    /// let positions = vec![
51    ///     vec![Point3::new(0.0, 0.0, 0.0), Point3::new(1.0, 0.0, 0.0)],
52    ///     vec![Point3::new(0.0, 1.0, 0.0)],
53    /// ];
54    ///
55    /// match StructuredMesh::try_from_positions(positions) {
56    ///     Err(Error::IrregularArray) => {}
57    ///     _ => panic!("wrong result!"),
58    /// }
59    /// ```
60    #[error("This 2-dim array is irregular.")]
61    IrregularArray,
62    /// The division of uv coords of `StructuredMesh` is not sorted.
63    /// # Examples
64    /// ```
65    /// use truck_polymesh::*;
66    /// use errors::Error;
67    ///
68    /// let positions = vec![
69    ///     vec![Point3::new(0.0, 0.0, 0.0), Point3::new(0.0, 1.0, 0.0)],
70    ///     vec![Point3::new(1.0, 0.0, 0.0), Point3::new(1.0, 1.0, 0.0)],
71    /// ];
72    ///
73    /// let udiv = vec![1.0, 0.0];
74    /// let vdiv = vec![0.0, 1.0];
75    ///
76    /// match StructuredMesh::try_from_positions_and_uvs(positions, (udiv, vdiv)) {
77    ///     Err(Error::UnsortedDivision) => {}
78    ///     _ => panic!("wrong result!"),
79    /// }
80    /// ```
81    #[error("This division vector is unsorted.")]
82    UnsortedDivision,
83    /// Errors caused by obj files I/O.
84    #[error(transparent)]
85    FromIO(#[from] std::io::Error),
86}
87
88impl From<std::num::ParseFloatError> for Error {
89    fn from(error: std::num::ParseFloatError) -> Error {
90        std::io::Error::new(std::io::ErrorKind::InvalidData, error).into()
91    }
92}
93
94impl From<std::num::ParseIntError> for Error {
95    fn from(error: std::num::ParseIntError) -> Error {
96        std::io::Error::new(std::io::ErrorKind::InvalidData, error).into()
97    }
98}