sbrd_gen/builder/
generator_builder.rs

1//! Module for builder
2
3use 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/// Generator Builder used in [`SchemeBuilder`] as Generator Builder.
28///
29/// [`SchemeBuilder`]: ../schema/struct.SchemaBuilder.html
30#[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    /// Create from [`GeneratorBuilder`]
39    ///
40    /// [`GeneratorBuilder`]: ./struct.GeneratorBuilder.html
41    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    /// Split to `key` and [`GeneratorBuilder`]
52    ///
53    /// [`GeneratorBuilder`]: ./struct.GeneratorBuilder.html
54    pub fn split_key(self) -> (String, GeneratorBuilder) {
55        let Self { key, builder } = self;
56        (key, builder)
57    }
58}
59
60/// Alias of Weight
61pub type Weight = u8;
62
63/// Generator Builder used in [`SchemeBuilder`] as Generator Builder's Children.
64///
65/// [`SchemeBuilder`]: ../schema/struct.SchemaBuilder.html
66#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
67pub struct ChildGeneratorBuilder {
68    /// Child Generator's `condition` option
69    #[serde(rename = "case", skip_serializing_if = "Option::is_none")]
70    pub(crate) condition: Option<String>,
71
72    /// Child Generator's `weight` option
73    #[serde(skip_serializing_if = "Option::is_none")]
74    pub(crate) weight: Option<Weight>,
75
76    /// Child Generator's base builder
77    #[serde(flatten)]
78    pub(crate) builder: GeneratorBuilder,
79}
80
81impl ChildGeneratorBuilder {
82    /// Create from [`GeneratorBuilder`]
83    ///
84    /// [`GeneratorBuilder`]: ./struct.GeneratorBuilder.html
85    fn new(builder: GeneratorBuilder) -> ChildGeneratorBuilder {
86        Self {
87            condition: None,
88            weight: None,
89            builder,
90        }
91    }
92
93    /// Set `condition` condition
94    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    /// Set `weight` condition
103    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/// Base Generator Builder
118#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
119pub struct GeneratorBuilder {
120    /// Generator's `type` parameter
121    ///
122    /// This is a type of the generator.
123    #[serde(rename = "type")]
124    pub(crate) generator_type: GeneratorType,
125
126    /// Generator's `nullable` status
127    ///
128    /// This is a nullable flag for the generator.
129    #[serde(skip_serializing_if = "is_required", default = "as_required")]
130    pub(crate) nullable: bool,
131
132    /// Generator's `format` option
133    ///
134    /// This is a format for the generated value.
135    /// Evaluate by [`Evaluator`] as String.
136    ///
137    /// [`Evaluator`]: ../eval/struct.Evaluator.html
138    #[serde(skip_serializing_if = "Option::is_none")]
139    pub(crate) format: Option<String>,
140
141    /// Generator's `script` option
142    ///
143    /// This is a script for the generated value.
144    /// Evaluate by [`Evaluator`] as not String.
145    ///
146    /// [`Evaluator`]: ../eval/struct.Evaluator.html
147    #[serde(skip_serializing_if = "Option::is_none")]
148    pub(crate) script: Option<String>,
149
150    /// Generator's `separator` option
151    ///
152    /// This separator use as glue to join the generated values.
153    #[serde(skip_serializing_if = "Option::is_none")]
154    pub(crate) separator: Option<String>,
155
156    /// Generator's `range` option
157    ///
158    /// This is a range for the generated value.
159    #[serde(skip_serializing_if = "Option::is_none")]
160    pub(crate) range: Option<ValueBound<DataValue>>,
161
162    /// Generator's `increment` option
163    ///
164    /// This is a data of each step values from initial for the generated value.
165    #[serde(skip_serializing_if = "Option::is_none")]
166    pub(crate) increment: Option<ValueStep<DataValue>>,
167
168    /// Generator's `children` option
169    ///
170    /// The generator pick out the child from this children.
171    #[serde(skip_serializing_if = "Option::is_none")]
172    pub(crate) children: Option<Vec<ChildGeneratorBuilder>>,
173
174    /// Generator's `characters` option
175    ///
176    /// The generator pick out the character from this characters.
177    #[serde(skip_serializing_if = "Option::is_none")]
178    pub(crate) chars: Option<String>,
179
180    /// Generator's `values` option
181    ///
182    /// The generator pick out the value from this values.
183    #[serde(skip_serializing_if = "Option::is_none")]
184    pub(crate) values: Option<Vec<DataValue>>,
185
186    /// Generator's `filepath` option
187    ///
188    /// The generator pick out the value from the lines in the file at the filepath.
189    #[serde(skip_serializing_if = "Option::is_none")]
190    pub(crate) filepath: Option<PathBuf>,
191
192    /// Generator's `parameters` option
193    ///
194    /// This is a parameter data set for a Distribution.
195    #[serde(skip_serializing_if = "Option::is_none")]
196    pub(crate) parameters: Option<DataValueMap<String>>,
197}
198
199/// Helper for build generator.
200macro_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
207//
208// building
209//
210impl GeneratorBuilder {
211    /// Build generator as the type
212    pub fn build<R: Randomizer + ?Sized>(self) -> Result<Box<dyn GeneratorBase<R>>, BuildError> {
213        match self.generator_type {
214            // build string
215            GeneratorType::DuplicatePermutation => {
216                build_generator!(self, R, DuplicatePermutationGenerator<R>)
217            }
218            GeneratorType::Format => build_generator!(self, R, FormatGenerator),
219
220            // distribution
221            GeneratorType::DistNormal => build_generator!(self, R, NormalGenerator),
222
223            // evaluate
224            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            // incremental
230            GeneratorType::IncrementId => build_generator!(self, R, IncrementIdGenerator),
231
232            // primitive
233            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            // randomize children
242            GeneratorType::CaseWhen => build_generator!(self, R, CaseWhenGenerator<R>),
243            GeneratorType::RandomChild => build_generator!(self, R, RandomChildGenerator<R>),
244
245            // randomize values
246            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    /// Convert this builder to parent builder
263    pub fn into_parent<S>(self, key: S) -> ParentGeneratorBuilder
264    where
265        S: Into<String>,
266    {
267        ParentGeneratorBuilder::new(key, self)
268    }
269
270    /// Convert this builder to child
271    pub fn into_child(self) -> ChildGeneratorBuilder {
272        ChildGeneratorBuilder::new(self)
273    }
274}
275
276//
277// constructor functions following:
278//
279impl GeneratorBuilder {
280    /// Create generate builder as the type.
281    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    //
299    // build string
300    //
301
302    /// Create builder for [`DuplicatePermutationGenerator`] as generator
303    ///
304    /// [`DuplicatePermutationGenerator`]: ../generator/build_string/duplicate_permutation/struct.DuplicatePermutationGenerator.html
305    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    /// Create builder for [`FormatGenerator`]
338    ///
339    /// [`FormatGenerator`]: ../generator/build_string/format_generator/struct.FormatGenerator.html
340    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    //
348    // distribution
349    //
350
351    /// Create builder for [`NormalGenerator`]
352    ///
353    /// [`NormalGenerator`]: ../generator/distribution/normal_generator/struct.NormalGenerator.html
354    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    //
362    // evaluate
363    //
364
365    /// Create builder for [`EvalGenerator`] with type [`SbrdInt`]
366    ///
367    /// [`EvalGenerator`]: ../generator/evaluate/eval_generator/struct.EvalGenerator.html
368    /// [`SbrdInt`]: ../value/type.SbrdInt.html
369    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    /// Create builder for [`EvalGenerator`] with type [`SbrdReal`]
377    ///
378    /// [`EvalGenerator`]: ../generator/evaluate/eval_generator/struct.EvalGenerator.html
379    /// [`SbrdReal`]: ../value/type.SbrdReal.html
380    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    /// Create builder for [`EvalGenerator`] with type [`SbrdBool`]
388    ///
389    /// [`EvalGenerator`]: ../generator/evaluate/eval_generator/struct.EvalGenerator.html
390    /// [`SbrdBool`]: ../value/type.SbrdBool.html
391    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    /// Create builder for [`EvalGenerator`] with type [`SbrdString`]
399    ///
400    /// [`EvalGenerator`]: ../generator/evaluate/eval_generator/struct.EvalGenerator.html
401    /// [`SbrdString`]: ../value/type.SbrdString.html
402    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    //
410    // incremental
411    //
412
413    /// Create builder for [`IncrementIdGenerator`]
414    ///
415    /// [`IncrementIdGenerator`]: ../generator/incremental/increment_id_generator/struct.IncrementIdGenerator.html
416    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    //
427    // primitive
428    //
429
430    /// Create builder for [`IntGenerator`]
431    ///
432    /// [`IntGenerator`]: ../generator/primitive/int_generator/struct.IntGenerator.html
433    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    /// Create builder for [`RealGenerator`]
443    ///
444    /// [`RealGenerator`]: ../generator/primitive/real_generator/struct.RealGenerator.html
445    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    /// Create builder for [`BoolGenerator`]
455    ///
456    /// [`BoolGenerator`]: ../generator/primitive/bool_generator/struct.BoolGenerator.html
457    pub fn new_bool() -> Self {
458        Self::new(GeneratorType::Bool)
459    }
460
461    /// Create builder for [`DateTimeGenerator`].
462    /// See [`format::strftime` module] for more information on `format` option.
463    /// The default for `format` and the format when parsing is [`DATE_TIME_DEFAULT_FORMAT`].
464    ///
465    /// [`DateTimeGenerator`]: ../generator/primitive/date_time_generator/struct.DateTimeGenerator.html
466    /// [`format::strftime` module]: https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html#specifiers
467    /// [`DATE_TIME_DEFAULT_FORMAT`]: ../value/constant.DATE_TIME_DEFAULT_FORMAT.html
468    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    /// Create builder for [`DateGenerator`]
487    /// See [`format::strftime` module] for more information on `format` option.
488    /// The default for `format` and the format when parsing is [`DATE_DEFAULT_FORMAT`].
489    ///
490    /// [`DateGenerator`]: ../generator/primitive/date_generator/struct.DateGenerator.html
491    /// [`format::strftime` module]: https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html#specifiers
492    /// [`DATE_DEFAULT_FORMAT`]: ../value/constant.DATE_DEFAULT_FORMAT.html
493    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    /// Create builder for [`TimeGenerator`]
512    /// See [`format::strftime` module] for more information on `format` option.
513    /// The default for `format` and the format when parsing is [`TIME_DEFAULT_FORMAT`].
514    ///
515    /// [`TimeGenerator`]: ../generator/primitive/time_generator/struct.TimeGenerator.html
516    /// [`format::strftime` module]: https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html#specifiers
517    /// [`TIME_DEFAULT_FORMAT`]: ../value/constant.TIME_DEFAULT_FORMAT.html
518    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    /// Create builder for [`AlwaysNullGenerator`]
537    ///
538    /// [`AlwaysNullGenerator`]: ../generator/primitive/always_null_generator/struct.AlwaysNullGenerator.html
539    pub fn new_always_null() -> Self {
540        Self::new(GeneratorType::AlwaysNull)
541    }
542
543    //
544    // randomize children
545    //
546
547    /// Create builder for [`CaseWhenGenerator`] as generator with generate from children
548    ///
549    /// [`CaseWhenGenerator`]: ../generator/random_children/case_when_generator/struct.CaseWhenGenerator.html
550    pub fn new_case_when(children: Vec<ChildGeneratorBuilder>) -> Self {
551        Self::new(GeneratorType::CaseWhen).children(children)
552    }
553
554    /// Create builder for [`RandomChildGenerator`] as generator with generate from children
555    ///
556    /// [`RandomChildGenerator`]: ../generator/random_children/random_child_generator/struct.RandomChildGenerator.html
557    pub fn new_random_child(children: Vec<ChildGeneratorBuilder>) -> Self {
558        Self::new(GeneratorType::RandomChild).children(children)
559    }
560
561    //
562    // randomize values
563    //
564
565    /// Create builder for [`SelectGenerator`] with type [`SbrdInt`]
566    ///
567    /// [`SelectGenerator`]: ../generator/random_values/select_generator/struct.SelectGenerator.html
568    /// [`SbrdInt`]: ../value/type.SbrdInt.html
569    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    /// Create builder for [`SelectGenerator`] with type [`SbrdReal`]
589    ///
590    /// [`SelectGenerator`]: ../generator/random_values/select_generator/struct.SelectGenerator.html
591    /// [`SbrdReal`]: ../value/type.SbrdReal.html
592    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    /// Create builder for [`SelectGenerator`] with type [`SbrdString`]
612    ///
613    /// [`SelectGenerator`]: ../generator/random_values/select_generator/struct.SelectGenerator.html
614    /// [`SbrdString`]: ../value/type.SbrdString.html
615    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    /// Create builder for [`GetValueAtGenerator`] with type [`SbrdInt`]
635    ///
636    /// [`GetValueAtGenerator`]: ../generator/random_values/get_value_at_generator/struct.GetValueAtGenerator.html
637    /// [`SbrdInt`]: ../value/type.SbrdInt.html
638    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    /// Create builder for [`GetValueAtGenerator`] with type [`SbrdReal`]
663    ///
664    /// [`GetValueAtGenerator`]: ../generator/random_values/get_value_at_generator/struct.GetValueAtGenerator.html
665    /// [`SbrdReal`]: ../value/type.SbrdReal.html
666    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    /// Create builder for [`GetValueAtGenerator`] with type [`SbrdString`]
691    ///
692    /// [`GetValueAtGenerator`]: ../generator/random_values/get_value_at_generator/struct.GetValueAtGenerator.html
693    /// [`SbrdString`]: ../value/type.SbrdString.html
694    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    /// Create builder for [`GetValueIndexGenerator`]
719    ///
720    /// [`GetValueIndexGenerator`]: ../generator/random_values/get_value_index_generator/struct.GetValueIndexGenerator.html
721    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
742//
743// build parameter functions following:
744//
745impl GeneratorBuilder {
746    /// Set `nullable` status to change to nullable
747    pub fn nullable(mut self) -> Self {
748        self.nullable = true;
749        self
750    }
751
752    //
753    // setter
754    //
755
756    /// Set `format` option
757    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    /// Set `script` option
766    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    /// Set `separator` option
775    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    /// Set `range` option
784    fn range(mut self, range: ValueBound<DataValue>) -> Self {
785        self.range = Some(range);
786        self
787    }
788
789    /// Set `range` option
790    fn increment(mut self, increment: ValueStep<DataValue>) -> Self {
791        self.increment = Some(increment);
792        self
793    }
794
795    /// Set `children` option
796    fn children(mut self, children: Vec<ChildGeneratorBuilder>) -> Self {
797        self.children = Some(children);
798        self
799    }
800
801    /// Set `characters` option
802    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    /// Set `values` option
811    fn values(mut self, values: Vec<DataValue>) -> Self {
812        self.values = Some(values);
813        self
814    }
815
816    /// Set `filepath` option
817    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    /// Set `parameters` option
826    fn parameters(mut self, parameters: DataValueMap<String>) -> Self {
827        self.parameters = Some(parameters);
828        self
829    }
830}