use std::{
fmt::Debug,
ops::{Deref, DerefMut},
};
use peace_data::fn_graph::FnGraph;
use peace_resources::states::{States, StatesSerde};
use crate::ItemBoxed;
#[derive(Debug)]
pub struct ItemGraph<E>(FnGraph<ItemBoxed<E>>);
impl<E> Clone for ItemGraph<E> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<E> PartialEq for ItemGraph<E>
where
E: 'static,
{
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<E> Eq for ItemGraph<E> where E: 'static {}
impl<E> ItemGraph<E> {
pub fn into_inner(self) -> FnGraph<ItemBoxed<E>> {
self.0
}
pub fn states_serde<ValueT, TS>(&self, states: &States<TS>) -> StatesSerde<ValueT>
where
ValueT: Clone + Debug + PartialEq + Eq,
E: 'static,
{
StatesSerde::from_iter(self.0.iter_insertion().map(|item| {
let item_id = item.id();
(item_id.clone(), states.get_raw(item_id).cloned())
}))
}
}
impl<E> Deref for ItemGraph<E> {
type Target = FnGraph<ItemBoxed<E>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<E> DerefMut for ItemGraph<E> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<E> From<FnGraph<ItemBoxed<E>>> for ItemGraph<E> {
fn from(graph: FnGraph<ItemBoxed<E>>) -> Self {
Self(graph)
}
}
impl<'graph, ValueT, E> From<&'graph ItemGraph<E>> for StatesSerde<ValueT>
where
ValueT: Clone + Debug + PartialEq + Eq,
E: 'static,
{
fn from(graph: &'graph ItemGraph<E>) -> Self {
StatesSerde::from_iter(graph.iter_insertion().map(|item| (item.id().clone(), None)))
}
}