1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// =============================================================================
// Copyright (c) 2025 - 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Structured reasons why a value read produced no concrete item.
use std::fmt;
use qubit_datatype::DataType;
/// Describes the typed state that produced a missing-value error.
#[must_use]
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ValueMissing {
/// A scalar was unset with a declared data type.
UnsetScalar {
/// Type retained by the unset scalar storage.
data_type: DataType,
},
/// A collection was unset with a declared element type.
UnsetCollection {
/// Element type retained by the unset collection storage.
data_type: DataType,
},
/// A concrete collection contains no item for a first-item read.
EmptyCollection {
/// Element type of the concrete empty collection.
data_type: DataType,
},
/// A conversion requested one item from an empty collection.
///
/// The source collection has no item and therefore no source value type
/// can be recovered from the shared conversion error. `to` records the
/// requested target type instead of overloading `EmptyCollection`.
EmptyCollectionConversion {
/// Requested target data type.
to: DataType,
},
/// A conversion policy treated a concrete scalar as missing.
Conversion {
/// Declared source data type.
from: DataType,
/// Requested target data type.
to: DataType,
},
/// A collection item conversion produced no value.
CollectionItem {
/// Original zero-based source position.
source_index: usize,
/// Declared source data type.
from: DataType,
/// Requested target data type.
to: DataType,
},
}
impl ValueMissing {
/// Returns the source or declared data type associated with the error.
///
/// Returns `None` for [`Self::EmptyCollectionConversion`] because no source
/// item exists for that conversion.
#[inline(always)]
pub const fn source_type(self) -> Option<DataType> {
match self {
Self::UnsetScalar { data_type }
| Self::UnsetCollection { data_type }
| Self::EmptyCollection { data_type } => Some(data_type),
Self::EmptyCollectionConversion { .. } => None,
Self::Conversion { from, .. }
| Self::CollectionItem { from, .. } => Some(from),
}
}
/// Returns the requested target type for conversion failures.
#[must_use]
#[inline(always)]
pub const fn target_type(self) -> Option<DataType> {
match self {
Self::Conversion { to, .. }
| Self::CollectionItem { to, .. }
| Self::EmptyCollectionConversion { to } => Some(to),
Self::UnsetScalar { .. }
| Self::UnsetCollection { .. }
| Self::EmptyCollection { .. } => None,
}
}
/// Returns the source index for a missing collection item.
#[must_use]
#[inline(always)]
pub const fn source_index(self) -> Option<usize> {
match self {
Self::CollectionItem { source_index, .. } => Some(source_index),
Self::UnsetScalar { .. }
| Self::UnsetCollection { .. }
| Self::EmptyCollection { .. }
| Self::EmptyCollectionConversion { .. }
| Self::Conversion { .. } => None,
}
}
/// Reports whether storage itself is unset.
#[must_use]
#[inline(always)]
pub const fn is_unset(self) -> bool {
matches!(
self,
Self::UnsetScalar { .. } | Self::UnsetCollection { .. }
)
}
/// Reports whether a concrete collection is empty.
#[must_use]
#[inline(always)]
pub const fn is_empty_collection(self) -> bool {
matches!(
self,
Self::EmptyCollection { .. }
| Self::EmptyCollectionConversion { .. }
)
}
/// Reports whether the missing value came from a conversion.
#[must_use]
#[inline(always)]
pub const fn is_conversion(self) -> bool {
matches!(
self,
Self::Conversion { .. }
| Self::CollectionItem { .. }
| Self::EmptyCollectionConversion { .. }
)
}
/// Reports whether conversion APIs may use a caller-provided fallback.
#[cfg(feature = "converter")]
#[must_use]
#[inline(always)]
pub(crate) const fn is_defaultable_for_conversion(self) -> bool {
self.is_unset() || matches!(self, Self::Conversion { .. })
}
}
impl fmt::Display for ValueMissing {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnsetScalar { data_type } => {
write!(formatter, "unset scalar with declared type {data_type}")
}
Self::UnsetCollection { data_type } => {
write!(
formatter,
"unset collection with declared type {data_type}"
)
}
Self::EmptyCollection { data_type } => {
write!(
formatter,
"empty collection with element type {data_type}"
)
}
Self::Conversion { from, to } => {
write!(
formatter,
"conversion from {from} to {to} produced no value"
)
}
Self::CollectionItem {
source_index,
from,
to,
} => write!(
formatter,
"collection item at index {source_index} conversion from {from} to {to} produced no value"
),
Self::EmptyCollectionConversion { to } => write!(
formatter,
"empty collection conversion to {to} produced no value"
),
}
}
}