Skip to main content

qubit_validator/binding/
text_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//! Adapter from typed text validators to the erased validation boundary.
10
11use std::sync::Arc;
12
13use super::PreparedValidator;
14use super::internal::TextValidatorAdapter;
15use crate::Validator;
16use crate::ViolationDraft;
17
18/// Prepares a text validator using a domain-error mapper.
19///
20/// The returned adapter rejects non-text erased inputs and converts each domain
21/// error with `map_error`; callers should ensure the mapper does not retain raw
22/// input in the resulting draft.
23///
24/// # Type Parameters
25///
26/// - `V`: Validator of borrowed text with unit context.
27/// - `M`: Mapper from the validator error to a safe violation draft.
28///
29/// # Panics
30///
31/// This function does not panic. The prepared adapter reports an input-shape
32/// error if it receives a non-text value.
33///
34/// # Parameters
35///
36/// - `validator`: Thread-safe validator for borrowed text with unit context.
37/// - `map_error`: Converts each domain error to safe violation metadata.
38///
39/// # Returns
40///
41/// A shared prepared adapter that checks for text input before validation.
42#[must_use]
43pub fn prepare_text_validator<V, M>(validator: V, map_error: M) -> Arc<dyn PreparedValidator>
44where
45    V: Validator<str, ()> + Send + Sync + 'static,
46    M: Fn(V::Error) -> ViolationDraft + Send + Sync + 'static,
47{
48    Arc::new(TextValidatorAdapter::new(validator, map_error))
49}