Skip to main content

brep_cam/
error.rs

1//! The crate-wide error type. Generation-stopping failures are errors; a
2//! recoverable degradation is a warning on the operation report instead.
3
4use crate::cancel::Cancelled;
5use crate::cutter::CutterError;
6use crate::model::TransformError;
7use crate::numeric::NumericError;
8
9#[derive(Clone, Debug, PartialEq)]
10pub enum CamError {
11    /// The caller cancelled; the previous accepted result stands.
12    Cancelled,
13    /// A cutter's dimensions were refused.
14    Cutter(CutterError),
15    /// The tolerance object was refused.
16    Numeric(NumericError),
17    /// A setup transform was refused.
18    Transform(TransformError),
19    /// A coordinate that must be finite was not. `what` names it.
20    NonFinite { what: String },
21    /// An enabled operation has no target triangles.
22    EmptyTarget { operation_id: String },
23    /// A referenced body or face does not exist in the snapshot.
24    UnresolvedTarget { solid: String, face: Option<String> },
25    /// A strategy does not support the selected cutter kind.
26    UnsupportedCutter { strategy: String, cutter: String },
27    /// Any other invalid input, with a user-facing message.
28    InvalidInput(String),
29}
30
31impl std::fmt::Display for CamError {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        match self {
34            CamError::Cancelled => write!(f, "cancelled"),
35            CamError::Cutter(e) => write!(f, "cutter: {e}"),
36            CamError::Numeric(e) => write!(f, "tolerance: {e}"),
37            CamError::Transform(e) => write!(f, "setup transform: {e}"),
38            CamError::NonFinite { what } => write!(f, "{what} is not finite"),
39            CamError::EmptyTarget { operation_id } => {
40                write!(f, "operation {operation_id} has no target triangles")
41            }
42            CamError::UnresolvedTarget { solid, face: None } => {
43                write!(f, "target body {solid:?} is not in the mesh snapshot")
44            }
45            CamError::UnresolvedTarget { solid, face: Some(face) } => {
46                write!(f, "target face {face:?} of body {solid:?} is not in the mesh snapshot")
47            }
48            CamError::UnsupportedCutter { strategy, cutter } => {
49                write!(f, "strategy {strategy} does not support a {cutter} cutter")
50            }
51            CamError::InvalidInput(msg) => write!(f, "{msg}"),
52        }
53    }
54}
55
56impl std::error::Error for CamError {}
57
58impl From<Cancelled> for CamError {
59    fn from(_: Cancelled) -> Self {
60        CamError::Cancelled
61    }
62}
63
64impl From<CutterError> for CamError {
65    fn from(e: CutterError) -> Self {
66        CamError::Cutter(e)
67    }
68}
69
70impl From<NumericError> for CamError {
71    fn from(e: NumericError) -> Self {
72        CamError::Numeric(e)
73    }
74}
75
76impl From<TransformError> for CamError {
77    fn from(e: TransformError) -> Self {
78        CamError::Transform(e)
79    }
80}