Skip to main content

qubit_value/value_wire/
value_wire_decode_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//! Error reported while decoding bounded JSON wire input.
10// qubit-style: allow multiple-public-types
11
12use thiserror::Error;
13
14/// Shared resource category enforced while decoding one wire document.
15#[non_exhaustive]
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub enum ValueWireLimitKind {
18    /// Complete encoded input length.
19    InputBytes,
20    /// Recursive wire depth.
21    Depth,
22    /// Total decoded node count.
23    Nodes,
24    /// Elements in one collection.
25    CollectionItems,
26    /// Entries in one map.
27    MapEntries,
28    /// Bytes in one decoded string.
29    StringBytes,
30    /// UTF-8 bytes in one decoded numeric representation.
31    NumericBytes,
32}
33
34/// Error produced by a bounded [`crate::ValueWireV1`] JSON decoder.
35#[non_exhaustive]
36#[derive(Debug, Error)]
37pub enum ValueWireDecodeError {
38    /// The input exceeds the configured byte budget.
39    #[error(
40        "wire input contains {input_bytes} bytes, exceeding the {max_input_bytes}-byte limit"
41    )]
42    InputTooLarge {
43        /// Actual input length in bytes.
44        input_bytes: usize,
45        /// Maximum accepted input length in bytes.
46        max_input_bytes: usize,
47    },
48
49    /// The decoded value exceeded one structural resource limit.
50    #[error("wire input {kind:?} value {value} exceeds the limit of {maximum}")]
51    LimitExceeded {
52        /// Resource category that exceeded its limit.
53        kind: ValueWireLimitKind,
54        /// Observed resource value.
55        value: usize,
56        /// Largest permitted resource value.
57        maximum: usize,
58    },
59
60    /// The bounded input is not a valid V1 JSON wire value.
61    #[error("failed to decode V1 JSON wire input: {0}")]
62    InvalidJson(#[from] serde_json::Error),
63}