rgmsh/
err.rs

1//! Idiomatic Rust error handling for the Gmsh API.
2
3use std::error::Error;
4use std::fmt::Display;
5use std::fmt as fmt;
6
7/// The error type for all Gmsh API functions.
8#[derive(Debug)]
9pub enum GmshError {
10    /// The Gmsh context wasn't properly initialized, or a required library component is missing.
11    /// For example, calling any `fltk` functions without a linked FLTK library.
12    Initialization, // -1 everywhere
13    /// One of Gmsh's "shell" methods couldn't run successfully.
14    /// For example, a bad file path was given to the `open` function.
15    Execution, // 1 in top-level Gmsh
16    /// Errors from the Rust/C FFI interface.
17    CInterface, // Problems from the Rust/C FFI interface
18    /// A function that mutates the model couldn't complete successfully.
19    /// For example, addPoint couldn't succeed because of a tag collision.
20    ModelMutation, // 1 in a model
21    /// A data lookup getter function failed.
22    /// For example, tried to work on a view that doesn't exist.
23    ModelLookup, // 2 in a model
24    /// The function couldn't successfully use a required input parameter.
25    /// For example,  a user-specified quadrature scheme couldn't be applied to the data.
26    ModelBadInput, // 3 in a model
27    /// A parallelizable mesh query function failed
28    ModelParallelMeshQuery, // 4 in a model
29    /// The given option doesn't exist in Gmsh.
30    UnknownOption, // 1 in an option function
31    /// Any unexpected error codes in the Gmsh API.
32    UnknownError,
33}
34
35/// Type alias for Result using `GmshError`.
36pub type GmshResult<T> = Result<T, GmshError>;
37
38impl Display for GmshError {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            GmshError::Initialization => {
42                write!(f, "initialization error for Gmsh or an associated library is missing")
43            },
44            _ => write!(f, "big ol error")
45        }
46    }
47}
48
49impl Error for GmshError {}
50
51// Handle error codes from top-level Gmsh functions.
52#[doc(hidden)]
53#[macro_export]
54macro_rules! check_main_error {
55    ($ierr:expr, $return_val: expr) => {
56        match $ierr {
57            0 => Ok($return_val),
58            -1 => Err(GmshError::Initialization),
59            1 => Err(GmshError::Execution),
60            _ => Err(GmshError::UnknownError),
61        }
62    };
63}
64
65// Handle error codes from Gmsh model functions.
66#[doc(hidden)]
67#[macro_export]
68macro_rules! check_model_error {
69    ($ierr:expr, $return_val: expr) => {
70        match $ierr {
71            0 => Ok($return_val),
72            -1 => Err(GmshError::Initialization),
73            1 => Err(GmshError::ModelMutation),
74            2 => Err(GmshError::ModelLookup),
75            3 => Err(GmshError::ModelBadInput),
76            4 => Err(GmshError::ModelParallelMeshQuery),
77            _ => Err(GmshError::UnknownError),
78        }
79    };
80}
81
82// Handle error codes from Gmsh option configuration functions.
83#[doc(hidden)]
84#[macro_export]
85macro_rules! check_option_error {
86    ($ierr:expr, $return_val: expr) => {
87        match $ierr {
88            0 => Ok($return_val),
89            -1 => Err(GmshError::Initialization),
90            1 => Err(GmshError::UnknownOption),
91            _ => Err(GmshError::UnknownError),
92        }
93    };
94}