Skip to main content

molrs/compute/
error.rs

1use std::fmt;
2
3use crate::MolRsError;
4
5/// Error type for compute operations.
6#[derive(Debug)]
7pub enum ComputeError {
8    /// Required block not found in Frame.
9    MissingBlock { name: &'static str },
10
11    /// Required column not found in a block.
12    MissingColumn {
13        block: &'static str,
14        col: &'static str,
15    },
16
17    /// Frame has no SimBox but the compute requires one.
18    MissingSimBox,
19
20    /// Array dimensions do not match expectations.
21    DimensionMismatch { expected: usize, got: usize },
22
23    /// Reducer queried before any frames were fed.
24    NoFrames,
25
26    /// Forwarded from molrs-core.
27    MolRs(MolRsError),
28}
29
30impl fmt::Display for ComputeError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Self::MissingBlock { name } => {
34                write!(f, "missing block '{name}' in Frame")
35            }
36            Self::MissingColumn { block, col } => {
37                write!(f, "missing column '{col}' in block '{block}'")
38            }
39            Self::MissingSimBox => write!(f, "Frame has no SimBox"),
40            Self::DimensionMismatch { expected, got } => {
41                write!(f, "dimension mismatch: expected {expected}, got {got}")
42            }
43            Self::NoFrames => write!(f, "no frames have been fed to the reducer"),
44            Self::MolRs(e) => write!(f, "{e}"),
45        }
46    }
47}
48
49impl std::error::Error for ComputeError {
50    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
51        match self {
52            Self::MolRs(e) => Some(e),
53            _ => None,
54        }
55    }
56}
57
58impl From<MolRsError> for ComputeError {
59    fn from(err: MolRsError) -> Self {
60        Self::MolRs(err)
61    }
62}