Skip to main content

molrs_core/block/
error.rs

1//! Error types for Block operations.
2
3use std::fmt;
4
5/// Errors that can occur when manipulating a [`Block`](super::Block).
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum BlockError {
8    /// Inserted array has rank 0 (no axis-0 length can be defined)
9    RankZero {
10        /// The key for which the error occurred
11        key: String,
12    },
13    /// Inserted array's axis-0 length does not match the Block's `nrows`
14    RaggedAxis0 {
15        /// The key for which the mismatch was detected
16        key: String,
17        /// The axis-0 length the block expects
18        expected: usize,
19        /// The axis-0 length provided by the inserted array
20        got: usize,
21    },
22    /// General validation error
23    Validation {
24        /// Error message
25        message: String,
26    },
27}
28
29impl fmt::Display for BlockError {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match self {
32            BlockError::RankZero { key } => {
33                write!(
34                    f,
35                    "array for key '{}' has rank 0; expected at least 1D",
36                    key
37                )
38            }
39            BlockError::RaggedAxis0 { key, expected, got } => write!(
40                f,
41                "array for key '{}' has axis-0 length {} but block expects {}",
42                key, got, expected
43            ),
44            BlockError::Validation { message } => write!(f, "{}", message),
45        }
46    }
47}
48
49impl std::error::Error for BlockError {}
50
51impl BlockError {
52    /// Creates a validation error with the given message.
53    pub fn validation(message: impl Into<String>) -> Self {
54        BlockError::Validation {
55            message: message.into(),
56        }
57    }
58}