Skip to main content

antecedent_data/
error.rs

1//! Data-layer errors.
2//!
3//! SPDX-License-Identifier: MIT OR Apache-2.0
4
5use core::fmt;
6
7use antecedent_core::VariableId;
8
9/// Errors from data construction, lookup, or materialization.
10#[derive(Clone, Debug, Eq, PartialEq)]
11#[non_exhaustive]
12pub enum DataError {
13    /// Schema/data length mismatch.
14    LengthMismatch {
15        /// Expected length.
16        expected: usize,
17        /// Actual length.
18        actual: usize,
19        /// Context.
20        context: &'static str,
21    },
22    /// Unknown variable in this table.
23    UnknownVariable {
24        /// Requested id.
25        id: VariableId,
26    },
27    /// Column type does not match the requested view.
28    TypeMismatch {
29        /// Variable id.
30        id: VariableId,
31        /// Expected type label.
32        expected: &'static str,
33    },
34    /// Invalid validity bitmap length.
35    InvalidValidity {
36        /// Explanation.
37        message: &'static str,
38    },
39    /// Row selection produced an empty sample.
40    EmptySelection {
41        /// Explanation.
42        context: &'static str,
43    },
44    /// Temporal gather requires a complete series (no missing values or masked rows).
45    IncompleteSeries {
46        /// Offending variable, when the gap is column-specific.
47        id: Option<VariableId>,
48        /// Explanation.
49        message: &'static str,
50    },
51    /// Invalid argument (split policy, configuration, etc.).
52    InvalidArgument {
53        /// Explanation.
54        message: String,
55    },
56    /// Underlying schema error.
57    Schema(String),
58}
59
60impl fmt::Display for DataError {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        match self {
63            Self::LengthMismatch { expected, actual, context } => {
64                write!(f, "{context}: expected length {expected}, got {actual}")
65            }
66            Self::UnknownVariable { id } => write!(f, "unknown variable {id}"),
67            Self::TypeMismatch { id, expected } => {
68                write!(f, "variable {id} is not of type {expected}")
69            }
70            Self::InvalidValidity { message } => write!(f, "invalid validity: {message}"),
71            Self::EmptySelection { context } => write!(f, "empty selection: {context}"),
72            Self::IncompleteSeries { id, message } => match id {
73                Some(id) => write!(f, "incomplete series (variable {id}): {message}"),
74                None => write!(f, "incomplete series: {message}"),
75            },
76            Self::InvalidArgument { message } => write!(f, "invalid argument: {message}"),
77            Self::Schema(msg) => write!(f, "schema error: {msg}"),
78        }
79    }
80}
81
82impl std::error::Error for DataError {}