qubit_metadata/metadata_error.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! [`MetadataError`] — failures from explicit metadata APIs and schema checks.
11
12use std::fmt;
13
14use qubit_datatype::DataType;
15use qubit_value::{
16 Value,
17 ValueError,
18};
19
20/// Errors produced by explicit metadata accessors and schema validation.
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub enum MetadataError {
23 /// The requested key does not exist.
24 MissingKey(String),
25 /// A stored value cannot be converted to the requested type.
26 TypeMismatch {
27 /// Metadata key being read or validated.
28 key: String,
29 /// Expected data type.
30 expected: DataType,
31 /// Actual stored data type.
32 actual: DataType,
33 /// Human-readable conversion or validation message.
34 message: String,
35 },
36 /// A required schema field is missing from a metadata object.
37 MissingRequiredField {
38 /// Required metadata key.
39 key: String,
40 /// Expected data type for the missing field.
41 expected: DataType,
42 },
43 /// A metadata object contains a key not accepted by the schema.
44 UnknownField {
45 /// Unknown metadata key.
46 key: String,
47 },
48 /// A filter references a key that is not defined by the schema.
49 UnknownFilterField {
50 /// Unknown filter key.
51 key: String,
52 },
53 /// A filter uses an operator that is not compatible with the field type.
54 InvalidFilterOperator {
55 /// Metadata key being filtered.
56 key: String,
57 /// Filter operator name.
58 operator: &'static str,
59 /// Field data type defined by the schema.
60 data_type: DataType,
61 /// Human-readable validation message.
62 message: String,
63 },
64 /// A filter expression is structurally invalid.
65 InvalidFilterExpression {
66 /// Human-readable validation message.
67 message: String,
68 },
69}
70
71impl MetadataError {
72 /// Builds a conversion error for `key` using the requested type and stored value.
73 #[inline]
74 pub(crate) fn conversion_error(key: &str, expected: DataType, value: &Value, error: ValueError) -> Self {
75 Self::TypeMismatch {
76 key: key.to_string(),
77 expected,
78 actual: value.data_type(),
79 message: error.to_string(),
80 }
81 }
82
83 /// Builds a schema type-mismatch error for `key`.
84 #[inline]
85 pub(crate) fn type_mismatch(key: &str, expected: DataType, actual: DataType) -> Self {
86 Self::TypeMismatch {
87 key: key.to_string(),
88 expected,
89 actual,
90 message: format!("expected {expected}, got {actual}"),
91 }
92 }
93}
94
95impl fmt::Display for MetadataError {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 match self {
98 Self::MissingKey(key) => write!(f, "Metadata key not found: {key}"),
99 Self::TypeMismatch {
100 key,
101 expected,
102 actual,
103 message,
104 } => write!(
105 f,
106 "Metadata key '{key}' expected {expected} but actual {actual}: {message}"
107 ),
108 Self::MissingRequiredField { key, expected } => {
109 write!(f, "Required metadata key '{key}' is missing (expected {expected})")
110 }
111 Self::UnknownField { key } => {
112 write!(f, "Metadata key '{key}' is not defined in schema")
113 }
114 Self::UnknownFilterField { key } => {
115 write!(f, "Metadata filter references key '{key}' not defined in schema")
116 }
117 Self::InvalidFilterOperator {
118 key,
119 operator,
120 data_type,
121 message,
122 } => write!(
123 f,
124 "Metadata filter operator '{operator}' is invalid for key '{key}' with type {data_type}: {message}"
125 ),
126 Self::InvalidFilterExpression { message } => {
127 write!(f, "Metadata filter expression is invalid: {message}")
128 }
129 }
130 }
131}
132
133impl std::error::Error for MetadataError {}