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
use crate::{
    objects::{Curve, GlobalCurve, Objects, Surface},
    partial::{util::merge_options, MaybePartial},
    path::SurfacePath,
    storage::Handle,
    validate::ValidationError,
};

/// A partial [`Curve`]
///
/// See [`crate::partial`] for more information.
#[derive(Clone, Debug, Default)]
pub struct PartialCurve {
    path: Option<SurfacePath>,
    surface: Option<Handle<Surface>>,
    global_form: Option<MaybePartial<GlobalCurve>>,
}

impl PartialCurve {
    /// Access the path that defines the [`Curve`]
    pub fn path(&self) -> Option<SurfacePath> {
        self.path
    }

    /// Access the surface that the [`Curve`] is defined in
    pub fn surface(&self) -> Option<Handle<Surface>> {
        self.surface.clone()
    }

    /// Access the global form of the [`Curve`]
    pub fn global_form(&self) -> Option<MaybePartial<GlobalCurve>> {
        self.global_form.clone()
    }

    /// Provide a path for the partial curve
    pub fn with_path(mut self, path: Option<SurfacePath>) -> Self {
        if let Some(path) = path {
            self.path = Some(path);
        }
        self
    }

    /// Provide a surface for the partial curve
    pub fn with_surface(mut self, surface: Option<Handle<Surface>>) -> Self {
        if let Some(surface) = surface {
            self.surface = Some(surface);
        }
        self
    }

    /// Provide a global form for the partial curve
    pub fn with_global_form(
        mut self,
        global_form: Option<impl Into<MaybePartial<GlobalCurve>>>,
    ) -> Self {
        if let Some(global_form) = global_form {
            self.global_form = Some(global_form.into());
        }
        self
    }

    /// Merge this partial object with another
    pub fn merge_with(self, other: Self) -> Self {
        // This is harder than it should be, as `global_form` uses the redundant
        // `Option<MaybePartial<_>>` representation. There's some code relying
        // on that though, so we have to live with it for now.
        let global_form = match (self.global_form, other.global_form) {
            (Some(a), Some(b)) => Some(a.merge_with(b)),
            (Some(global_form), None) | (None, Some(global_form)) => {
                Some(global_form)
            }
            (None, None) => None,
        };

        Self {
            path: merge_options(self.path, other.path),
            surface: merge_options(self.surface, other.surface),
            global_form,
        }
    }

    /// Build a full [`Curve`] from the partial curve
    pub fn build(
        self,
        objects: &Objects,
    ) -> Result<Handle<Curve>, ValidationError> {
        let path = self.path.expect("Can't build `Curve` without path");
        let surface =
            self.surface.expect("Can't build `Curve` without surface");

        let global_form = match self.global_form {
            Some(global_form) => global_form,
            None => objects.global_curves.insert(GlobalCurve)?.into(),
        }
        .into_full(objects)?;

        Ok(objects
            .curves
            .insert(Curve::new(surface, path, global_form))?)
    }
}

impl From<&Curve> for PartialCurve {
    fn from(curve: &Curve) -> Self {
        Self {
            path: Some(curve.path()),
            surface: Some(curve.surface().clone()),
            global_form: Some(curve.global_form().clone().into()),
        }
    }
}

/// A partial [`GlobalCurve`]
///
/// This struct might seem unnecessary. [`GlobalCurve`] literally has nothing in
/// it. Why would we need to represent a part of nothing? However, having this
/// provides some regularity that helps simplify some things within the partial
/// object and builder APIs.
///
/// See [`crate::partial`] for more information.
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct PartialGlobalCurve;

impl PartialGlobalCurve {
    /// Merge this partial object with another
    pub fn merge_with(self, _: Self) -> Self {
        Self
    }

    /// Build a full [`GlobalCurve`] from the partial global curve
    pub fn build(
        self,
        objects: &Objects,
    ) -> Result<Handle<GlobalCurve>, ValidationError> {
        let global_curve = objects.global_curves.insert(GlobalCurve)?;
        Ok(global_curve)
    }
}

impl From<&GlobalCurve> for PartialGlobalCurve {
    fn from(_: &GlobalCurve) -> Self {
        Self
    }
}