Skip to main content

qubit_value/
numeric_comparison_error.rs

1// =============================================================================
2//    Copyright (c) 2025 - 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8
9//! Errors that explain why runtime values cannot be numerically ordered.
10
11use qubit_datatype::DataType;
12use thiserror::Error;
13
14/// Describes why two [`crate::Value`] instances cannot be numerically ordered.
15///
16/// # Examples
17///
18/// ```
19/// use qubit_datatype::NumericComparisonPolicy;
20/// use qubit_value::NumericComparisonError;
21/// use qubit_value::Value;
22///
23/// let error = Value::from("text")
24///     .numeric_cmp(&Value::from(1_i32), NumericComparisonPolicy::Exact)
25///     .unwrap_err();
26/// assert!(matches!(error, NumericComparisonError::LeftNotNumeric { .. }));
27/// ```
28#[must_use]
29#[derive(Debug, Clone, PartialEq, Eq, Error)]
30#[non_exhaustive]
31pub enum NumericComparisonError {
32    /// The left operand is unset but retains a declared type.
33    #[error("left value is missing: declared type is {declared}")]
34    LeftMissing {
35        /// Declared runtime type of the unset left operand.
36        declared: DataType,
37    },
38    /// The right operand is unset but retains a declared type.
39    #[error("right value is missing: declared type is {declared}")]
40    RightMissing {
41        /// Declared runtime type of the unset right operand.
42        declared: DataType,
43    },
44    /// The concrete left operand is not numeric.
45    #[error("left value is not numeric: {actual}")]
46    LeftNotNumeric {
47        /// Actual runtime type of the left operand.
48        actual: DataType,
49    },
50    /// The concrete right operand is not numeric.
51    #[error("right value is not numeric: {actual}")]
52    RightNotNumeric {
53        /// Actual runtime type of the right operand.
54        actual: DataType,
55    },
56    /// Only the left operand is NaN.
57    #[error("left value is NaN")]
58    LeftNaN,
59    /// Only the right operand is NaN.
60    #[error("right value is NaN")]
61    RightNaN,
62    /// Both operands are NaN.
63    #[error("both values are NaN")]
64    BothNaN,
65}