1use super::{identified_seed, identified_setters, missing, top_level_seed, top_level_setters};
4use crate::client::identity::{DEFAULT_VERSION, build_top_level_identity};
5use crate::client::{
6 Attachment, Collection, CombinatorialDerivation, ComponentDefinition, Experiment,
7 ExperimentalData, IdentifiedData, Implementation, Model, ModuleDefinition, Sequence,
8 TopLevelData,
9};
10use crate::error::BuildError;
11use crate::identity::{DisplayId, Namespace};
12use crate::{Iri, Resource, Sbol2Class, Term};
13use sbol_core::error::BuildError as LexError;
14
15fn seed(namespace: &Namespace, display_id: &DisplayId) -> (Resource, IdentifiedData, TopLevelData) {
16 let (identity, persistent) = build_top_level_identity(namespace, display_id, DEFAULT_VERSION);
17 (
18 identity,
19 identified_seed(display_id, persistent),
20 top_level_seed(),
21 )
22}
23
24macro_rules! top_level_builder {
25 (
26 $class:ident, $builder:ident, $sbol_class:ident,
27 fields { $( $field:ident : $fty:ty = $default:expr ),* $(,)? }
28 setters { $( $setter:item )* }
29 build { $( $req:ident )* }
30 ) => {
31 #[derive(Clone, Debug)]
33 pub struct $builder {
34 identity: Resource,
35 identified: IdentifiedData,
36 top_level: TopLevelData,
37 $( $field: $fty, )*
38 }
39
40 impl $builder {
41 pub(crate) fn seed(namespace: Namespace, display_id: DisplayId) -> Self {
42 let (identity, identified, top_level) = seed(&namespace, &display_id);
43 Self { identity, identified, top_level, $( $field: $default, )* }
44 }
45
46 identified_setters!();
47 top_level_setters!();
48 $( $setter )*
49
50 pub fn build(self) -> Result<$class, BuildError> {
51 $(
52 if is_unset(&self.$req) {
53 return Err(missing(&self.identity, Sbol2Class::$sbol_class, stringify!($req)));
54 }
55 )*
56 Ok($class {
57 identity: self.identity,
58 identified: self.identified,
59 top_level: self.top_level,
60 $( $field: self.$field, )*
61 })
62 }
63 }
64
65 impl $class {
66 pub fn builder(
67 namespace: impl TryInto<Namespace, Error = LexError>,
68 display_id: impl TryInto<DisplayId, Error = LexError>,
69 ) -> Result<$builder, BuildError> {
70 Ok($builder::seed(namespace.try_into()?, display_id.try_into()?))
71 }
72 }
73 };
74}
75
76trait Unset {
78 fn is_unset(&self) -> bool;
79}
80impl<T> Unset for Option<T> {
81 fn is_unset(&self) -> bool {
82 self.is_none()
83 }
84}
85impl<T> Unset for Vec<T> {
86 fn is_unset(&self) -> bool {
87 self.is_empty()
88 }
89}
90fn is_unset(value: &impl Unset) -> bool {
91 value.is_unset()
92}
93
94top_level_builder! {
95 Sequence, SequenceBuilder, Sequence,
96 fields { elements: Option<String> = None, encoding: Option<Iri> = None }
97 setters {
98 pub fn elements(mut self, value: impl Into<String>) -> Self {
99 self.elements = Some(value.into());
100 self
101 }
102 pub fn encoding(mut self, value: Iri) -> Self {
103 self.encoding = Some(value);
104 self
105 }
106 }
107 build {}
108}
109
110top_level_builder! {
111 ComponentDefinition, ComponentDefinitionBuilder, ComponentDefinition,
112 fields {
113 types: Vec<Iri> = Vec::new(),
114 roles: Vec<Iri> = Vec::new(),
115 sequences: Vec<Resource> = Vec::new(),
116 components: Vec<Resource> = Vec::new(),
117 sequence_annotations: Vec<Resource> = Vec::new(),
118 sequence_constraints: Vec<Resource> = Vec::new(),
119 }
120 setters {
121 pub fn types(mut self, values: impl IntoIterator<Item = Iri>) -> Self {
122 self.types = values.into_iter().collect();
123 self
124 }
125 pub fn add_type(mut self, value: Iri) -> Self { self.types.push(value); self }
126 pub fn roles(mut self, values: impl IntoIterator<Item = Iri>) -> Self {
127 self.roles = values.into_iter().collect();
128 self
129 }
130 pub fn add_role(mut self, value: Iri) -> Self { self.roles.push(value); self }
131 pub fn sequences(mut self, values: impl IntoIterator<Item = Resource>) -> Self {
132 self.sequences = values.into_iter().collect();
133 self
134 }
135 pub fn add_sequence(mut self, value: Resource) -> Self { self.sequences.push(value); self }
136 pub fn components(mut self, values: impl IntoIterator<Item = Resource>) -> Self {
137 self.components = values.into_iter().collect();
138 self
139 }
140 pub fn add_component(mut self, value: Resource) -> Self { self.components.push(value); self }
141 pub fn sequence_annotations(mut self, values: impl IntoIterator<Item = Resource>) -> Self {
142 self.sequence_annotations = values.into_iter().collect();
143 self
144 }
145 pub fn add_sequence_annotation(mut self, value: Resource) -> Self {
146 self.sequence_annotations.push(value);
147 self
148 }
149 pub fn sequence_constraints(mut self, values: impl IntoIterator<Item = Resource>) -> Self {
150 self.sequence_constraints = values.into_iter().collect();
151 self
152 }
153 pub fn add_sequence_constraint(mut self, value: Resource) -> Self {
154 self.sequence_constraints.push(value);
155 self
156 }
157 }
158 build { types }
159}
160
161top_level_builder! {
162 ModuleDefinition, ModuleDefinitionBuilder, ModuleDefinition,
163 fields {
164 roles: Vec<Iri> = Vec::new(),
165 modules: Vec<Resource> = Vec::new(),
166 functional_components: Vec<Resource> = Vec::new(),
167 interactions: Vec<Resource> = Vec::new(),
168 models: Vec<Resource> = Vec::new(),
169 }
170 setters {
171 pub fn roles(mut self, values: impl IntoIterator<Item = Iri>) -> Self {
172 self.roles = values.into_iter().collect();
173 self
174 }
175 pub fn add_role(mut self, value: Iri) -> Self { self.roles.push(value); self }
176 pub fn modules(mut self, values: impl IntoIterator<Item = Resource>) -> Self {
177 self.modules = values.into_iter().collect();
178 self
179 }
180 pub fn add_module(mut self, value: Resource) -> Self { self.modules.push(value); self }
181 pub fn functional_components(mut self, values: impl IntoIterator<Item = Resource>) -> Self {
182 self.functional_components = values.into_iter().collect();
183 self
184 }
185 pub fn add_functional_component(mut self, value: Resource) -> Self {
186 self.functional_components.push(value);
187 self
188 }
189 pub fn interactions(mut self, values: impl IntoIterator<Item = Resource>) -> Self {
190 self.interactions = values.into_iter().collect();
191 self
192 }
193 pub fn add_interaction(mut self, value: Resource) -> Self { self.interactions.push(value); self }
194 pub fn models(mut self, values: impl IntoIterator<Item = Resource>) -> Self {
195 self.models = values.into_iter().collect();
196 self
197 }
198 pub fn add_model(mut self, value: Resource) -> Self { self.models.push(value); self }
199 }
200 build {}
201}
202
203top_level_builder! {
204 Model, ModelBuilder, Model,
205 fields {
206 source: Option<Resource> = None,
207 language: Option<Iri> = None,
208 framework: Option<Iri> = None,
209 }
210 setters {
211 pub fn source(mut self, value: Resource) -> Self { self.source = Some(value); self }
212 pub fn language(mut self, value: Iri) -> Self { self.language = Some(value); self }
213 pub fn framework(mut self, value: Iri) -> Self { self.framework = Some(value); self }
214 }
215 build { source language framework }
216}
217
218top_level_builder! {
219 Collection, CollectionBuilder, Collection,
220 fields { members: Vec<Resource> = Vec::new() }
221 setters {
222 pub fn members(mut self, values: impl IntoIterator<Item = Resource>) -> Self {
223 self.members = values.into_iter().collect();
224 self
225 }
226 pub fn add_member(mut self, value: Resource) -> Self { self.members.push(value); self }
227 }
228 build {}
229}
230
231top_level_builder! {
232 CombinatorialDerivation, CombinatorialDerivationBuilder, CombinatorialDerivation,
233 fields {
234 template: Option<Resource> = None,
235 variable_components: Vec<Resource> = Vec::new(),
236 strategy: Option<Iri> = None,
237 }
238 setters {
239 pub fn template(mut self, value: Resource) -> Self { self.template = Some(value); self }
240 pub fn variable_components(mut self, values: impl IntoIterator<Item = Resource>) -> Self {
241 self.variable_components = values.into_iter().collect();
242 self
243 }
244 pub fn add_variable_component(mut self, value: Resource) -> Self {
245 self.variable_components.push(value);
246 self
247 }
248 pub fn strategy(mut self, value: Iri) -> Self { self.strategy = Some(value); self }
249 }
250 build { template }
251}
252
253top_level_builder! {
254 Implementation, ImplementationBuilder, Implementation,
255 fields { built: Option<Resource> = None }
256 setters {
257 pub fn built(mut self, value: Resource) -> Self { self.built = Some(value); self }
258 }
259 build {}
260}
261
262top_level_builder! {
263 Attachment, AttachmentBuilder, Attachment,
264 fields {
265 source: Option<Resource> = None,
266 format: Option<Iri> = None,
267 size: Option<i64> = None,
268 hash: Option<String> = None,
269 }
270 setters {
271 pub fn source(mut self, value: Resource) -> Self { self.source = Some(value); self }
272 pub fn format(mut self, value: Iri) -> Self { self.format = Some(value); self }
273 pub fn size(mut self, value: i64) -> Self { self.size = Some(value); self }
274 pub fn hash(mut self, value: impl Into<String>) -> Self { self.hash = Some(value.into()); self }
275 }
276 build { source }
277}
278
279top_level_builder! {
280 ExperimentalData, ExperimentalDataBuilder, ExperimentalData,
281 fields {}
282 setters {}
283 build {}
284}
285
286top_level_builder! {
287 Experiment, ExperimentBuilder, Experiment,
288 fields { experimental_data: Vec<Resource> = Vec::new() }
289 setters {
290 pub fn experimental_data(mut self, values: impl IntoIterator<Item = Resource>) -> Self {
291 self.experimental_data = values.into_iter().collect();
292 self
293 }
294 pub fn add_experimental_data(mut self, value: Resource) -> Self {
295 self.experimental_data.push(value);
296 self
297 }
298 }
299 build {}
300}
301
302impl Sequence {
303 pub fn new(
304 namespace: impl TryInto<Namespace, Error = LexError>,
305 display_id: impl TryInto<DisplayId, Error = LexError>,
306 ) -> Result<Self, BuildError> {
307 Self::builder(namespace, display_id)?.build()
308 }
309}
310
311impl ComponentDefinition {
312 pub fn new(
313 namespace: impl TryInto<Namespace, Error = LexError>,
314 display_id: impl TryInto<DisplayId, Error = LexError>,
315 types: impl IntoIterator<Item = Iri>,
316 ) -> Result<Self, BuildError> {
317 Self::builder(namespace, display_id)?.types(types).build()
318 }
319}
320
321impl ModuleDefinition {
322 pub fn new(
323 namespace: impl TryInto<Namespace, Error = LexError>,
324 display_id: impl TryInto<DisplayId, Error = LexError>,
325 ) -> Result<Self, BuildError> {
326 Self::builder(namespace, display_id)?.build()
327 }
328}
329
330impl Model {
331 pub fn new(
332 namespace: impl TryInto<Namespace, Error = LexError>,
333 display_id: impl TryInto<DisplayId, Error = LexError>,
334 source: Resource,
335 language: Iri,
336 framework: Iri,
337 ) -> Result<Self, BuildError> {
338 Self::builder(namespace, display_id)?
339 .source(source)
340 .language(language)
341 .framework(framework)
342 .build()
343 }
344}
345
346impl Collection {
347 pub fn new(
348 namespace: impl TryInto<Namespace, Error = LexError>,
349 display_id: impl TryInto<DisplayId, Error = LexError>,
350 ) -> Result<Self, BuildError> {
351 Self::builder(namespace, display_id)?.build()
352 }
353}
354
355impl CombinatorialDerivation {
356 pub fn new(
357 namespace: impl TryInto<Namespace, Error = LexError>,
358 display_id: impl TryInto<DisplayId, Error = LexError>,
359 template: Resource,
360 ) -> Result<Self, BuildError> {
361 Self::builder(namespace, display_id)?
362 .template(template)
363 .build()
364 }
365}
366
367impl Implementation {
368 pub fn new(
369 namespace: impl TryInto<Namespace, Error = LexError>,
370 display_id: impl TryInto<DisplayId, Error = LexError>,
371 ) -> Result<Self, BuildError> {
372 Self::builder(namespace, display_id)?.build()
373 }
374}
375
376impl Attachment {
377 pub fn new(
378 namespace: impl TryInto<Namespace, Error = LexError>,
379 display_id: impl TryInto<DisplayId, Error = LexError>,
380 source: Resource,
381 ) -> Result<Self, BuildError> {
382 Self::builder(namespace, display_id)?.source(source).build()
383 }
384}
385
386impl ExperimentalData {
387 pub fn new(
388 namespace: impl TryInto<Namespace, Error = LexError>,
389 display_id: impl TryInto<DisplayId, Error = LexError>,
390 ) -> Result<Self, BuildError> {
391 Self::builder(namespace, display_id)?.build()
392 }
393}
394
395impl Experiment {
396 pub fn new(
397 namespace: impl TryInto<Namespace, Error = LexError>,
398 display_id: impl TryInto<DisplayId, Error = LexError>,
399 ) -> Result<Self, BuildError> {
400 Self::builder(namespace, display_id)?.build()
401 }
402}