1use serde::{Deserialize, Serialize};
4use std::path::PathBuf;
5
6use crate::builder::{ValueBound, ValueStep};
7use crate::error::BuildError;
8use crate::generator::build_string::{DuplicatePermutationGenerator, FormatGenerator};
9use crate::generator::distribution::NormalGenerator;
10use crate::generator::evaluate::EvalGenerator;
11use crate::generator::incremental::IncrementIdGenerator;
12use crate::generator::primitive::{
13 AlwaysNullGenerator, BoolGenerator, DateGenerator, DateTimeGenerator, IntGenerator,
14 RealGenerator, TimeGenerator,
15};
16use crate::generator::random_children::{CaseWhenGenerator, RandomChildGenerator};
17use crate::generator::random_values::{
18 GetValueAtGenerator, GetValueIndexGenerator, SelectGenerator,
19};
20use crate::generator::{GeneratorBase, Randomizer};
21use crate::generator_type::GeneratorType;
22use crate::value::{
23 DataValue, DataValueMap, SbrdBool, SbrdDate, SbrdDateTime, SbrdInt, SbrdReal, SbrdString,
24 SbrdTime, DATE_DEFAULT_FORMAT, DATE_TIME_DEFAULT_FORMAT, TIME_DEFAULT_FORMAT,
25};
26
27#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
31pub struct ParentGeneratorBuilder {
32 pub(crate) key: String,
33 #[serde(flatten)]
34 pub(crate) builder: GeneratorBuilder,
35}
36
37impl ParentGeneratorBuilder {
38 fn new<S>(key: S, builder: GeneratorBuilder) -> ParentGeneratorBuilder
42 where
43 S: Into<String>,
44 {
45 Self {
46 key: key.into(),
47 builder,
48 }
49 }
50
51 pub fn split_key(self) -> (String, GeneratorBuilder) {
55 let Self { key, builder } = self;
56 (key, builder)
57 }
58}
59
60pub type Weight = u8;
62
63#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
67pub struct ChildGeneratorBuilder {
68 #[serde(rename = "case", skip_serializing_if = "Option::is_none")]
70 pub(crate) condition: Option<String>,
71
72 #[serde(skip_serializing_if = "Option::is_none")]
74 pub(crate) weight: Option<Weight>,
75
76 #[serde(flatten)]
78 pub(crate) builder: GeneratorBuilder,
79}
80
81impl ChildGeneratorBuilder {
82 fn new(builder: GeneratorBuilder) -> ChildGeneratorBuilder {
86 Self {
87 condition: None,
88 weight: None,
89 builder,
90 }
91 }
92
93 pub fn condition<S>(mut self, condition: S) -> Self
95 where
96 S: Into<String>,
97 {
98 self.condition = Some(condition.into());
99 self
100 }
101
102 pub fn weight(mut self, weight: Weight) -> Self {
104 self.weight = Some(weight);
105 self
106 }
107}
108
109fn is_required(use_nullable: &bool) -> bool {
110 use_nullable != &false
111}
112
113fn as_required() -> bool {
114 false
115}
116
117#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
119pub struct GeneratorBuilder {
120 #[serde(rename = "type")]
124 pub(crate) generator_type: GeneratorType,
125
126 #[serde(skip_serializing_if = "is_required", default = "as_required")]
130 pub(crate) nullable: bool,
131
132 #[serde(skip_serializing_if = "Option::is_none")]
139 pub(crate) format: Option<String>,
140
141 #[serde(skip_serializing_if = "Option::is_none")]
148 pub(crate) script: Option<String>,
149
150 #[serde(skip_serializing_if = "Option::is_none")]
154 pub(crate) separator: Option<String>,
155
156 #[serde(skip_serializing_if = "Option::is_none")]
160 pub(crate) range: Option<ValueBound<DataValue>>,
161
162 #[serde(skip_serializing_if = "Option::is_none")]
166 pub(crate) increment: Option<ValueStep<DataValue>>,
167
168 #[serde(skip_serializing_if = "Option::is_none")]
172 pub(crate) children: Option<Vec<ChildGeneratorBuilder>>,
173
174 #[serde(skip_serializing_if = "Option::is_none")]
178 pub(crate) chars: Option<String>,
179
180 #[serde(skip_serializing_if = "Option::is_none")]
184 pub(crate) values: Option<Vec<DataValue>>,
185
186 #[serde(skip_serializing_if = "Option::is_none")]
190 pub(crate) filepath: Option<PathBuf>,
191
192 #[serde(skip_serializing_if = "Option::is_none")]
196 pub(crate) parameters: Option<DataValueMap<String>>,
197}
198
199macro_rules! build_generator {
201 ($builder: expr,$R:ty, $builder_type: ty) => {{
202 let generator: $builder_type = GeneratorBase::<$R>::create($builder)?;
203 Ok(Box::new(generator))
204 }};
205}
206
207impl GeneratorBuilder {
211 pub fn build<R: Randomizer + ?Sized>(self) -> Result<Box<dyn GeneratorBase<R>>, BuildError> {
213 match self.generator_type {
214 GeneratorType::DuplicatePermutation => {
216 build_generator!(self, R, DuplicatePermutationGenerator<R>)
217 }
218 GeneratorType::Format => build_generator!(self, R, FormatGenerator),
219
220 GeneratorType::DistNormal => build_generator!(self, R, NormalGenerator),
222
223 GeneratorType::EvalInt => build_generator!(self, R, EvalGenerator<SbrdInt>),
225 GeneratorType::EvalReal => build_generator!(self, R, EvalGenerator<SbrdReal>),
226 GeneratorType::EvalBool => build_generator!(self, R, EvalGenerator<SbrdBool>),
227 GeneratorType::EvalString => build_generator!(self, R, EvalGenerator<SbrdString>),
228
229 GeneratorType::IncrementId => build_generator!(self, R, IncrementIdGenerator),
231
232 GeneratorType::Int => build_generator!(self, R, IntGenerator),
234 GeneratorType::Real => build_generator!(self, R, RealGenerator),
235 GeneratorType::Bool => build_generator!(self, R, BoolGenerator),
236 GeneratorType::DateTime => build_generator!(self, R, DateTimeGenerator),
237 GeneratorType::Date => build_generator!(self, R, DateGenerator),
238 GeneratorType::Time => build_generator!(self, R, TimeGenerator),
239 GeneratorType::AlwaysNull => build_generator!(self, R, AlwaysNullGenerator),
240
241 GeneratorType::CaseWhen => build_generator!(self, R, CaseWhenGenerator<R>),
243 GeneratorType::RandomChild => build_generator!(self, R, RandomChildGenerator<R>),
244
245 GeneratorType::SelectInt => build_generator!(self, R, SelectGenerator<SbrdInt>),
247 GeneratorType::SelectReal => build_generator!(self, R, SelectGenerator<SbrdReal>),
248 GeneratorType::SelectString => build_generator!(self, R, SelectGenerator<SbrdString>),
249 GeneratorType::GetIntValueAt => build_generator!(self, R, GetValueAtGenerator<SbrdInt>),
250 GeneratorType::GetRealValueAt => {
251 build_generator!(self, R, GetValueAtGenerator<SbrdReal>)
252 }
253 GeneratorType::GetStringValueAt => {
254 build_generator!(self, R, GetValueAtGenerator<SbrdString>)
255 }
256 GeneratorType::GetValueIndex => {
257 build_generator!(self, R, GetValueIndexGenerator)
258 }
259 }
260 }
261
262 pub fn into_parent<S>(self, key: S) -> ParentGeneratorBuilder
264 where
265 S: Into<String>,
266 {
267 ParentGeneratorBuilder::new(key, self)
268 }
269
270 pub fn into_child(self) -> ChildGeneratorBuilder {
272 ChildGeneratorBuilder::new(self)
273 }
274}
275
276impl GeneratorBuilder {
280 fn new(generator_type: GeneratorType) -> Self {
282 Self {
283 generator_type,
284 nullable: as_required(),
285 format: None,
286 script: None,
287 separator: None,
288 range: None,
289 increment: None,
290 children: None,
291 chars: None,
292 values: None,
293 filepath: None,
294 parameters: None,
295 }
296 }
297
298 pub fn new_duplicate_permutation<S>(
306 range: Option<ValueBound<SbrdInt>>,
307 separator: S,
308 children: Option<Vec<ChildGeneratorBuilder>>,
309 chars: Option<String>,
310 values: Option<Vec<DataValue>>,
311 filepath: Option<PathBuf>,
312 ) -> Self
313 where
314 S: Into<String>,
315 {
316 let mut this = Self::new(GeneratorType::DuplicatePermutation).separator(separator);
317 if let Some(range) = range {
318 this = this.range(range.convert_into());
319 }
320
321 if let Some(children) = children {
322 this = this.children(children);
323 }
324 if let Some(chars) = chars {
325 this = this.chars(chars);
326 }
327 if let Some(values) = values {
328 this = this.values(values);
329 }
330 if let Some(filepath) = filepath {
331 this = this.filepath(filepath);
332 }
333
334 this
335 }
336
337 pub fn new_format<S>(format: S) -> Self
341 where
342 S: Into<String>,
343 {
344 Self::new(GeneratorType::Format).format(format)
345 }
346
347 pub fn new_dist_normal(mean: SbrdReal, std_dev: SbrdReal) -> Self {
355 let mut parameters = DataValueMap::new();
356 parameters.insert(NormalGenerator::MEAN.to_string(), mean.into());
357 parameters.insert(NormalGenerator::STD_DEV.to_string(), std_dev.into());
358 Self::new(GeneratorType::DistNormal).parameters(parameters)
359 }
360
361 pub fn new_eval_int<S>(script: S) -> Self
370 where
371 S: Into<String>,
372 {
373 Self::new(GeneratorType::EvalInt).script(script)
374 }
375
376 pub fn new_eval_real<S>(script: S) -> Self
381 where
382 S: Into<String>,
383 {
384 Self::new(GeneratorType::EvalReal).script(script)
385 }
386
387 pub fn new_eval_bool<S>(script: S) -> Self
392 where
393 S: Into<String>,
394 {
395 Self::new(GeneratorType::EvalBool).script(script)
396 }
397
398 pub fn new_eval_string<S>(script: S) -> Self
403 where
404 S: Into<String>,
405 {
406 Self::new(GeneratorType::EvalString).script(script)
407 }
408
409 pub fn new_increment_id(increment: Option<ValueStep<SbrdInt>>) -> Self {
417 let mut this = Self::new(GeneratorType::IncrementId);
418
419 if let Some(_increment) = increment {
420 this = this.increment(_increment.convert_with(DataValue::from))
421 }
422
423 this
424 }
425
426 pub fn new_int(range: Option<ValueBound<SbrdInt>>) -> Self {
434 let mut this = Self::new(GeneratorType::Int);
435 if let Some(range) = range {
436 this = this.range(range.convert_into());
437 }
438
439 this
440 }
441
442 pub fn new_real(range: Option<ValueBound<SbrdReal>>) -> Self {
446 let mut this = Self::new(GeneratorType::Real);
447 if let Some(range) = range {
448 this = this.range(range.convert_into());
449 }
450
451 this
452 }
453
454 pub fn new_bool() -> Self {
458 Self::new(GeneratorType::Bool)
459 }
460
461 pub fn new_date_time(range: Option<ValueBound<SbrdDateTime>>, format: Option<String>) -> Self {
469 let mut this = Self::new(GeneratorType::DateTime);
470 if let Some(range) = range {
471 this = this.range(range.convert_with(|v| {
472 Into::<SbrdDateTime>::into(v)
473 .format(DATE_TIME_DEFAULT_FORMAT)
474 .to_string()
475 .into()
476 }));
477 }
478
479 if let Some(_format) = format {
480 this = this.format(_format);
481 }
482
483 this
484 }
485
486 pub fn new_date(range: Option<ValueBound<SbrdDate>>, format: Option<String>) -> Self {
494 let mut this = Self::new(GeneratorType::Date);
495 if let Some(range) = range {
496 this = this.range(range.convert_with(|v| {
497 Into::<SbrdDate>::into(v)
498 .format(DATE_DEFAULT_FORMAT)
499 .to_string()
500 .into()
501 }));
502 }
503
504 if let Some(_format) = format {
505 this = this.format(_format);
506 }
507
508 this
509 }
510
511 pub fn new_time(range: Option<ValueBound<SbrdTime>>, format: Option<String>) -> Self {
519 let mut this = Self::new(GeneratorType::Time);
520 if let Some(range) = range {
521 this = this.range(range.convert_with(|v| {
522 Into::<SbrdTime>::into(v)
523 .format(TIME_DEFAULT_FORMAT)
524 .to_string()
525 .into()
526 }));
527 }
528
529 if let Some(_format) = format {
530 this = this.format(_format);
531 }
532
533 this
534 }
535
536 pub fn new_always_null() -> Self {
540 Self::new(GeneratorType::AlwaysNull)
541 }
542
543 pub fn new_case_when(children: Vec<ChildGeneratorBuilder>) -> Self {
551 Self::new(GeneratorType::CaseWhen).children(children)
552 }
553
554 pub fn new_random_child(children: Vec<ChildGeneratorBuilder>) -> Self {
558 Self::new(GeneratorType::RandomChild).children(children)
559 }
560
561 pub fn new_select_int(
570 chars: Option<String>,
571 values: Option<Vec<SbrdInt>>,
572 filepath: Option<PathBuf>,
573 ) -> Self {
574 let mut this = Self::new(GeneratorType::SelectInt);
575 if let Some(chars) = chars {
576 this = this.chars(chars);
577 }
578 if let Some(values) = values {
579 this = this.values(values.into_iter().map(|v| v.into()).collect());
580 }
581 if let Some(filepath) = filepath {
582 this = this.filepath(filepath);
583 }
584
585 this
586 }
587
588 pub fn new_select_real(
593 chars: Option<String>,
594 values: Option<Vec<SbrdReal>>,
595 filepath: Option<PathBuf>,
596 ) -> Self {
597 let mut this = Self::new(GeneratorType::SelectReal);
598 if let Some(chars) = chars {
599 this = this.chars(chars);
600 }
601 if let Some(values) = values {
602 this = this.values(values.into_iter().map(|v| v.into()).collect());
603 }
604 if let Some(filepath) = filepath {
605 this = this.filepath(filepath);
606 }
607
608 this
609 }
610
611 pub fn new_select_string(
616 chars: Option<String>,
617 values: Option<Vec<SbrdString>>,
618 filepath: Option<PathBuf>,
619 ) -> Self {
620 let mut this = Self::new(GeneratorType::SelectString);
621 if let Some(chars) = chars {
622 this = this.chars(chars);
623 }
624 if let Some(values) = values {
625 this = this.values(values.into_iter().map(|v| v.into()).collect());
626 }
627 if let Some(filepath) = filepath {
628 this = this.filepath(filepath);
629 }
630
631 this
632 }
633
634 pub fn new_get_int_value_at<S>(
639 script: S,
640 chars: Option<String>,
641 values: Option<Vec<SbrdInt>>,
642 filepath: Option<PathBuf>,
643 ) -> Self
644 where
645 S: Into<String>,
646 {
647 let mut this = Self::new(GeneratorType::GetIntValueAt).script(script);
648
649 if let Some(chars) = chars {
650 this = this.chars(chars);
651 }
652 if let Some(values) = values {
653 this = this.values(values.into_iter().map(|v| v.into()).collect());
654 }
655 if let Some(filepath) = filepath {
656 this = this.filepath(filepath);
657 }
658
659 this
660 }
661
662 pub fn new_get_real_value_at<S>(
667 script: S,
668 chars: Option<String>,
669 values: Option<Vec<SbrdReal>>,
670 filepath: Option<PathBuf>,
671 ) -> Self
672 where
673 S: Into<String>,
674 {
675 let mut this = Self::new(GeneratorType::GetRealValueAt).script(script);
676
677 if let Some(chars) = chars {
678 this = this.chars(chars);
679 }
680 if let Some(values) = values {
681 this = this.values(values.into_iter().map(|v| v.into()).collect());
682 }
683 if let Some(filepath) = filepath {
684 this = this.filepath(filepath);
685 }
686
687 this
688 }
689
690 pub fn new_get_string_value_at<S>(
695 script: S,
696 chars: Option<String>,
697 values: Option<Vec<SbrdString>>,
698 filepath: Option<PathBuf>,
699 ) -> Self
700 where
701 S: Into<String>,
702 {
703 let mut this = Self::new(GeneratorType::GetStringValueAt).script(script);
704
705 if let Some(chars) = chars {
706 this = this.chars(chars);
707 }
708 if let Some(values) = values {
709 this = this.values(values.into_iter().map(|v| v.into()).collect());
710 }
711 if let Some(filepath) = filepath {
712 this = this.filepath(filepath);
713 }
714
715 this
716 }
717
718 pub fn new_get_value_index(
722 chars: Option<String>,
723 values: Option<Vec<DataValue>>,
724 filepath: Option<PathBuf>,
725 ) -> Self {
726 let mut this = Self::new(GeneratorType::GetValueIndex);
727
728 if let Some(chars) = chars {
729 this = this.chars(chars);
730 }
731 if let Some(values) = values {
732 this = this.values(values);
733 }
734 if let Some(filepath) = filepath {
735 this = this.filepath(filepath);
736 }
737
738 this
739 }
740}
741
742impl GeneratorBuilder {
746 pub fn nullable(mut self) -> Self {
748 self.nullable = true;
749 self
750 }
751
752 fn format<S>(mut self, format: S) -> Self
758 where
759 S: Into<String>,
760 {
761 self.format = Some(format.into());
762 self
763 }
764
765 fn script<S>(mut self, script: S) -> Self
767 where
768 S: Into<String>,
769 {
770 self.script = Some(script.into());
771 self
772 }
773
774 fn separator<S>(mut self, separator: S) -> Self
776 where
777 S: Into<String>,
778 {
779 self.separator = Some(separator.into());
780 self
781 }
782
783 fn range(mut self, range: ValueBound<DataValue>) -> Self {
785 self.range = Some(range);
786 self
787 }
788
789 fn increment(mut self, increment: ValueStep<DataValue>) -> Self {
791 self.increment = Some(increment);
792 self
793 }
794
795 fn children(mut self, children: Vec<ChildGeneratorBuilder>) -> Self {
797 self.children = Some(children);
798 self
799 }
800
801 fn chars<S>(mut self, chars: S) -> Self
803 where
804 S: Into<String>,
805 {
806 self.chars = Some(chars.into());
807 self
808 }
809
810 fn values(mut self, values: Vec<DataValue>) -> Self {
812 self.values = Some(values);
813 self
814 }
815
816 fn filepath<P>(mut self, filepath: P) -> Self
818 where
819 P: Into<PathBuf>,
820 {
821 self.filepath = Some(filepath.into());
822 self
823 }
824
825 fn parameters(mut self, parameters: DataValueMap<String>) -> Self {
827 self.parameters = Some(parameters);
828 self
829 }
830}