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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// =============================================================================
// Copyright (c) 2025 - 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Adapters for domain validators whose errors need domain-specific mapping.
use Error;
use Arc;
use BoundValidationContext;
use DependencySpec;
use ExecutionError;
use ExecutionErrorKind;
use PreparedOutcome;
use PreparedValidator;
use ViolationDraft;
use prepare_text_with_context;
use prepare_typed_with_context;
/// Prepares a text domain validator with dependency context and error mapping.
///
/// `call` returns the domain result and invocation-specific metadata. The
/// metadata is passed to `map_error` only when the domain result is an error.
/// Infrastructure failures returned directly by `call` are forwarded.
///
/// # Type Parameters
///
/// - `V`: Owned domain validator value used by each invocation.
/// - `E`: Static domain error mapped to safe violation drafts. It is created
/// and immediately mapped within the synchronous call, so it need not be
/// `Send` or `Sync`.
/// - `D`: Invocation metadata created and mapped within one synchronous call.
/// It need not be `Send` or `Sync`; the metadata type remains `'static`.
/// - `Call`: Operation that invokes the validator with text and context.
/// - `Map`: Mapper from a domain error and metadata to violation drafts.
///
/// # Parameters
///
/// - `dependencies`: Static dependency slots consumed by `call`.
/// - `validator`: Domain rule value borrowed by `call` for each validation.
/// - `call`: Operation separating infrastructure failure from domain result.
/// - `map_error`: Converts a domain failure to a non-empty list of safe
/// violations.
///
/// # Returns
///
/// A shared prepared validator that accepts text and exposes declared
/// dependency slots through the context passed to `call`. When invoked through
/// `BoundValidator`, dependency shape is checked before `call`; direct callers
/// of the prepared validator must supply a context suitable for the closure.
///
/// # Runtime Errors
///
/// An empty invalid draft list becomes `AdapterContractViolation` when the
/// returned validator runs.
///
/// # Privacy
///
/// Implementations of `map_error` must map errors to safe codes and parameters;
/// they must not copy raw input or secret error text into a violation.
///
/// # Examples
///
/// ```
/// use std::rc::Rc;
///
/// use qubit_validator::{
/// BoundValidationContext, ExecutionError, PreparedOutcome, PreparedValidator,
/// ValidationValue, ViolationCode, ViolationDraft, prepare_text_domain_rule,
/// };
///
/// #[derive(Debug)]
/// struct Rejected(Rc<u32>);
/// impl std::fmt::Display for Rejected {
/// fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// formatter.write_str("domain rejection")
/// }
/// }
/// impl std::error::Error for Rejected {}
///
/// let prepared = prepare_text_domain_rule::<(), Rejected, (), _, _>(
/// &[],
/// (),
/// |_, value, _| Ok((if value == "ready" { Ok(()) } else { Err(Rejected(Rc::new(1))) }, ())),
/// |error, _| {
/// let _local_error = error.0;
/// vec![ViolationDraft::new(ViolationCode::new("input.rejected"))]
/// },
/// );
/// let context = BoundValidationContext::new(&[]);
/// assert_eq!(
/// prepared.validate(ValidationValue::Text("ready"), &context)?,
/// PreparedOutcome::Valid,
/// );
/// assert!(matches!(
/// prepared.validate(ValidationValue::Text("bad"), &context)?,
/// PreparedOutcome::Invalid(_),
/// ));
/// # Ok::<(), ExecutionError>(())
/// ```
/// Prepares a typed domain validator with dependency context and error mapping.
///
/// `call` returns the domain result and invocation-specific metadata. The
/// metadata is passed to `map_error` only when the domain result is an error.
/// Infrastructure failures returned directly by `call` are forwarded.
///
/// # Type Parameters
///
/// - `T`: Exact `'static` input type accepted by the domain validator.
/// - `V`: Owned domain validator value used by each invocation.
/// - `E`: Static domain error mapped to safe violation drafts. It is created
/// and immediately mapped within the synchronous call, so it need not be
/// `Send` or `Sync`.
/// - `D`: Invocation metadata created and mapped within one synchronous call.
/// It need not be `Send` or `Sync`; the metadata type remains `'static`.
/// - `Call`: Operation that invokes the validator with `T` and context.
/// - `Map`: Mapper from a domain error and metadata to violation drafts.
///
/// # Parameters
///
/// - `dependencies`: Static dependency slots consumed by `call`.
/// - `validator`: Domain rule value borrowed by `call` for each validation.
/// - `call`: Operation separating infrastructure failure from domain result.
/// - `map_error`: Converts a domain failure to a non-empty list of safe
/// violations.
///
/// # Returns
///
/// A shared prepared validator that accepts `T` and exposes declared
/// dependency slots through the context passed to `call`. When invoked through
/// `BoundValidator`, dependency shape is checked before `call`; direct callers
/// of the prepared validator must supply a context suitable for the closure.
///
/// # Runtime Errors
///
/// An empty invalid draft list becomes `AdapterContractViolation` when the
/// returned validator runs.
///
/// # Privacy
///
/// Implementations of `map_error` must map errors to safe codes and parameters;
/// they must not copy raw input or secret error text into a violation.
///
/// # Examples
///
/// ```
/// use std::rc::Rc;
///
/// use qubit_validator::{
/// BoundValidationContext, ExecutionError, PreparedOutcome, PreparedValidator,
/// ValidationValue, ViolationCode, ViolationDraft, prepare_typed_domain_rule,
/// };
///
/// #[derive(Debug)]
/// struct Rejected(Rc<u32>);
/// impl std::fmt::Display for Rejected {
/// fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// formatter.write_str("domain rejection")
/// }
/// }
/// impl std::error::Error for Rejected {}
///
/// let prepared = prepare_typed_domain_rule::<u32, _, _, (), _, _>(
/// &[],
/// (),
/// |_, value, _| Ok((if *value == 7 { Ok(()) } else { Err(Rejected(Rc::new(1))) }, ())),
/// |error, _| {
/// let _local_error = error.0;
/// vec![ViolationDraft::new(ViolationCode::new("number.rejected"))]
/// },
/// );
/// let context = BoundValidationContext::new(&[]);
/// assert_eq!(
/// prepared.validate(ValidationValue::Typed(&7_u32), &context)?,
/// PreparedOutcome::Valid,
/// );
/// assert!(matches!(
/// prepared.validate(ValidationValue::Typed(&8_u32), &context)?,
/// PreparedOutcome::Invalid(_),
/// ));
/// # Ok::<(), ExecutionError>(())
/// ```
/// Maps a domain call result into the prepared validator protocol.
///
/// Successful domain results are valid. Failed results are passed with their
/// invocation metadata to the mapper; an empty invalid draft list is rejected
/// as an adapter contract violation.
///
/// # Type Parameters
///
/// - `E`: Domain error consumed by the error mapper.
/// - `D`: Invocation metadata borrowed by the error mapper on domain failure.
///
/// # Parameters
///
/// - `result`: Domain result paired with metadata from the same invocation.
/// - `map_error`: Maps a domain error and its metadata to violation drafts.
///
/// # Returns
///
/// A valid outcome for domain success, or an invalid outcome containing the
/// mapped violation drafts for domain failure.
///
/// # Errors
///
/// Returns `AdapterContractViolation` when a domain failure maps to no
/// violation drafts.
,
map_error: &