Skip to main content

manifold_csg/
types.rs

1//! Common types and error definitions.
2
3use std::sync::Mutex;
4
5use manifold_csg_sys::ManifoldOpType;
6
7/// Errors from manifold3d operations.
8#[non_exhaustive]
9#[derive(Debug, thiserror::Error)]
10pub enum CsgError {
11    #[error("manifold3d status: {0:?}")]
12    ManifoldStatus(manifold_csg_sys::ManifoldError),
13
14    #[error("invalid input: {0}")]
15    InvalidInput(String),
16}
17
18impl CsgError {
19    pub(crate) fn from_status(status: manifold_csg_sys::ManifoldError) -> Result<(), Self> {
20        if status.is_ok() {
21            Ok(())
22        } else {
23            Err(Self::ManifoldStatus(status))
24        }
25    }
26}
27
28/// Boolean operation type for generic manifold and cross-section booleans.
29#[non_exhaustive]
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub enum OpType {
32    /// Union/addition.
33    Add,
34    /// Difference/subtraction.
35    Subtract,
36    /// Intersection.
37    Intersect,
38}
39
40impl OpType {
41    pub(crate) const fn to_ffi(self) -> ManifoldOpType {
42        match self {
43            Self::Add => ManifoldOpType::Add,
44            Self::Subtract => ManifoldOpType::Subtract,
45            Self::Intersect => ManifoldOpType::Intersect,
46        }
47    }
48}
49
50pub(crate) type PanicPayload = Box<dyn std::any::Any + Send + 'static>;
51
52pub(crate) fn store_panic(slot: &Mutex<Option<PanicPayload>>, payload: PanicPayload) {
53    let mut guard = slot.lock().unwrap_or_else(|e| e.into_inner());
54    if guard.is_none() {
55        *guard = Some(payload);
56    }
57}
58
59pub(crate) fn take_stored_panic(slot: &Mutex<Option<PanicPayload>>) -> Option<PanicPayload> {
60    slot.lock().unwrap_or_else(|e| e.into_inner()).take()
61}