use super::super::BoundValidationContext;
use super::super::DependencySpec;
use super::super::ExecutionError;
use super::super::ExecutionErrorKind;
use super::super::InputType;
use super::super::PreparedOutcome;
use super::super::PreparedValidator;
use super::super::ValidationValue;
pub(in crate::binding) struct TextContextFnAdapter<F> {
call: F,
dependencies: &'static [DependencySpec],
}
impl<F> TextContextFnAdapter<F> {
#[must_use]
#[inline]
pub(in crate::binding) const fn new(call: F, dependencies: &'static [DependencySpec]) -> Self {
Self { call, dependencies }
}
}
impl<F> PreparedValidator for TextContextFnAdapter<F>
where
F: for<'a> Fn(&str, &BoundValidationContext<'a>) -> Result<PreparedOutcome, ExecutionError> + Send + Sync,
{
#[inline]
fn input_type(&self) -> InputType {
InputType::Text
}
#[inline]
fn dependency_specs(&self) -> &'static [DependencySpec] {
self.dependencies
}
fn validate(
&self,
value: ValidationValue<'_>,
context: &BoundValidationContext<'_>,
) -> Result<PreparedOutcome, ExecutionError> {
let text = value
.as_text()
.ok_or_else(|| ExecutionError::new(ExecutionErrorKind::InputTypeMismatch))?;
(self.call)(text, context)
}
}