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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Error)]
37pub enum JsonSerializationErrorKind {
38    /// A signed or unsigned integer exceeded the strict 64-bit union range.
39    #[error("JSON integer is outside the supported 64-bit range")]
40    IntegerOutOfRange {
41        /// Signedness of the rejected Serde integer entry point.
42        signedness: JsonIntegerSignedness,
43    },
44    /// A floating-point value was NaN or infinite.
45    #[error("non-finite float")]
46    NonFiniteFloat,
47    /// A finite number unexpectedly could not become a serde_json number.
48    #[error("invalid JSON number representation")]
49    InvalidNumberRepresentation,
50    /// A Serde value shape cannot be represented as an object key.
51    #[error("unsupported JSON object key")]
52    UnsupportedMapKey {
53        /// Rejected non-sensitive Serde shape.
54        kind: JsonMapKeyKind,
55    },
56    /// Two source entries produced the same JSON object key.
57    #[error("duplicate JSON object key")]
58    DuplicateObjectKey,
59    /// A serde_json RawValue payload violated the strict value contract.
60    #[error("invalid raw JSON value")]
61    InvalidRawValue,
62    /// A JSON collection count overflowed the platform representation.
63    #[error("JSON collection length overflow")]
64    CollectionLengthOverflow {
65        /// Collection whose count overflowed.
66        kind: JsonCollectionKind,
67    },
68    /// A hand-written Serialize implementation violated compound state rules.
69    #[error("invalid serializer state")]
70    InvalidSerializerState {
71        /// Exact privacy-safe state violation.
72        reason: JsonSerializerStateError,
73    },
74    /// A Display implementation rejected fallible formatting.
75    #[error("display formatting failed during JSON serialization")]
76    DisplayFormattingFailed,
77    /// An external serializer returned opaque custom failure text.
78    #[error("custom JSON serialization failed")]
79    CustomSerialization,
80}