rust_3d/
result.rs

1/*
2Copyright 2016 Martin Buck
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation the
7rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software
9is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall
12be included all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23//! Result, the result type used within rust-3d. Also defining the error enum and several transformation methods between error types.
24
25use std::{
26    fmt,
27    io::Error as ioError,
28    num::{ParseFloatError, ParseIntError},
29    result,
30};
31
32use crate::io::*;
33
34//------------------------------------------------------------------------------
35
36/// The Error Enum used by rust-3d
37pub enum ErrorKind {
38    MinMaxSwapped,
39    MinMaxEqual,
40    TooFewPoints,
41    BoundingBoxMissing,
42    NormalizeVecWithoutLength,
43    IOError,
44    ParseError,
45    IndexOutOfBounds,
46    IncorrectFaceID,
47    IncorrectVertexID,
48    IncorrectEdgeID,
49    IncorrectVoxelID,
50    IncorrectUnitID,
51    IncorrectSegmentID,
52    IncorrectDimension,
53    DimensionsDontMatch,
54    NumberConversionError,
55    NumberInWrongRange,
56    ComparisionFailed,
57    ClusterTooBig,
58    CantCalculateAngleIfZeroLength,
59    TriFace3DNotSpanningVolume,
60    PlyError(PlyError),
61    StlError(StlError),
62    PtxError(PtxError),
63    XyError(XyError),
64    XyzError(XyzError),
65    ObjError(ObjError),
66    OffError(OffError),
67    PslError(PslError),
68    GcodeError(GcodeError),
69}
70
71//------------------------------------------------------------------------------
72
73impl fmt::Debug for ErrorKind {
74    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75        match self {
76            Self::MinMaxSwapped => write!(f, "Passed min/max values are swapped (min > max)"),
77            Self::MinMaxEqual => write!(f, "Passed min/max values are equal"),
78            Self::TooFewPoints => write!(f, "Container had too few points for the operation"),
79            Self::BoundingBoxMissing => write!(f, "Bounding box is missing for the operation"),
80            Self::NormalizeVecWithoutLength => write!(f, "Can't normalize a vector of length 0"),
81            Self::IOError => write!(f, "Can't read or write a file"),
82            Self::ParseError => write!(f, "Can't parse data"),
83            Self::IndexOutOfBounds => write!(f, "Tried to access an out of bounds index"),
84            Self::IncorrectFaceID => write!(f, "Used an incorrect face id"),
85            Self::IncorrectVertexID => write!(f, "Used an incorrect vertex id"),
86            Self::IncorrectEdgeID => write!(f, "Used an incorrect edge id"),
87            Self::IncorrectVoxelID => write!(f, "Used an incorrect voxel id"),
88            Self::IncorrectUnitID => write!(f, "Used an incorrect unit id"),
89            Self::IncorrectSegmentID => write!(f, "Used an incorrect segment id"),
90            Self::IncorrectDimension => write!(f, "Trying to access an incorrect dimension"),
91            Self::DimensionsDontMatch => write!(f, "Trying to mix types with different dimensions"),
92            Self::NumberConversionError => {
93                write!(f, "Failed converting one number type to another")
94            }
95            Self::NumberInWrongRange => write!(f, "Passed number is within the wrong range"),
96            Self::ComparisionFailed => write!(f, "Comparision between two values failed"),
97            Self::CantCalculateAngleIfZeroLength => {
98                write!(f, "Can't calculate the angle between 0 vectors")
99            }
100            Self::ClusterTooBig => write!(f, "Clustering size is too big for given mesh"),
101            Self::TriFace3DNotSpanningVolume => write!(
102                f,
103                "TriFace3D must be constructed from points spanning a volume"
104            ),
105            Self::PlyError(x) => x.fmt(f),
106            Self::StlError(x) => x.fmt(f),
107            Self::PtxError(x) => x.fmt(f),
108            Self::XyError(x) => x.fmt(f),
109            Self::XyzError(x) => x.fmt(f),
110            Self::ObjError(x) => x.fmt(f),
111            Self::OffError(x) => x.fmt(f),
112            Self::PslError(x) => x.fmt(f),
113            Self::GcodeError(x) => x.fmt(f),
114        }
115    }
116}
117
118//------------------------------------------------------------------------------
119
120/// Result type used by rust-3d
121pub type Result<T> = result::Result<T, ErrorKind>;
122
123impl From<ParseFloatError> for ErrorKind {
124    fn from(_error: ParseFloatError) -> Self {
125        ErrorKind::ParseError
126    }
127}
128
129impl From<ParseIntError> for ErrorKind {
130    fn from(_error: ParseIntError) -> ErrorKind {
131        ErrorKind::ParseError
132    }
133}
134
135impl From<ioError> for ErrorKind {
136    fn from(_error: ioError) -> Self {
137        ErrorKind::IOError
138    }
139}
140
141impl From<PlyError> for ErrorKind {
142    fn from(error: PlyError) -> Self {
143        Self::PlyError(error)
144    }
145}
146
147impl From<StlError> for ErrorKind {
148    fn from(error: StlError) -> Self {
149        Self::StlError(error)
150    }
151}
152
153impl From<XyError> for ErrorKind {
154    fn from(error: XyError) -> Self {
155        Self::XyError(error)
156    }
157}
158
159impl From<XyzError> for ErrorKind {
160    fn from(error: XyzError) -> Self {
161        Self::XyzError(error)
162    }
163}
164
165impl From<ObjError> for ErrorKind {
166    fn from(error: ObjError) -> Self {
167        Self::ObjError(error)
168    }
169}
170
171impl From<OffError> for ErrorKind {
172    fn from(error: OffError) -> Self {
173        Self::OffError(error)
174    }
175}
176
177impl From<GcodeError> for ErrorKind {
178    fn from(error: GcodeError) -> Self {
179        Self::GcodeError(error)
180    }
181}