use crate::{
objects::{Face, Shell},
storage::Handle,
};
pub trait UpdateShell {
#[must_use]
fn add_faces(&self, faces: impl IntoIterator<Item = Handle<Face>>) -> Self;
#[must_use]
fn update_face(
&self,
handle: &Handle<Face>,
update: impl FnOnce(&Handle<Face>) -> Handle<Face>,
) -> Self;
#[must_use]
fn replace_face<const N: usize>(
&self,
handle: &Handle<Face>,
replace: impl FnOnce(&Handle<Face>) -> [Handle<Face>; N],
) -> Self;
#[must_use]
fn remove_face(&self, handle: &Handle<Face>) -> Self;
}
impl UpdateShell for Shell {
fn add_faces(&self, faces: impl IntoIterator<Item = Handle<Face>>) -> Self {
let faces = self.faces().iter().cloned().chain(faces);
Shell::new(faces)
}
fn update_face(
&self,
handle: &Handle<Face>,
update: impl FnOnce(&Handle<Face>) -> Handle<Face>,
) -> Self {
let faces = self
.faces()
.replace(handle, [update(handle)])
.expect("Face not found");
Shell::new(faces)
}
fn replace_face<const N: usize>(
&self,
handle: &Handle<Face>,
replace: impl FnOnce(&Handle<Face>) -> [Handle<Face>; N],
) -> Self {
let faces = self
.faces()
.replace(handle, replace(handle))
.expect("Face not found");
Shell::new(faces)
}
fn remove_face(&self, handle: &Handle<Face>) -> Self {
let faces = self
.faces()
.iter()
.filter(|face| face.id() != handle.id())
.cloned();
Shell::new(faces)
}
}