1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use thiserror::Error;
/// Errors that can occur while decoding a query string into an [`crate::Object`].
///
/// This enum is marked `non_exhaustive` so additional variants can be added in
/// future releases without breaking downstream matches.
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum DecodeError {
/// The configured delimiter string was empty.
#[error("delimiter must not be empty")]
EmptyDelimiter,
/// The configured parameter limit was zero.
#[error("parameter limit must be positive")]
InvalidParameterLimit,
/// [`crate::DecodeOptions::decode_dot_in_keys`] was enabled while
/// [`crate::DecodeOptions::allow_dots`] was disabled.
#[error("decode_dot_in_keys requires allow_dots to be true")]
DecodeDotInKeysRequiresAllowDots,
/// The decoded input exceeded the configured parameter limit.
#[error("parameter limit exceeded; only {limit} parameter(s) allowed")]
ParameterLimitExceeded {
/// The configured maximum number of parameters.
limit: usize,
},
/// A decoded list exceeded the configured list limit.
#[error("list limit exceeded; only {limit} element(s) allowed in a list")]
ListLimitExceeded {
/// The configured maximum list length.
limit: usize,
},
/// The decoded nesting depth exceeded the configured limit.
#[error("input depth exceeded depth option of {depth} and strict_depth is true")]
DepthExceeded {
/// The configured maximum decode depth.
depth: usize,
},
#[cfg(feature = "serde")]
/// Serde deserialization failed after the query string was decoded into an
/// intermediate [`crate::Value`] tree.
#[error("serde decode failed: {0}")]
Serde(#[from] serde_json::Error),
}
impl DecodeError {
/// Returns `true` when the error reports that the parameter limit was
/// exceeded.
pub fn is_parameter_limit_exceeded(&self) -> bool {
matches!(self, Self::ParameterLimitExceeded { .. })
}
/// Returns the configured parameter limit for
/// [`Self::ParameterLimitExceeded`].
pub fn parameter_limit(&self) -> Option<usize> {
match self {
Self::ParameterLimitExceeded { limit } => Some(*limit),
_ => None,
}
}
/// Returns `true` when the error reports that a list limit was exceeded.
pub fn is_list_limit_exceeded(&self) -> bool {
matches!(self, Self::ListLimitExceeded { .. })
}
/// Returns the configured list limit for [`Self::ListLimitExceeded`].
pub fn list_limit(&self) -> Option<usize> {
match self {
Self::ListLimitExceeded { limit } => Some(*limit),
_ => None,
}
}
/// Returns `true` when the error reports that the nesting depth limit was
/// exceeded.
pub fn is_depth_exceeded(&self) -> bool {
matches!(self, Self::DepthExceeded { .. })
}
/// Returns the configured depth limit for [`Self::DepthExceeded`].
pub fn depth_limit(&self) -> Option<usize> {
match self {
Self::DepthExceeded { depth } => Some(*depth),
_ => None,
}
}
}
/// Errors that can occur while encoding a [`crate::Value`] tree into a query
/// string.
///
/// This enum is marked `non_exhaustive` so additional variants can be added in
/// future releases without breaking downstream matches.
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum EncodeError {
/// The configured delimiter string was empty.
#[error("delimiter must not be empty")]
EmptyDelimiter,
/// [`crate::EncodeOptions::encode_dot_in_keys`] was enabled while
/// [`crate::EncodeOptions::allow_dots`] was disabled.
#[error("encode_dot_in_keys requires allow_dots to be true")]
EncodeDotInKeysRequiresAllowDots,
/// The encoder exceeded the configured maximum traversal depth.
#[error("encode depth exceeded max_depth option of {depth}")]
DepthExceeded {
/// The configured maximum encode depth.
depth: usize,
},
#[cfg(feature = "serde")]
/// Serde serialization failed before the value could be encoded into a
/// query string.
#[error("serde encode failed: {0}")]
Serde(#[from] serde_json::Error),
}
impl EncodeError {
/// Returns `true` when the delimiter configuration was invalid.
pub fn is_empty_delimiter(&self) -> bool {
matches!(self, Self::EmptyDelimiter)
}
/// Returns `true` when dot-encoding was requested without enabling dot
/// notation.
pub fn is_encode_dot_in_keys_requires_allow_dots(&self) -> bool {
matches!(self, Self::EncodeDotInKeysRequiresAllowDots)
}
/// Returns `true` when the encoder exceeded the configured maximum depth.
pub fn is_depth_exceeded(&self) -> bool {
matches!(self, Self::DepthExceeded { .. })
}
/// Returns the configured depth limit for [`Self::DepthExceeded`].
pub fn depth_limit(&self) -> Option<usize> {
match self {
Self::DepthExceeded { depth } => Some(*depth),
_ => None,
}
}
}
#[cfg(test)]
mod tests;