use crate::{serde::de::error_utils::make_custom_error, TypeRegistration, TypeRegistry};
use core::{fmt, fmt::Formatter};
use serde::de::{DeserializeSeed, Error, Visitor};
pub struct TypeRegistrationDeserializer<'a> {
registry: &'a TypeRegistry,
}
impl<'a> TypeRegistrationDeserializer<'a> {
pub fn new(registry: &'a TypeRegistry) -> Self {
Self { registry }
}
}
impl<'a, 'de> DeserializeSeed<'de> for TypeRegistrationDeserializer<'a> {
type Value = &'a TypeRegistration;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: serde::Deserializer<'de>,
{
struct TypeRegistrationVisitor<'a>(&'a TypeRegistry);
impl<'de, 'a> Visitor<'de> for TypeRegistrationVisitor<'a> {
type Value = &'a TypeRegistration;
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
formatter.write_str("string containing `type` entry for the reflected value")
}
fn visit_str<E>(self, type_path: &str) -> Result<Self::Value, E>
where
E: Error,
{
if let Some(registration) = self.0.get_with_type_path(type_path) {
return Ok(registration);
}
match self.0.get_with_short_type_path(type_path) {
Some(registration) => Ok(registration),
None => Err({
if self.0.is_ambiguous(type_path) {
make_custom_error(format_args!(
"found short type path `{type_path}`, but this short type path is ambiguous between multiple different types"
))
} else {
make_custom_error(format_args!(
"no registration found for `{type_path}`"
))
}
}),
}
}
}
deserializer.deserialize_str(TypeRegistrationVisitor(self.registry))
}
}