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
use std::any::Any;
use crate::{
objects::{
Curve, Cycle, Face, GlobalCurve, GlobalEdge, GlobalVertex, HalfEdge,
Objects, Shell, Sketch, Solid, Surface, SurfaceVertex,
},
storage::{Handle, ObjectId},
validate::{Validate, ValidationError},
};
macro_rules! object {
($($ty:ident, $name:expr, $store:ident;)*) => {
#[derive(Clone, Debug)]
pub enum Object<F: Form> {
$(
#[doc = concat!("A ", $name)]
$ty(F::Form<$ty>),
)*
}
impl<F: Form> Object<F> {
pub fn as_inner<T>(&self) -> Option<&F::Form<T>>
where
Self: 'static,
F::Form<T>: Any,
{
match self {
$(
Self::$ty(object) =>
(object as &dyn Any).downcast_ref(),
)*
}
}
}
impl Object<BehindHandle> {
pub fn id(&self) -> ObjectId {
match self {
$(
Self::$ty(handle) => handle.id(),
)*
}
}
}
impl Object<WithHandle> {
pub fn insert(self, objects: &mut Objects) -> Object<BehindHandle> {
match self {
$(
Self::$ty((handle, object)) => {
objects.$store.insert(handle.clone(), object);
handle.into()
}
)*
}
}
pub fn validate(&self, errors: &mut Vec<ValidationError>) {
match self {
$(
Self::$ty((_, object)) => object.validate(errors),
)*
}
}
}
impl From<Object<WithHandle>> for Object<BehindHandle> {
fn from(object: Object<WithHandle>) -> Self {
match object {
$(
Object::$ty((handle, _)) => Self::$ty(handle),
)*
}
}
}
$(
impl From<$ty> for Object<Bare> {
fn from(object: $ty) -> Self {
Self::$ty(object)
}
}
impl From<Handle<$ty>> for Object<BehindHandle> {
fn from(object: Handle<$ty>) -> Self {
Self::$ty(object)
}
}
impl From<(Handle<$ty>, $ty)> for Object<WithHandle> {
fn from((handle, object): (Handle<$ty>, $ty)) -> Self {
Self::$ty((handle, object))
}
}
)*
};
}
object!(
Curve, "curve", curves;
Cycle, "cycle", cycles;
Face, "face", faces;
GlobalCurve, "global curve", global_curves;
GlobalEdge, "global edge", global_edges;
GlobalVertex, "global vertex", global_vertices;
HalfEdge, "half-edge", half_edges;
Shell, "shell", shells;
Sketch, "sketch", sketches;
Solid, "solid", solids;
Surface, "surface", surfaces;
SurfaceVertex, "surface vertex", surface_vertices;
);
pub trait Form {
type Form<T>;
}
#[derive(Clone, Debug)]
pub struct Bare;
impl Form for Bare {
type Form<T> = T;
}
#[derive(Clone, Debug)]
pub struct BehindHandle;
impl Form for BehindHandle {
type Form<T> = Handle<T>;
}
#[derive(Clone, Debug)]
pub struct WithHandle;
impl Form for WithHandle {
type Form<T> = (Handle<T>, T);
}