Skip to main content

qubit_json/encode/
json_serialization_error_kind.rs

1// =============================================================================
2//    Copyright (c) 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! Defines precise, privacy-safe strict JSON serialization failures.
9
10use thiserror::Error;
11
12use super::JsonCollectionKind;
13use super::JsonIntegerSignedness;
14use super::JsonMapKeyKind;
15use super::JsonSerializerStateError;
16
17/// Precise reason why a Serde value could not become strict JSON.
18///
19/// # Examples
20///
21/// ```
22/// use qubit_json::encode::JsonIntegerSignedness;
23/// use qubit_json::encode::JsonSerializationErrorKind;
24/// use qubit_json::value::JsonValueEncoder;
25///
26/// let error = JsonValueEncoder::new()
27///     .encode(&u128::MAX)
28///     .expect_err("wide integer must be rejected");
29/// assert_eq!(
30///     error.kind(),
31///     JsonSerializationErrorKind::IntegerOutOfRange {
32///         signedness: JsonIntegerSignedness::Unsigned,
33///     },
34/// );
35/// ```
36#[must_use]
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Error)]
38pub enum JsonSerializationErrorKind {
39    /// A signed or unsigned integer exceeded the strict 64-bit union range.
40    #[error("JSON integer is outside the supported 64-bit range")]
41    IntegerOutOfRange {
42        /// Signedness of the rejected Serde integer entry point.
43        signedness: JsonIntegerSignedness,
44    },
45    /// A floating-point value was NaN or infinite.
46    #[error("non-finite float")]
47    NonFiniteFloat,
48    /// A finite number unexpectedly could not become a serde_json number.
49    #[error("invalid JSON number representation")]
50    InvalidNumberRepresentation,
51    /// A Serde value shape cannot be represented as an object key.
52    #[error("unsupported JSON object key")]
53    UnsupportedMapKey {
54        /// Rejected non-sensitive Serde shape.
55        kind: JsonMapKeyKind,
56    },
57    /// Two source entries produced the same JSON object key.
58    #[error("duplicate JSON object key")]
59    DuplicateObjectKey,
60    /// A serde_json RawValue payload violated the strict value contract.
61    #[error("invalid raw JSON value")]
62    InvalidRawValue,
63    /// A JSON collection count overflowed the platform representation.
64    #[error("JSON collection length overflow")]
65    CollectionLengthOverflow {
66        /// Collection whose count overflowed.
67        kind: JsonCollectionKind,
68    },
69    /// A hand-written Serialize implementation violated compound state rules.
70    #[error("invalid serializer state")]
71    InvalidSerializerState {
72        /// Exact privacy-safe state violation.
73        reason: JsonSerializerStateError,
74    },
75    /// A Display implementation rejected fallible formatting.
76    #[error("display formatting failed during JSON serialization")]
77    DisplayFormattingFailed,
78    /// An external serializer returned opaque custom failure text.
79    #[error("custom JSON serialization failed")]
80    CustomSerialization,
81}