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

/// A partial [`Curve`]
///
/// See [`crate::partial`] for more information.
#[derive(Clone, Debug, Default)]
pub struct PartialCurve {
    /// The path that defines the [`Curve`]
    pub path: Option<SurfacePath>,

    /// The surface that the [`Curve`] is defined in
    pub surface: Option<Handle<Surface>>,

    /// The global form of the [`Curve`]
    pub global_form: MaybePartial<GlobalCurve>,
}

impl PartialCurve {
    /// Build a full [`Curve`] from the partial curve
    pub fn build(self, objects: &Objects) -> Result<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 = self.global_form.into_full(objects)?;

        Ok(Curve::new(surface, path, global_form))
    }
}

impl MergeWith for PartialCurve {
    fn merge_with(self, other: impl Into<Self>) -> Self {
        let other = other.into();

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

impl Replace<Surface> for PartialCurve {
    fn replace(&mut self, surface: Handle<Surface>) -> &mut Self {
        self.surface = Some(surface);
        self
    }
}

impl From<&Curve> for PartialCurve {
    fn from(curve: &Curve) -> Self {
        Self {
            path: Some(curve.path()),
            surface: Some(curve.surface().clone()),
            global_form: 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 {
    /// Build a full [`GlobalCurve`] from the partial global curve
    pub fn build(self, _: &Objects) -> Result<GlobalCurve, ValidationError> {
        Ok(GlobalCurve)
    }
}

impl MergeWith for PartialGlobalCurve {
    fn merge_with(self, _: impl Into<Self>) -> Self {
        Self
    }
}

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