use std::ops::{Deref, DerefMut};
use serde::{Deserialize, Serialize};
use crate::{Id, Map};
#[cfg_attr(
all(feature = "schemars", not(feature = "test")),
derive(schemars::JsonSchema)
)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
pub struct EntityTooltips<'id>(Map<Id<'id>, String>);
impl<'id> EntityTooltips<'id> {
pub fn new() -> Self {
Self::default()
}
pub fn with_capacity(capacity: usize) -> Self {
Self(Map::with_capacity(capacity))
}
pub fn into_inner(self) -> Map<Id<'id>, String> {
self.0
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn into_static(self) -> EntityTooltips<'static> {
EntityTooltips(
self.0
.into_iter()
.map(|(id, desc)| (id.into_static(), desc))
.collect(),
)
}
pub fn contains_key<IdT>(&self, id: &IdT) -> bool
where
IdT: AsRef<Id<'id>>,
{
self.0.contains_key(id.as_ref())
}
}
impl<'id> Deref for EntityTooltips<'id> {
type Target = Map<Id<'id>, String>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'id> DerefMut for EntityTooltips<'id> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<'id> From<Map<Id<'id>, String>> for EntityTooltips<'id> {
fn from(inner: Map<Id<'id>, String>) -> Self {
Self(inner)
}
}
impl<'id> FromIterator<(Id<'id>, String)> for EntityTooltips<'id> {
fn from_iter<I: IntoIterator<Item = (Id<'id>, String)>>(iter: I) -> Self {
Self(Map::from_iter(iter))
}
}