qubit_value/
value_missing.rs1use std::fmt;
11
12use qubit_datatype::DataType;
13
14#[must_use]
16#[non_exhaustive]
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
18pub enum ValueMissing {
19 UnsetScalar {
21 data_type: DataType,
23 },
24 UnsetCollection {
26 data_type: DataType,
28 },
29 EmptyCollection {
31 data_type: DataType,
33 },
34 EmptyCollectionConversion {
40 to: DataType,
42 },
43 Conversion {
45 from: DataType,
47 to: DataType,
49 },
50 CollectionItem {
52 source_index: usize,
54 from: DataType,
56 to: DataType,
58 },
59}
60
61impl ValueMissing {
62 #[inline(always)]
67 pub const fn source_type(self) -> Option<DataType> {
68 match self {
69 Self::UnsetScalar { data_type }
70 | Self::UnsetCollection { data_type }
71 | Self::EmptyCollection { data_type } => Some(data_type),
72 Self::EmptyCollectionConversion { .. } => None,
73 Self::Conversion { from, .. }
74 | Self::CollectionItem { from, .. } => Some(from),
75 }
76 }
77
78 #[must_use]
80 #[inline(always)]
81 pub const fn target_type(self) -> Option<DataType> {
82 match self {
83 Self::Conversion { to, .. }
84 | Self::CollectionItem { to, .. }
85 | Self::EmptyCollectionConversion { to } => Some(to),
86 Self::UnsetScalar { .. }
87 | Self::UnsetCollection { .. }
88 | Self::EmptyCollection { .. } => None,
89 }
90 }
91
92 #[must_use]
94 #[inline(always)]
95 pub const fn source_index(self) -> Option<usize> {
96 match self {
97 Self::CollectionItem { source_index, .. } => Some(source_index),
98 Self::UnsetScalar { .. }
99 | Self::UnsetCollection { .. }
100 | Self::EmptyCollection { .. }
101 | Self::EmptyCollectionConversion { .. }
102 | Self::Conversion { .. } => None,
103 }
104 }
105
106 #[must_use]
108 #[inline(always)]
109 pub const fn is_unset(self) -> bool {
110 matches!(
111 self,
112 Self::UnsetScalar { .. } | Self::UnsetCollection { .. }
113 )
114 }
115
116 #[must_use]
118 #[inline(always)]
119 pub const fn is_empty_collection(self) -> bool {
120 matches!(
121 self,
122 Self::EmptyCollection { .. }
123 | Self::EmptyCollectionConversion { .. }
124 )
125 }
126
127 #[must_use]
129 #[inline(always)]
130 pub const fn is_conversion(self) -> bool {
131 matches!(
132 self,
133 Self::Conversion { .. }
134 | Self::CollectionItem { .. }
135 | Self::EmptyCollectionConversion { .. }
136 )
137 }
138
139 #[cfg(feature = "converter")]
141 #[must_use]
142 #[inline(always)]
143 pub(crate) const fn is_defaultable_for_conversion(self) -> bool {
144 self.is_unset() || matches!(self, Self::Conversion { .. })
145 }
146}
147
148impl fmt::Display for ValueMissing {
149 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
150 match self {
151 Self::UnsetScalar { data_type } => {
152 write!(formatter, "unset scalar with declared type {data_type}")
153 }
154 Self::UnsetCollection { data_type } => {
155 write!(
156 formatter,
157 "unset collection with declared type {data_type}"
158 )
159 }
160 Self::EmptyCollection { data_type } => {
161 write!(
162 formatter,
163 "empty collection with element type {data_type}"
164 )
165 }
166 Self::Conversion { from, to } => {
167 write!(
168 formatter,
169 "conversion from {from} to {to} produced no value"
170 )
171 }
172 Self::CollectionItem {
173 source_index,
174 from,
175 to,
176 } => write!(
177 formatter,
178 "collection item at index {source_index} conversion from {from} to {to} produced no value"
179 ),
180 Self::EmptyCollectionConversion { to } => write!(
181 formatter,
182 "empty collection conversion to {to} produced no value"
183 ),
184 }
185 }
186}