use std::any::Any;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt::Result;
use super::InputType;
#[derive(Clone, Copy)]
#[non_exhaustive]
pub enum ValidationValue<'a> {
Text(
&'a str,
),
Typed(
&'a dyn Any,
),
Missing,
}
impl<'a> ValidationValue<'a> {
#[inline]
pub fn input_type(self) -> Option<InputType> {
match self {
Self::Text(_) => Some(InputType::Text),
Self::Typed(value) => Some(InputType::Typed(value.type_id())),
Self::Missing => None,
}
}
#[inline]
pub const fn as_text(self) -> Option<&'a str> {
match self {
Self::Text(value) => Some(value),
_ => None,
}
}
#[must_use]
#[inline]
pub const fn is_missing(self) -> bool {
matches!(self, Self::Missing)
}
#[inline]
pub fn typed<T: 'static>(self) -> Option<&'a T> {
match self {
Self::Typed(value) => value.downcast_ref::<T>(),
_ => None,
}
}
}
impl Debug for ValidationValue<'_> {
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
match self {
Self::Text(_) => formatter.write_str("Text(<redacted>)"),
Self::Typed(_) => formatter.write_str("Typed(<redacted>)"),
Self::Missing => formatter.write_str("Missing"),
}
}
}