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,
};
#[derive(Clone, Debug, Default)]
pub struct PartialCurve {
path: Option<SurfacePath>,
surface: Option<Handle<Surface>>,
global_form: Option<MaybePartial<GlobalCurve>>,
}
impl PartialCurve {
pub fn path(&self) -> Option<SurfacePath> {
self.path
}
pub fn surface(&self) -> Option<Handle<Surface>> {
self.surface.clone()
}
pub fn global_form(&self) -> Option<MaybePartial<GlobalCurve>> {
self.global_form.clone()
}
pub fn with_path(mut self, path: Option<SurfacePath>) -> Self {
if let Some(path) = path {
self.path = Some(path);
}
self
}
pub fn with_surface(mut self, surface: Option<Handle<Surface>>) -> Self {
if let Some(surface) = surface {
self.surface = Some(surface);
}
self
}
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
}
pub fn merge_with(self, other: Self) -> Self {
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,
}
}
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()),
}
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct PartialGlobalCurve;
impl PartialGlobalCurve {
pub fn merge_with(self, _: Self) -> Self {
Self
}
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
}
}