1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use crate::{
    objects::{Face, Shell},
    storage::Handle,
};

/// Update a [`Shell`]
pub trait UpdateShell {
    /// Update a face of the shell
    fn update_face(
        &self,
        handle: &Handle<Face>,
        f: impl FnMut(&Handle<Face>) -> Handle<Face>,
    ) -> Shell;

    /// Remove a face from the shell
    fn remove_face(&self, handle: &Handle<Face>) -> Shell;
}

impl UpdateShell for Shell {
    fn update_face(
        &self,
        handle: &Handle<Face>,
        mut f: impl FnMut(&Handle<Face>) -> Handle<Face>,
    ) -> Shell {
        let faces = self.faces().into_iter().map(|face| {
            if face.id() == handle.id() {
                f(face)
            } else {
                face.clone()
            }
        });

        Shell::new(faces)
    }

    fn remove_face(&self, handle: &Handle<Face>) -> Shell {
        let faces = self
            .faces()
            .into_iter()
            .filter(|face| face.id() == handle.id())
            .cloned();

        Shell::new(faces)
    }
}