Skip to main content

PrepareFn

Type Alias PrepareFn 

Source
pub type PrepareFn = for<'a> fn(&mut ArgumentReader<'a>) -> Result<Arc<dyn PreparedValidator>, BindError>;
Expand description

Constructs an owned prepared validator from declaration parameters.

When binding through a descriptor or registry, the binding layer rejects duplicate parameter names before calling the factory and rejects unconsumed parameters after it returns. The factory’s reader calls decide which parameters are required and reject invalid types or ranges. Calling the factory directly does not provide the binding layer’s checks.

§Examples

use std::sync::Arc;

use qubit_validator::{
    ArgumentReader, BindError, BoundValidationContext, ExecutionError,
    PrepareFn, PreparedOutcome, PreparedValidator, ValidationValue,
};

struct Accept;
impl PreparedValidator for Accept {
    fn input_type(&self) -> qubit_validator::InputType {
        qubit_validator::InputType::Text
    }

    fn dependency_specs(&self) -> &'static [qubit_validator::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 factory: PrepareFn = prepare;
let mut reader = ArgumentReader::new(&[])?;
let prepared = factory(&mut reader)?;
reader.finish()?;
assert_eq!(prepared.validate(
    ValidationValue::Text("ready"),
    &BoundValidationContext::new(&[]),
)?, PreparedOutcome::valid());