use std::any::Any;
use std::collections::BTreeMap;
use std::fmt;
use crate::chunk_map::ChunkMap;
use crate::model::{ModelError, leaf_paths};
use crate::path::{Edit, EditOp, Path, Segment, Settable};
use crate::table::{MapEntries, MapIn, ModelTable};
use crate::{Model, Shape};
#[derive(Clone)]
pub struct Map<V> {
entries: ChunkMap<V>,
}
impl<V> Default for Map<V> {
fn default() -> Self {
Self {
entries: ChunkMap::default(),
}
}
}
impl<V: fmt::Debug> fmt::Debug for Map<V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.entries.fmt(f)
}
}
impl<V: PartialEq> PartialEq for Map<V> {
fn eq(&self, other: &Self) -> bool {
self.entries == other.entries
}
}
impl<V: Eq> Eq for Map<V> {}
impl<V> Map<V> {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn len(&self) -> usize {
self.entries.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
#[must_use]
pub fn get(&self, key: &str) -> Option<&V> {
self.entries.get(key)
}
#[must_use]
pub fn contains_key(&self, key: &str) -> bool {
self.entries.contains_key(key)
}
pub fn iter(&self) -> impl Iterator<Item = (&String, &V)> {
self.entries.iter()
}
pub fn keys(&self) -> impl Iterator<Item = &String> {
self.entries.iter().map(|(key, _)| key)
}
pub fn values(&self) -> impl ExactSizeIterator<Item = &V> {
self.entries.values()
}
pub fn get_mut(&mut self, key: &str) -> Option<&mut V> {
self.entries.get_mut(key)
}
pub fn insert(&mut self, key: impl Into<String>, value: V) -> Option<V> {
self.entries.insert(key.into(), value)
}
pub fn remove(&mut self, key: &str) -> Option<V> {
self.entries.remove(key)
}
pub fn clear(&mut self) {
self.entries.clear();
}
}
impl<V> From<BTreeMap<String, V>> for Map<V> {
fn from(entries: BTreeMap<String, V>) -> Self {
Self {
entries: ChunkMap::from(entries),
}
}
}
impl<V: Clone> From<&Map<V>> for BTreeMap<String, V> {
fn from(map: &Map<V>) -> Self {
map.iter()
.map(|(key, value)| (key.clone(), value.clone()))
.collect()
}
}
impl<K: Into<String>, V> FromIterator<(K, V)> for Map<V> {
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
let sorted: BTreeMap<String, V> = iter
.into_iter()
.map(|(key, value)| (key.into(), value))
.collect();
Self::from(sorted)
}
}
impl<K: Into<String>, V> Extend<(K, V)> for Map<V> {
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
for (key, value) in iter {
self.insert(key, value);
}
}
}
impl<T: 'static> MapEntries for Map<T> {
fn count(&self) -> usize {
self.entries.count()
}
fn each(&self, visit: &mut dyn FnMut(&str, &dyn Any)) {
self.entries.each(visit);
}
fn value(&self, key: &str) -> Option<&dyn Any> {
self.entries.value(key)
}
fn value_mut(&mut self, key: &str) -> Option<&mut dyn Any> {
self.entries.value_mut(key)
}
fn remove_key(&mut self, key: &str) {
self.entries.remove_key(key);
}
fn insert_filled(
&mut self,
key: String,
fill: &mut dyn FnMut(&mut dyn Any) -> Result<(), ModelError>,
) -> Result<(), ModelError> {
self.entries.insert_filled(key, fill)
}
fn insert_moved(&mut self, key: String, staged: &mut dyn Any) -> Result<(), ModelError> {
self.entries.insert_moved(key, staged)
}
fn differing_keys<'a>(&'a self, before: &'a dyn Any) -> Option<Vec<&'a str>> {
let before = before.downcast_ref::<Self>()?;
Some(self.entries.differing_keys(&before.entries))
}
}
impl<T: Model> Settable for Map<T> {}
impl<T: Model> Model for Map<T> {
const SHAPE: Shape = Shape::Map(&T::SHAPE);
fn value_schema() -> serde_json::Value {
serde_json::json!({"type": "object"})
}
fn field_type(types: &mut crate::TypeSet) -> crate::FieldType {
crate::FieldType::Map(Box::new(T::field_type(types)))
}
const CHECKS: bool = T::CHECKS;
const OPS: bool = T::OPS;
const TABLE: &'static ModelTable = &MapIn::<T, Self>::TABLE;
leaf_paths!();
crate::__table_functions!();
fn missing() -> Result<Self, ModelError> {
Ok(Self::new())
}
}
impl<R, T: Model> Path<R, Map<T>> {
#[must_use]
pub fn key(&self, key: impl Into<String>) -> T::Paths<R> {
T::paths_at(self.child(Segment::Key(key.into())))
}
#[must_use]
pub fn remove(&self, key: impl Into<String>) -> Edit<R> {
self.edit(EditOp::RemoveKey { key: key.into() })
}
}