use std::cell::RefCell;
use std::rc::Rc;
use serde::de::DeserializeSeed;
use serde::de::Deserializer;
use super::internal::StrictStringMapValueVisitor;
use super::strict_string_map::StrictStringMap;
use crate::MetadataError;
pub(crate) struct StrictStringMapValueSeed<S> {
max_entries: usize,
max_key_bytes: usize,
error_slot: Option<Rc<RefCell<Option<MetadataError>>>>,
value_seed: S,
}
impl<S> StrictStringMapValueSeed<S> {
#[must_use]
#[inline]
pub(crate) const fn new(max_entries: usize, max_key_bytes: usize, value_seed: S) -> Self {
Self {
max_entries,
max_key_bytes,
error_slot: None,
value_seed,
}
}
#[must_use]
#[cfg(feature = "json")]
#[inline]
pub(crate) fn with_error_slot(mut self, slot: Rc<RefCell<Option<MetadataError>>>) -> Self {
self.error_slot = Some(slot);
self
}
}
impl<'de, S> DeserializeSeed<'de> for StrictStringMapValueSeed<S>
where
S: DeserializeSeed<'de> + Clone,
{
type Value = StrictStringMap<S::Value>;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_map(StrictStringMapValueVisitor::new(
self.max_entries,
self.max_key_bytes,
self.error_slot,
self.value_seed,
))
}
}