Skip to main content

qubit_json/
json_decode_error_kind.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the stable error categories returned by the decoder.
10//!
11//! Author: Haixing Hu
12
13use std::fmt;
14
15/// Represents the coarse category of a lenient JSON decoding failure.
16///
17/// This type is intended for callers that need stable, programmatic branching
18/// without depending on full error messages produced by lower-level parsers.
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
20pub enum JsonDecodeErrorKind {
21    /// Indicates that the input became empty after normalization.
22    EmptyInput,
23    /// Indicates that the normalized text is not valid JSON syntax.
24    InvalidJson,
25    /// Indicates that the parsed top-level JSON kind is not the one required
26    /// by the decoding method.
27    UnexpectedTopLevel,
28    /// Indicates that the JSON syntax is valid but the value cannot be
29    /// deserialized into the requested Rust type.
30    Deserialize,
31}
32
33impl fmt::Display for JsonDecodeErrorKind {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        let text = match self {
36            Self::EmptyInput => "empty_input",
37            Self::InvalidJson => "invalid_json",
38            Self::UnexpectedTopLevel => "unexpected_top_level",
39            Self::Deserialize => "deserialize",
40        };
41        f.write_str(text)
42    }
43}