Skip to main content

ValidatorSignature

Struct ValidatorSignature 

Source
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

Source

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?
examples/validate_named_bench.rs (line 102)
102static S0: &[ValidatorSignature] = &[ValidatorSignature::new(InputType::Text, D0, prepare0)];
103static S1: &[ValidatorSignature] = &[ValidatorSignature::new(InputType::Text, D1, prepare1)];
104static S4: &[ValidatorSignature] = &[ValidatorSignature::new(InputType::Text, D4, prepare4)];
105static S8: &[ValidatorSignature] = &[ValidatorSignature::new(InputType::Text, D8, prepare8)];
106static S9: &[ValidatorSignature] = &[ValidatorSignature::new(InputType::Text, D9, prepare9)];
More examples
Hide additional 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)];
Source

pub const fn input(self) -> InputType

Returns the accepted value shape.

§Returns

The erased input shape this signature accepts.

Source

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.

Source

pub const fn prepare(self) -> PrepareFn

Returns the parameter binding function.

§Returns

The factory function that binds parameters and creates the prepared validator.

Trait Implementations§

Source§

impl Clone for ValidatorSignature

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for ValidatorSignature

Source§

impl Debug for ValidatorSignature

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult

Formats the input shape and dependency count without invoking the factory.

§Parameters
  • formatter: Destination that receives the debug representation.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.