Skip to main content

qubit_validator/binding/
contextual_fn_validator_adapter.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//! Adapters for text and typed validation closures with dependency context.
10
11use std::sync::Arc;
12
13use super::BoundValidationContext;
14use super::DependencySpec;
15use super::ExecutionError;
16use super::PreparedOutcome;
17use super::PreparedValidator;
18use super::internal::TextContextFnAdapter;
19use super::internal::TypedContextFnAdapter;
20
21/// Prepares a text validation closure that receives its checked dependency
22/// context.
23///
24/// The closure can report a valid input, one or more violation drafts, or an
25/// execution failure. `BoundValidator` checks declared dependencies before it
26/// invokes this adapter. When calling the returned `PreparedValidator`
27/// directly, the closure is responsible for reading and checking its context.
28///
29/// Callers must keep raw rejected input out of violation drafts and execution
30/// errors. Captured data must be owned and thread-safe.
31///
32/// # Type Parameters
33///
34/// - `F`: Thread-safe operation for one text input and its dependency context.
35///
36/// # Parameters
37///
38/// - `dependencies`: Static dependency slots consumed by `call`, in execution
39///   order.
40/// - `call`: Operation invoked with checked text and a borrowed dependency
41///   context.
42///
43/// # Returns
44///
45/// A shared prepared validator that checks the target is text before invoking
46/// `call` and forwards any execution errors from it unchanged. This function
47/// only constructs the adapter and does not itself return an error.
48#[must_use]
49pub fn prepare_text_with_context<F>(dependencies: &'static [DependencySpec], call: F) -> Arc<dyn PreparedValidator>
50where
51    F: for<'a> Fn(&str, &BoundValidationContext<'a>) -> Result<PreparedOutcome, ExecutionError> + Send + Sync + 'static,
52{
53    Arc::new(TextContextFnAdapter::new(call, dependencies))
54}
55
56/// Prepares a typed validation closure that receives its checked dependency
57/// context.
58///
59/// The closure can report a valid input, one or more violation drafts, or an
60/// execution failure. `BoundValidator` checks declared dependencies before it
61/// invokes this adapter. When calling the returned `PreparedValidator`
62/// directly, the closure is responsible for reading and checking its context.
63///
64/// Callers must keep raw rejected input out of violation drafts and execution
65/// errors. Captured data must be owned and thread-safe.
66///
67/// # Type Parameters
68///
69/// - `T`: Exact input type, which must be `'static` for `TypeId`.
70/// - `F`: Thread-safe operation for one typed input and its dependency context.
71///
72/// # Parameters
73///
74/// - `dependencies`: Static dependency slots consumed by `call`, in execution
75///   order.
76/// - `call`: Operation invoked with a checked `T` value and borrowed context.
77///
78/// # Returns
79///
80/// A shared prepared validator that checks the target has type `T` before
81/// invoking `call` and forwards any execution errors from it unchanged. This
82/// function only constructs the adapter and does not itself return an error.
83#[must_use]
84pub fn prepare_typed_with_context<T: 'static, F>(
85    dependencies: &'static [DependencySpec],
86    call: F,
87) -> Arc<dyn PreparedValidator>
88where
89    F: for<'a> Fn(&T, &BoundValidationContext<'a>) -> Result<PreparedOutcome, ExecutionError> + Send + Sync + 'static,
90{
91    Arc::new(TypedContextFnAdapter::new(call, dependencies))
92}