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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::{
    objects::{Shell, Solid},
    storage::Handle,
};

/// Update a [`Solid`]
pub trait UpdateSolid {
    /// Add a shell to the solid
    #[must_use]
    fn add_shells(
        &self,
        shells: impl IntoIterator<Item = Handle<Shell>>,
    ) -> Self;

    /// Update a shell of the solid
    ///
    /// # Panics
    ///
    /// Panics, if the object can't be found.
    ///
    /// Panics, if the update results in a duplicate object.
    #[must_use]
    fn update_shell(
        &self,
        handle: &Handle<Shell>,
        update: impl FnOnce(&Handle<Shell>) -> Handle<Shell>,
    ) -> Self;

    /// Replace a shell of the solid
    ///
    /// This is a more general version of [`UpdateSolid::update_shell`] which
    /// can replace a single edge with multiple others.
    ///
    /// # Panics
    ///
    /// Panics, if the object can't be found.
    ///
    /// Panics, if the update results in a duplicate object.
    #[must_use]
    fn replace_shell<const N: usize>(
        &self,
        handle: &Handle<Shell>,
        replace: impl FnOnce(&Handle<Shell>) -> [Handle<Shell>; N],
    ) -> Self;
}

impl UpdateSolid for Solid {
    fn add_shells(
        &self,
        shells: impl IntoIterator<Item = Handle<Shell>>,
    ) -> Self {
        let shells = self.shells().iter().cloned().chain(shells);
        Solid::new(shells)
    }

    fn update_shell(
        &self,
        handle: &Handle<Shell>,
        update: impl FnOnce(&Handle<Shell>) -> Handle<Shell>,
    ) -> Self {
        let shells = self
            .shells()
            .replace(handle, [update(handle)])
            .expect("Shell not found");
        Solid::new(shells)
    }

    fn replace_shell<const N: usize>(
        &self,
        handle: &Handle<Shell>,
        replace: impl FnOnce(&Handle<Shell>) -> [Handle<Shell>; N],
    ) -> Self {
        let shells = self
            .shells()
            .replace(handle, replace(handle))
            .expect("Shell not found");
        Solid::new(shells)
    }
}