Skip to main content

fast_mesh_fields/
error.rs

1//! Errors reported by field kernels.
2
3use std::fmt;
4
5use fast_mesh_query::QueryError;
6use fast_mesh_store::StoreError;
7
8/// Why a field kernel could not run.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum FieldsError {
11    /// An argument was invalid (non-finite point, mismatched lengths, stale
12    /// sign data and so on); the message says which.
13    InvalidInput(&'static str),
14    /// Memory for the result could not be reserved.
15    OutOfMemory,
16    /// The mesh store failed, for example while building adjacency.
17    Store(StoreError),
18    /// A query failed, for example because the BVH is stale.
19    Query(QueryError),
20}
21
22impl fmt::Display for FieldsError {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        match self {
25            FieldsError::InvalidInput(what) => write!(f, "invalid input: {what}"),
26            FieldsError::OutOfMemory => f.write_str("out of memory"),
27            FieldsError::Store(e) => write!(f, "store error: {e}"),
28            FieldsError::Query(e) => write!(f, "query error: {e}"),
29        }
30    }
31}
32
33impl std::error::Error for FieldsError {
34    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
35        match self {
36            FieldsError::Store(e) => Some(e),
37            FieldsError::Query(e) => Some(e),
38            _ => None,
39        }
40    }
41}
42
43impl From<StoreError> for FieldsError {
44    fn from(e: StoreError) -> Self {
45        match e {
46            StoreError::OutOfMemory => FieldsError::OutOfMemory,
47            e => FieldsError::Store(e),
48        }
49    }
50}
51
52impl From<QueryError> for FieldsError {
53    fn from(e: QueryError) -> Self {
54        match e {
55            QueryError::OutOfMemory => FieldsError::OutOfMemory,
56            e => FieldsError::Query(e),
57        }
58    }
59}
60
61/// `Result` with [`FieldsError`] as the error type.
62pub type Result<T> = std::result::Result<T, FieldsError>;