qubit_validator/registry/validator_registration.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//! Validator registrations for the next-generation registry.
10
11use super::ValidatorDescriptor;
12use crate::RegistrationSource;
13use crate::ValidatorId;
14
15/// One validator definition associated with a stable identifier.
16///
17/// # Examples
18///
19/// ```
20/// use std::sync::Arc;
21///
22/// use qubit_validator::{
23/// ArgumentReader, BindError, BoundValidationContext, ExecutionError, InputType,
24/// PreparedOutcome, PreparedValidator, RegistrationSource, ValidationValue, ValidatorDescriptor,
25/// ValidatorId, ValidatorRegistration, ValidatorSignature,
26/// };
27///
28/// struct Accept;
29/// impl PreparedValidator for Accept {
30/// fn input_type(&self) -> InputType { InputType::Text }
31/// fn dependency_specs(&self) -> &'static [qubit_validator::DependencySpec] { &[] }
32/// fn validate(
33/// &self,
34/// _: ValidationValue<'_>,
35/// _: &BoundValidationContext<'_>,
36/// ) -> Result<PreparedOutcome, ExecutionError> {
37/// Ok(PreparedOutcome::valid())
38/// }
39/// }
40/// fn prepare(_reader: &mut ArgumentReader<'_>) -> Result<Arc<dyn PreparedValidator>, BindError> {
41/// Ok(Arc::new(Accept))
42/// }
43/// static SIGNATURES: &[ValidatorSignature] = &[
44/// ValidatorSignature::new(InputType::Text, &[], prepare),
45/// ];
46/// static DESCRIPTOR: ValidatorDescriptor = ValidatorDescriptor::new(SIGNATURES);
47///
48/// let registration = ValidatorRegistration::new(
49/// ValidatorId::new("text.non_blank"),
50/// &DESCRIPTOR,
51/// RegistrationSource::new("rules", "rules::text", "src/text.rs", 12),
52/// );
53/// assert_eq!(registration.id().as_str(), "text.non_blank");
54/// ```
55#[derive(Clone, Copy, Debug)]
56pub struct ValidatorRegistration {
57 /// Stable identifier claimed by the registration.
58 id: ValidatorId,
59 /// Immutable descriptor for every supported input shape.
60 descriptor: &'static ValidatorDescriptor,
61 /// Compile-time source metadata used in duplicate diagnostics.
62 source: RegistrationSource,
63}
64
65impl ValidatorRegistration {
66 /// Creates a validator registration from its identifier, descriptor, and
67 /// source.
68 ///
69 /// `id` is the stable validator identifier, `descriptor` defines the
70 /// supported signatures, and `source` records where the registration was
71 /// declared. The descriptor must live for the duration of the program.
72 /// This constructor only stores the supplied values and does not access
73 /// the file system. It can be used for local registrations as well as the
74 /// registrations generated by `register_validator!`.
75 ///
76 /// # Parameters
77 ///
78 /// - `id`: Stable identifier claimed by the registration.
79 /// - `descriptor`: Static definition of the supported signatures.
80 /// - `source`: Compile-time metadata identifying the declaration site.
81 ///
82 /// # Returns
83 ///
84 /// A registration containing the supplied identifier, descriptor, and
85 /// source metadata.
86 #[must_use]
87 #[inline]
88 pub const fn new(id: ValidatorId, descriptor: &'static ValidatorDescriptor, source: RegistrationSource) -> Self {
89 Self { id, descriptor, source }
90 }
91
92 /// Returns the stable identifier.
93 ///
94 /// # Returns
95 ///
96 /// The identifier claimed by this registration.
97 #[must_use]
98 #[inline]
99 pub const fn id(&self) -> ValidatorId {
100 self.id
101 }
102
103 /// Returns the immutable descriptor.
104 ///
105 /// # Returns
106 ///
107 /// The static descriptor defining this registration's supported
108 /// signatures.
109 #[must_use]
110 #[inline]
111 pub const fn descriptor(&self) -> &'static ValidatorDescriptor {
112 self.descriptor
113 }
114
115 /// Returns the source location used for duplicate diagnostics.
116 ///
117 /// # Returns
118 ///
119 /// The compile-time source metadata associated with this registration.
120 #[must_use]
121 #[inline]
122 pub const fn source(&self) -> RegistrationSource {
123 self.source
124 }
125}
126
127// Supports callers that still hold static references to registrations.
128impl From<&ValidatorRegistration> for ValidatorRegistration {
129 /// Copies a registration borrowed from a static-compatible reference.
130 ///
131 /// # Parameters
132 ///
133 /// - `value`: Registration whose copy is returned.
134 #[inline]
135 fn from(value: &ValidatorRegistration) -> Self {
136 *value
137 }
138}