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