Skip to main content

mz_pgrepr/value/
error.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Error type for fallible conversion from [`super::Value`] to [`mz_repr::Datum`].
11
12use std::error::Error;
13use std::fmt;
14
15use mz_repr::adt::array::InvalidArrayError;
16use mz_repr::adt::range::InvalidRangeError;
17
18/// Errors that can occur when converting a [`super::Value`] into a [`mz_repr::Datum`].
19#[derive(Debug)]
20pub enum IntoDatumError {
21    /// Invalid range (e.g. misordered bounds).
22    Range(InvalidRangeError),
23    /// Invalid array (e.g. wrong cardinality or too many dimensions).
24    Array(InvalidArrayError),
25}
26
27impl fmt::Display for IntoDatumError {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            IntoDatumError::Range(e) => e.fmt(f),
31            IntoDatumError::Array(e) => e.fmt(f),
32        }
33    }
34}
35
36impl Error for IntoDatumError {
37    fn source(&self) -> Option<&(dyn Error + 'static)> {
38        match self {
39            IntoDatumError::Range(e) => Some(e),
40            IntoDatumError::Array(e) => Some(e),
41        }
42    }
43}
44
45impl From<InvalidRangeError> for IntoDatumError {
46    fn from(e: InvalidRangeError) -> Self {
47        IntoDatumError::Range(e)
48    }
49}
50
51impl From<InvalidArrayError> for IntoDatumError {
52    fn from(e: InvalidArrayError) -> Self {
53        IntoDatumError::Array(e)
54    }
55}