use std::fmt;
use std::fmt::Display;
use std::marker::PhantomData;
use serde::de;
use serde::de::Visitor;
pub(super) struct BoundedStringVisitor<F, E> {
pub(super) validate: F,
pub(super) _error: PhantomData<fn() -> E>,
}
impl<'de, F, E> Visitor<'de> for BoundedStringVisitor<F, E>
where
F: FnOnce(&str) -> Result<(), E>,
E: Display,
{
type Value = String;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a string")
}
fn visit_borrowed_str<DE>(self, value: &'de str) -> Result<Self::Value, DE>
where
DE: de::Error,
{
(self.validate)(value).map_err(DE::custom)?;
Ok(value.to_owned())
}
fn visit_str<DE>(self, value: &str) -> Result<Self::Value, DE>
where
DE: de::Error,
{
(self.validate)(value).map_err(DE::custom)?;
Ok(value.to_owned())
}
fn visit_string<DE>(self, value: String) -> Result<Self::Value, DE>
where
DE: de::Error,
{
(self.validate)(&value).map_err(DE::custom)?;
Ok(value)
}
}