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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// =============================================================================
// Copyright (c) 2025 - 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Bounds and caller-defined argument validation.
use Range;
use crateArgumentError;
use crateArgumentErrorKind;
use crateArgumentResult;
use crateIndexRole;
/// Validates a value with a caller-provided predicate.
///
/// The predicate receives a shared reference to `value`. If it returns `true`,
/// this function returns the original value without cloning it or allocating
/// error strings. If it returns `false`, `path`, `code`, and `message` identify
/// the failed validation rule.
///
/// # Parameters
///
/// - `value`: the value to validate and return unchanged on success.
/// - `path`: the argument path to attach to a validation error.
/// - `predicate`: a one-shot check that borrows `value`.
/// - `code`: the custom error code copied into an error if validation fails.
/// - `message`: the custom error message copied into an error if validation
/// fails.
///
/// # Returns
///
/// The original `value` when the predicate succeeds.
///
/// # Errors
///
/// Returns [`ArgumentErrorKind::Custom`] when `predicate` returns `false`.
/// The error owns copies of `path`, `code`, and `message`.
/// Validates that an offset and length fit within a total length.
///
/// `offset` may equal `total_length` only when `length` is zero. Validation
/// checks the offset before subtracting it from the total, so it never computes
/// an unchecked sum of the offset and length.
///
/// # Parameters
///
/// - `path`: the argument path to attach to a bounds error.
/// - `offset`: the starting position, which must not exceed `total_length`.
/// - `length`: the requested span, which must fit after `offset`.
/// - `total_length`: the total number of units available.
///
/// # Returns
///
/// `()` when the requested span fits within the total length.
///
/// # Errors
///
/// Returns [`ArgumentErrorKind::Bounds`] when `offset` exceeds `total_length`
/// or when `length` exceeds the number of units remaining after `offset`.
/// Validates an index that must identify an existing element.
///
/// `index` is valid exactly when it is strictly less than `size`. On success,
/// the validated index is returned unchanged.
///
/// # Parameters
///
/// - `path`: the argument path to attach to an index error.
/// - `index`: the element index to validate.
/// - `size`: the number of elements available.
///
/// # Returns
///
/// The validated `index` unchanged.
///
/// # Errors
///
/// Returns [`ArgumentErrorKind::Index`] with [`IndexRole::Element`] when
/// `index` is greater than or equal to `size`.
/// Validates an index that identifies a boundary position.
///
/// `index` is valid when it is less than or equal to `size`, including the
/// position immediately after the final element. On success, the validated
/// index is returned unchanged.
///
/// # Parameters
///
/// - `path`: the argument path to attach to an index error.
/// - `index`: the boundary position to validate.
/// - `size`: the number of elements, and therefore the greatest valid position.
///
/// # Returns
///
/// The validated `index` unchanged.
///
/// # Errors
///
/// Returns [`ArgumentErrorKind::Index`] with [`IndexRole::Position`] when
/// `index` exceeds `size`.
/// Validates a half-open range of boundary positions.
///
/// `start` and `end` are valid when `start <= end <= size`. Equal endpoints
/// are accepted and produce an empty range. On success, this function returns
/// the validated `start..end` range.
///
/// # Parameters
///
/// - `path`: the argument path to attach to an index-range error.
/// - `start`: the inclusive beginning boundary.
/// - `end`: the exclusive ending boundary.
/// - `size`: the number of elements, which bounds both positions.
///
/// # Returns
///
/// The validated half-open range `start..end`.
///
/// # Errors
///
/// Returns [`ArgumentErrorKind::IndexRange`] when `start` exceeds `end` or
/// when `end` exceeds `size`.