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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use fj_math::Point;

use crate::{
    geometry::{path::SurfacePath, surface::SurfaceGeometry},
    get::Get,
    insert::Insert,
    objects::{
        Curve, GlobalCurve, GlobalEdge, GlobalVertex, HalfEdge, Objects,
        Surface, SurfaceVertex, Vertex,
    },
    storage::Handle,
    validate::{Validate, ValidationError},
};

use super::{HasPartial, MergeWith, Partial, Replace};

/// Can be used everywhere either a partial or full objects are accepted
///
/// Some convenience methods are available for specific instances of
/// `MaybePartial` (like, `MaybePartial<Curve>`, or `MaybePartial<Vertex>`).
///
/// # Implementation Note
///
/// The set of available convenience methods is far from complete. Please feel
/// free to just add more, if you need them.
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub enum MaybePartial<T: HasPartial> {
    /// A full object
    Full(Handle<T>),

    /// A partial object
    Partial(T::Partial),
}

impl<T: HasPartial> MaybePartial<T> {
    /// Indicate whether this is a full object
    pub fn is_full(&self) -> bool {
        if let Self::Full(_) = self {
            return true;
        }

        false
    }

    /// Indicate whether this is a partial object
    pub fn is_partial(&self) -> bool {
        if let Self::Partial(_) = self {
            return true;
        }

        false
    }

    /// If this is a partial object, update it
    ///
    /// This is useful whenever a partial object can infer something about its
    /// parts from other parts, and wants to update what was inferred, in case
    /// it *can* be updated.
    pub fn update_partial(
        self,
        f: impl FnOnce(T::Partial) -> T::Partial,
    ) -> Self {
        match self {
            Self::Partial(partial) => Self::Partial(f(partial)),
            _ => self,
        }
    }

    /// Return or build a full object
    ///
    /// If this already is a full object, it is returned. If this is a partial
    /// object, the full object is built from it, using [`Partial::build`].
    pub fn into_full(
        self,
        objects: &Objects,
    ) -> Result<Handle<T>, ValidationError>
    where
        T: Insert,
        ValidationError: From<<T as Validate>::Error>,
    {
        match self {
            Self::Partial(partial) => {
                Ok(partial.build(objects)?.insert(objects)?)
            }
            Self::Full(full) => Ok(full),
        }
    }

    /// Return or convert a partial object
    ///
    /// If this already is a partial object, is is returned. If this is a full
    /// object, it is converted into a partial object using
    /// [`HasPartial::to_partial`].
    pub fn into_partial(self) -> T::Partial {
        match self {
            Self::Partial(partial) => partial,
            Self::Full(full) => full.to_partial(),
        }
    }
}

impl<T> Default for MaybePartial<T>
where
    T: HasPartial,
    T::Partial: Default,
{
    fn default() -> Self {
        Self::Partial(T::Partial::default())
    }
}

impl<T> MergeWith for MaybePartial<T>
where
    T: HasPartial,
    T::Partial: MergeWith,
{
    fn merge_with(self, other: impl Into<Self>) -> Self {
        match (self, other.into()) {
            (Self::Full(a), Self::Full(b)) => Self::Full(a.merge_with(b)),
            (Self::Full(full), Self::Partial(_))
            | (Self::Partial(_), Self::Full(full)) => Self::Full(full),
            (Self::Partial(a), Self::Partial(b)) => {
                Self::Partial(a.merge_with(b))
            }
        }
    }
}

impl<T, R> Replace<R> for MaybePartial<T>
where
    T: HasPartial + Get<R>,
    T::Partial: Replace<R>,
{
    fn replace(&mut self, object: Handle<R>) -> &mut Self {
        match self {
            Self::Full(full) => {
                if full.get().id() != object.id() {
                    let mut partial = full.to_partial();
                    partial.replace(object);
                    *self = Self::Partial(partial);
                }
            }
            Self::Partial(partial) => {
                partial.replace(object);
            }
        }

        self
    }
}

impl<T> From<Handle<T>> for MaybePartial<T>
where
    T: HasPartial,
{
    fn from(full: Handle<T>) -> Self {
        Self::Full(full)
    }
}

// Unfortunately, we can't add a blanket implementation from `T::Partial` for
// `MaybePartial<T>`, as that would conflict.

impl MaybePartial<Curve> {
    /// Access the path
    pub fn path(&self) -> Option<SurfacePath> {
        match self {
            MaybePartial::Full(full) => Some(full.path()),
            MaybePartial::Partial(partial) => partial.path,
        }
    }

    /// Access the surface
    pub fn surface(&self) -> Option<Handle<Surface>> {
        match self {
            MaybePartial::Full(full) => Some(full.surface().clone()),
            MaybePartial::Partial(partial) => partial.surface.clone(),
        }
    }

    /// Access the global form
    pub fn global_form(&self) -> MaybePartial<GlobalCurve> {
        match self {
            Self::Full(full) => full.global_form().clone().into(),
            Self::Partial(partial) => partial.global_form.clone(),
        }
    }
}

impl MaybePartial<GlobalEdge> {
    /// Access the curve
    pub fn curve(&self) -> MaybePartial<GlobalCurve> {
        match self {
            Self::Full(full) => full.curve().clone().into(),
            Self::Partial(partial) => partial.curve.clone(),
        }
    }

    /// Access the vertices
    pub fn vertices(&self) -> [MaybePartial<GlobalVertex>; 2] {
        match self {
            Self::Full(full) => {
                full.vertices().access_in_normalized_order().map(Into::into)
            }
            Self::Partial(partial) => partial.vertices.clone(),
        }
    }
}

impl MaybePartial<GlobalVertex> {
    /// Access the position
    pub fn position(&self) -> Option<Point<3>> {
        match self {
            Self::Full(full) => Some(full.position()),
            Self::Partial(partial) => partial.position,
        }
    }
}

impl MaybePartial<HalfEdge> {
    /// Access the curve
    pub fn curve(&self) -> MaybePartial<Curve> {
        match self {
            Self::Full(full) => full.curve().clone().into(),
            Self::Partial(partial) => partial.curve.clone(),
        }
    }

    /// Access the front vertex
    pub fn front(&self) -> MaybePartial<Vertex> {
        match self {
            Self::Full(full) => full.front().clone().into(),
            Self::Partial(partial) => {
                let [_, front] = &partial.vertices;
                front.clone()
            }
        }
    }

    /// Access the vertices
    pub fn vertices(&self) -> [MaybePartial<Vertex>; 2] {
        match self {
            Self::Full(full) => full.vertices().clone().map(Into::into),
            Self::Partial(partial) => partial.vertices.clone(),
        }
    }
}

impl MaybePartial<Surface> {
    /// Access the geometry
    pub fn geometry(&self) -> Option<SurfaceGeometry> {
        match self {
            Self::Full(full) => Some(full.geometry()),
            Self::Partial(partial) => partial.geometry,
        }
    }
}

impl MaybePartial<SurfaceVertex> {
    /// Access the position
    pub fn position(&self) -> Option<Point<2>> {
        match self {
            Self::Full(full) => Some(full.position()),
            Self::Partial(partial) => partial.position,
        }
    }

    /// Access the surface
    pub fn surface(&self) -> Option<Handle<Surface>> {
        match self {
            Self::Full(full) => Some(full.surface().clone()),
            Self::Partial(partial) => partial.surface.clone(),
        }
    }

    /// Access the global form
    pub fn global_form(&self) -> MaybePartial<GlobalVertex> {
        match self {
            Self::Full(full) => full.global_form().clone().into(),
            Self::Partial(partial) => partial.global_form.clone(),
        }
    }
}

impl MaybePartial<Vertex> {
    /// Access the surface form
    pub fn surface_form(&self) -> MaybePartial<SurfaceVertex> {
        match self {
            Self::Full(full) => full.surface_form().clone().into(),
            Self::Partial(partial) => partial.surface_form.clone(),
        }
    }
}