pub struct ValidatorSignature { /* private fields */ }Expand description
One statically declared way to bind a validator.
§Examples
use std::sync::Arc;
use qubit_validator::ArgumentReader;
use qubit_validator::BindError;
use qubit_validator::BoundValidationContext;
use qubit_validator::DependencySpec;
use qubit_validator::ExecutionError;
use qubit_validator::InputType;
use qubit_validator::PreparedOutcome;
use qubit_validator::PreparedValidator;
use qubit_validator::ValidationValue;
use qubit_validator::ValidatorSignature;
struct Accept;
impl PreparedValidator for Accept {
fn input_type(&self) -> InputType { InputType::Text }
fn dependency_specs(&self) -> &'static [DependencySpec] { &[] }
fn validate(
&self,
_: ValidationValue<'_>,
_: &BoundValidationContext<'_>,
) -> Result<PreparedOutcome, ExecutionError> {
Ok(PreparedOutcome::valid())
}
}
fn prepare(_reader: &mut ArgumentReader<'_>) -> Result<Arc<dyn PreparedValidator>, BindError> {
Ok(Arc::new(Accept))
}
let signature = ValidatorSignature::new(InputType::Text, &[], prepare);
assert_eq!(signature.input(), InputType::Text);
assert!(signature.dependencies().is_empty());Implementations§
Source§impl ValidatorSignature
impl ValidatorSignature
Sourcepub const fn new(
input: InputType,
dependencies: &'static [DependencySpec],
prepare: PrepareFn,
) -> Self
pub const fn new( input: InputType, dependencies: &'static [DependencySpec], prepare: PrepareFn, ) -> Self
Creates a signature with ordered dependency slots.
§Parameters
input: Erased input shape accepted by the validator.dependencies: Dependency slots passed to the prepared validator, in order.prepare: Function that binds arguments and creates the prepared validator.
§Returns
A signature containing the supplied input shape, dependencies, and factory.
Examples found in repository?
More examples
examples/local_registry.rs (line 75)
75static SIGNATURES: &[ValidatorSignature] = &[ValidatorSignature::new(InputType::Text, &[], prepare_non_blank)];
76static DESCRIPTOR: ValidatorDescriptor = ValidatorDescriptor::new(SIGNATURES);
77
78#[cfg(feature = "inventory")]
79register_validator!(id = "text.non_blank.global", descriptor = &DESCRIPTOR);
80
81static DEPENDENCIES: &[DependencySpec] = &[
82 DependencySpec::new("minimum", InputType::Text, false),
83 DependencySpec::new("maximum", InputType::Text, false),
84];
85
86struct CheckDependencySlots;
87
88impl PreparedValidator for CheckDependencySlots {
89 fn input_type(&self) -> InputType {
90 InputType::Text
91 }
92 fn dependency_specs(&self) -> &'static [DependencySpec] {
93 DEPENDENCIES
94 }
95
96 fn validate(
97 &self,
98 _: ValidationValue<'_>,
99 context: &BoundValidationContext<'_>,
100 ) -> Result<PreparedOutcome, ExecutionError> {
101 let valid = context.value(0)?.as_text() == Some("minimum") && context.value(1)?.as_text() == Some("maximum");
102 Ok(if valid {
103 PreparedOutcome::Valid
104 } else {
105 PreparedOutcome::Invalid(vec![ViolationDraft::new(ViolationCode::new(
106 "text.dependencies_mismatch",
107 ))])
108 })
109 }
110}
111
112fn prepare_dependency_slots(_reader: &mut ArgumentReader<'_>) -> Result<Arc<dyn PreparedValidator>, BindError> {
113 Ok(Arc::new(CheckDependencySlots))
114}
115
116static DEPENDENCY_SIGNATURES: &[ValidatorSignature] = &[ValidatorSignature::new(
117 InputType::Text,
118 DEPENDENCIES,
119 prepare_dependency_slots,
120)];Sourcepub const fn dependencies(self) -> &'static [DependencySpec]
pub const fn dependencies(self) -> &'static [DependencySpec]
Returns dependency declarations in slot order.
§Returns
The static dependency slice in the order passed to the prepared validator.
Trait Implementations§
Source§impl Clone for ValidatorSignature
impl Clone for ValidatorSignature
impl Copy for ValidatorSignature
Auto Trait Implementations§
impl Forget for ValidatorSignature
impl Freeze for ValidatorSignature
impl RefUnwindSafe for ValidatorSignature
impl Send for ValidatorSignature
impl Sync for ValidatorSignature
impl Unpin for ValidatorSignature
impl UnsafeUnpin for ValidatorSignature
impl UnwindSafe for ValidatorSignature
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more