Skip to main content

ValidatorRegistration

Struct ValidatorRegistration 

Source
pub struct ValidatorRegistration { /* private fields */ }
Expand description

One validator definition associated with a stable identifier.

§Examples

use std::sync::Arc;

use qubit_validator::{
    ArgumentReader, BindError, BoundValidationContext, ExecutionError, InputType,
    PreparedOutcome, PreparedValidator, RegistrationSource, ValidationValue, ValidatorDescriptor,
    ValidatorId, ValidatorRegistration, ValidatorSignature,
};

struct Accept;
impl PreparedValidator for Accept {
    fn input_type(&self) -> InputType { 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))
}
static SIGNATURES: &[ValidatorSignature] = &[
    ValidatorSignature::new(InputType::Text, &[], prepare),
];
static DESCRIPTOR: ValidatorDescriptor = ValidatorDescriptor::new(SIGNATURES);

let registration = ValidatorRegistration::new(
    ValidatorId::new("text.non_blank"),
    &DESCRIPTOR,
    RegistrationSource::new("rules", "rules::text", "src/text.rs", 12),
);
assert_eq!(registration.id().as_str(), "text.non_blank");

Implementations§

Source§

impl ValidatorRegistration

Source

pub const fn new( id: ValidatorId, descriptor: &'static ValidatorDescriptor, source: RegistrationSource, ) -> Self

Creates a validator registration from its identifier, descriptor, and source.

id is the stable validator identifier, descriptor defines the supported signatures, and source records where the registration was declared. The descriptor must live for the duration of the program. This constructor only stores the supplied values and does not access the file system. It can be used for local registrations as well as the registrations generated by register_validator!.

§Parameters
  • id: Stable identifier claimed by the registration.
  • descriptor: Static definition of the supported signatures.
  • source: Compile-time metadata identifying the declaration site.
§Returns

A registration containing the supplied identifier, descriptor, and source metadata.

Examples found in repository?
examples/local_registry.rs (lines 151-155)
150fn main() -> Result<(), Box<dyn Error>> {
151    let registration = ValidatorRegistration::new(
152        ValidatorId::new("text.non_blank"),
153        &DESCRIPTOR,
154        RegistrationSource::new(env!("CARGO_PKG_NAME"), module_path!(), file!(), line!()),
155    );
156    let registry = ValidatorRegistry::from_registrations([registration])?;
157
158    assert!(registry.get("text.non_blank").is_some());
159    let validator = registry.bind("text.non_blank", InputType::Text, &[])?;
160    let context = BoundValidationContext::new(&[]);
161
162    let valid = validator.validate(ValidationValue::Text("Ada"), &context)?;
163    assert_eq!(valid, ValidationOutcome::Valid);
164
165    let invalid = validator.validate(ValidationValue::Text("   "), &context)?;
166    let ValidationOutcome::Invalid(violations) = invalid else {
167        panic!("blank text must be invalid");
168    };
169    assert_eq!(violations.len(), 1);
170    assert_eq!(violations[0].code(), ViolationCode::new("text.blank"));
171
172    assert_dependency_specs_come_from_signature()?;
173    assert_parameters_are_consumed_once()?;
174
175    #[cfg(feature = "inventory")]
176    {
177        let global = ValidatorRegistry::try_global()?;
178        assert!(global.get("text.non_blank.global").is_some());
179    }
180
181    Ok(())
182}
Source

pub const fn id(&self) -> ValidatorId

Returns the stable identifier.

§Returns

The identifier claimed by this registration.

Source

pub const fn descriptor(&self) -> &'static ValidatorDescriptor

Returns the immutable descriptor.

§Returns

The static descriptor defining this registration’s supported signatures.

Source

pub const fn source(&self) -> RegistrationSource

Returns the source location used for duplicate diagnostics.

§Returns

The compile-time source metadata associated with this registration.

Trait Implementations§

Source§

impl Clone for ValidatorRegistration

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 ValidatorRegistration

Source§

impl Debug for ValidatorRegistration

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&ValidatorRegistration> for ValidatorRegistration

Source§

fn from(value: &ValidatorRegistration) -> Self

Copies a registration borrowed from a static-compatible reference.

§Parameters
  • value: Registration whose copy is returned.

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.