1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use thiserror::Error;

/// Errors occured by polygon mesh handling
#[derive(Debug, Error)]
pub enum Error<V: std::fmt::Debug = crate::StandardVertex> {
    /// There is an index in out of range.
    /// # Examples
    /// ```
    /// use truck_polymesh::*;
    /// use errors::Error;
    /// 
    /// let positions = vec![
    ///     Point3::new(0.0, 0.0, 0.0),
    ///     Point3::new(1.0, 0.0, 0.0),
    ///     Point3::new(0.0, 1.0, 0.0),
    /// ];
    /// let faces = Faces::from_iter(&[
    ///     &[0, 1, 2],
    ///     &[1, 2, 4],
    /// ]);
    /// 
    /// let res = PolygonMesh::try_new(
    ///     StandardAttributes {
    ///         positions,
    ///         ..Default::default()
    ///     },
    ///     faces,
    /// );
    /// match res {
    ///     Err(Error::OutOfRange(vertex)) => {
    ///         assert_eq!(vertex.pos, 4);
    ///     }
    ///     _ => panic!("wrong result!"),
    /// }
    /// ```
    #[error("The index {0:?} is out of range.")]
    OutOfRange(V),
    /// There are no normal in polygon mesh.
    #[error("This mesh has no normal vectors.")]
    NoNormal,
    /// The length of arrays of `StructuredMesh` is incorrect.
    #[error("The lengthes of point vector, uvdivisions, normal vector are incompatible.")]
    DifferentLengthArrays,
    /// The length of arrays of `StructuredMesh` is incorrect.
    /// # Examples
    /// ```
    /// use truck_polymesh::*;
    /// use errors::Error;
    /// 
    /// let positions = vec![
    ///     vec![Point3::new(0.0, 0.0, 0.0), Point3::new(1.0, 0.0, 0.0)],
    ///     vec![Point3::new(0.0, 1.0, 0.0)],
    /// ];
    /// 
    /// match StructuredMesh::try_from_positions(positions) {
    ///     Err(Error::IrregularArray) => {}
    ///     _ => panic!("wrong result!"),
    /// }
    /// ```
    #[error("This 2-dim array is irregular.")]
    IrregularArray,
    /// The division of uv coords of `StructuredMesh` is not sorted.
    /// # Examples
    /// ```
    /// use truck_polymesh::*;
    /// use errors::Error;
    /// 
    /// let positions = vec![
    ///     vec![Point3::new(0.0, 0.0, 0.0), Point3::new(0.0, 1.0, 0.0)],
    ///     vec![Point3::new(1.0, 0.0, 0.0), Point3::new(1.0, 1.0, 0.0)],
    /// ];
    /// 
    /// let udiv = vec![1.0, 0.0];
    /// let vdiv = vec![0.0, 1.0];
    /// 
    /// match StructuredMesh::try_from_positions_and_uvs(positions, (udiv, vdiv)) {
    ///     Err(Error::UnsortedDivision) => {}
    ///     _ => panic!("wrong result!"),
    /// }
    /// ``` 
    #[error("This division vector is unsorted.")]
    UnsortedDivision,
    /// Errors caused by obj files I/O.
    #[error(transparent)]
    FromIO(#[from] std::io::Error),
}

impl From<std::num::ParseFloatError> for Error {
    fn from(error: std::num::ParseFloatError) -> Error {
        std::io::Error::new(std::io::ErrorKind::InvalidData, error).into()
    }
}

impl From<std::num::ParseIntError> for Error {
    fn from(error: std::num::ParseIntError) -> Error {
        std::io::Error::new(std::io::ErrorKind::InvalidData, error).into()
    }
}