former 2.45.0

A flexible implementation of the Builder pattern supporting nested builders and collection-specific subformers. Simplify the construction of complex objects.
Documentation
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#![allow(clippy::used_underscore_binding, clippy::all, warnings, missing_docs)]
#![allow(dead_code)]
#[ allow( unused_imports ) ]
use super::*;
#[ allow( unused_imports ) ]
use test_tools::a_id;

#[ derive( Debug, PartialEq, Default ) ]
pub struct Property<Name> {
  name: Name,
  code: isize,
}

/// generated by new
impl<Name> Property<Name> {
  #[ inline ]
  pub fn new<Code>(name: Name, code: Code) -> Self
  where
    Name: core::convert::Into<Name>,
    Code: core::convert::Into<isize>,
  {
    Self {
      name,
      code: code.into(),
    }
  }
}


// #[ derive( Debug, PartialEq, the_module::Former ) ]
// #[ derive( Debug, PartialEq, the_module::Former ) ] #[ debug ]
#[ derive( Debug, PartialEq ) ]
pub struct Child<K>
where
  K: core::hash::Hash + core::cmp::Eq,
{
  pub name: String,
  // #[ subform_collection( definition = former::HashMapDefinition ) ]
  pub properties: collection_tools::HashMap<K, Property<K>>,
}

// == begin_coercing of generated

#[ automatically_derived ]
impl<K> Child<K>
where
  K: core::hash::Hash + core::cmp::Eq,
{
  #[ inline( always ) ]
  pub fn former() -> ChildFormer<K, ChildFormerDefinition<K, (), Child<K>, former::ReturnPreformed>> {
    ChildFormer::<K, ChildFormerDefinition<K, (), Child<K>, former::ReturnPreformed>>::new(former::ReturnPreformed)
  }
}

#[ derive( Debug ) ]
pub struct ChildFormerDefinitionTypes<K, __Context = (), __Formed = Child<K>>
where
  K: core::hash::Hash + core::cmp::Eq,
{
  _phantom: core::marker::PhantomData<(K, __Context, __Formed)>,
}

impl<K, __Context, __Formed> ::core::default::Default for ChildFormerDefinitionTypes<K, __Context, __Formed>
where
  K: core::hash::Hash + core::cmp::Eq,
{
  fn default() -> Self {
    Self {
      _phantom: core::marker::PhantomData,
    }
  }
}

impl<K, __Context, __Formed> former::FormerDefinitionTypes for ChildFormerDefinitionTypes<K, __Context, __Formed>
where
  K: core::hash::Hash + core::cmp::Eq,
{
  type Storage = ChildFormerStorage<K>;
  type Formed = __Formed;
  type Context = __Context;
}

impl<K, Context, Formed> former::FormerMutator for ChildFormerDefinitionTypes<K, Context, Formed> where
  K: core::hash::Hash + core::cmp::Eq
{
}

#[ derive( Debug ) ]
pub struct ChildFormerDefinition<K, __Context = (), __Formed = Child<K>, __End = former::ReturnPreformed>
where
  K: core::hash::Hash + core::cmp::Eq,
{
  _phantom: core::marker::PhantomData<(K, __Context, __Formed, __End)>,
}

impl<K, __Context, __Formed, __End> ::core::default::Default for ChildFormerDefinition<K, __Context, __Formed, __End>
where
  K: core::hash::Hash + core::cmp::Eq,
{
  fn default() -> Self {
    Self {
      _phantom: core::marker::PhantomData,
    }
  }
}

impl<K, __Context, __Formed, __End> former::FormerDefinition for ChildFormerDefinition<K, __Context, __Formed, __End>
where
  __End: former::FormingEnd<ChildFormerDefinitionTypes<K, __Context, __Formed>>,
  K: core::hash::Hash + core::cmp::Eq,
{
  type Types = ChildFormerDefinitionTypes<K, __Context, __Formed>;
  type End = __End;
  type Storage = ChildFormerStorage<K>;
  type Formed = __Formed;
  type Context = __Context;
}

// pub type ChildFormerWithClosure< K, __Context, __Formed, > = ChildFormerDefinition< K, __Context, __Formed, former :: FormingEndClosure< ChildFormerDefinitionTypes< K, __Context, __Formed, > > >;

pub struct ChildFormerStorage<K>
where
  K: core::hash::Hash + core::cmp::Eq,
{
  pub name: ::core::option::Option<String>,

  pub properties: ::core::option::Option<collection_tools::HashMap<K, Property<K>>>,
}

impl<K> ::core::default::Default for ChildFormerStorage<K>
where
  K: core::hash::Hash + core::cmp::Eq,
{
  #[ inline( always ) ]
  fn default() -> Self {
    Self {
      name: ::core::option::Option::None,
      properties: ::core::option::Option::None,
    }
  }
}

impl<K> former::Storage for ChildFormerStorage<K>
where
  K: core::hash::Hash + core::cmp::Eq,
{
  type Preformed = Child<K>;
}

impl<K> former::StoragePreform for ChildFormerStorage<K>
where
  K: core::hash::Hash + core::cmp::Eq,
{
  // type Preformed = Child< K, >;

  fn preform(mut self) -> Self::Preformed
// fn preform( mut self ) -> < Self as former :: Storage > :: Formed
  {
    let name = if self.name.is_some() {
      self.name.take().unwrap()
    } else {
      {
        trait MaybeDefault<T> {
          fn maybe_default(&self) -> T {
            panic!("Field 'name' isn't initialized")
          }
        }
        impl<T> MaybeDefault<T> for &::core::marker::PhantomData<T> {}
        impl<T> MaybeDefault<T> for ::core::marker::PhantomData<T>
        where
          T: ::core::default::Default,
        {
          fn maybe_default(&self) -> T {
            T::default()
          }
        }
        ::core::marker::PhantomData::<String>.maybe_default()
      }
    };

    let properties = if self.properties.is_some() {
      self.properties.take().unwrap()
    } else {
      {
        trait MaybeDefault<T> {
          fn maybe_default(&self) -> T {
            panic!("Field 'properties' isn't initialized")
          }
        }
        impl<T> MaybeDefault<T> for &::core::marker::PhantomData<T> {}
        impl<T> MaybeDefault<T> for ::core::marker::PhantomData<T>
        where
          T: ::core::default::Default,
        {
          fn maybe_default(&self) -> T {
            T::default()
          }
        }
        ::core::marker::PhantomData::<collection_tools::HashMap<K, Property<K>>>.maybe_default()
      }
    };

    
    Child::<K> { name, properties }
  }
}

pub struct ChildFormer<K, Definition = ChildFormerDefinition<K, (), Child<K>, former::ReturnPreformed>>
where
  K: core::hash::Hash + core::cmp::Eq,
  Definition: former::FormerDefinition<Storage = ChildFormerStorage<K>>,
  // Definition::Types : former::FormerDefinitionTypes< Storage = ChildFormerStorage< K, > >
{
  storage: Definition::Storage,
  context: core::option::Option<Definition::Context>,
  on_end: core::option::Option<Definition::End>,
}

#[ automatically_derived ]
impl<K, Definition> ChildFormer<K, Definition>
where
  K: core::hash::Hash + core::cmp::Eq,
  Definition: former::FormerDefinition<Storage = ChildFormerStorage<K>>, // Definition::Types : former::FormerDefinitionTypes< Storage = ChildFormerStorage< K, > >,
{
  #[ inline( always ) ]
  pub fn perform(self) -> <Definition::Types as former::FormerDefinitionTypes>::Formed {
    
    self.form()
  }

  #[ inline( always ) ]
  pub fn new(on_end: Definition::End) -> Self {
    Self::begin_coercing(None, None, on_end)
  }

  #[ inline( always ) ]
  pub fn new_coercing<IntoEnd>(end: IntoEnd) -> Self
  where
    IntoEnd: Into<Definition::End>,
  {
    Self::begin_coercing(None, None, end)
  }

  #[ inline( always ) ]
  pub fn begin(
    mut storage: core::option::Option<Definition::Storage>,
    context: core::option::Option<Definition::Context>,
    on_end: <Definition as former::FormerDefinition>::End,
  ) -> Self {
    if storage.is_none() {
      storage = Some(::core::default::Default::default());
    }
    Self {
      storage: storage.unwrap(),
      context,
      on_end: ::core::option::Option::Some(on_end),
    }
  }

  #[ inline( always ) ]
  pub fn begin_coercing<IntoEnd>(
    mut storage: core::option::Option<Definition::Storage>,
    context: core::option::Option<Definition::Context>,
    on_end: IntoEnd,
  ) -> Self
  where
    IntoEnd: ::core::convert::Into<<Definition as former::FormerDefinition>::End>,
  {
    if storage.is_none() {
      storage = Some(::core::default::Default::default());
    }
    Self {
      storage: storage.unwrap(),
      context,
      on_end: ::core::option::Option::Some(::core::convert::Into::into(on_end)),
    }
  }

  #[ inline( always ) ]
  pub fn form(self) -> <Definition::Types as former::FormerDefinitionTypes>::Formed {
    self.end()
  }

  #[ inline( always ) ]
  pub fn end(mut self) -> <Definition::Types as former::FormerDefinitionTypes>::Formed {
    let on_end = self.on_end.take().unwrap();
    let mut context = self.context.take();
    <Definition::Types as former::FormerMutator>::form_mutation(&mut self.storage, &mut context);
    former::FormingEnd::<Definition::Types>::call(&on_end, self.storage, context)
  }

  #[ inline ]
  pub fn name<Src>(mut self, src: Src) -> Self
  where
    Src: ::core::convert::Into<String>,
  {
    debug_assert!(self.storage.name.is_none());
    self.storage.name = ::core::option::Option::Some(::core::convert::Into::into(src));
    self
  }

  #[ inline( always ) ]
  pub fn _properties_assign<'a, Former2>(self) -> Former2
  where
    K: 'a,
    Definition: 'a,
    Former2: former::FormerBegin<'a, former::HashMapDefinition<K, Property<K>, Self, Self, ChildFormerPropertiesEnd<Definition>>>,
    former::HashMapDefinition<K, Property<K>, Self, Self, ChildFormerPropertiesEnd<Definition>>: former::FormerDefinition<
      Storage = collection_tools::HashMap<K, Property<K>>,
      Context = ChildFormer<K, Definition>,
      End = ChildFormerPropertiesEnd<Definition>,
    >,
    ChildFormerPropertiesEnd<Definition>:
      former::FormingEnd<<collection_tools::HashMap<K, Property<K>> as former::EntityToDefinitionTypes<Self, Self>>::Types>,
  {
    Former2::former_begin(None, Some(self), ChildFormerPropertiesEnd::<Definition>::default())
  }

  #[ inline( always ) ]
  pub fn properties<'a>(
    self,
  ) -> former::CollectionFormer<
    (K, Property<K>),
    former::HashMapDefinition<K, Property<K>, Self, Self, ChildFormerPropertiesEnd<Definition>>,
  >
  where
    K: 'a,
    Definition: 'a,
    former::HashMapDefinition<K, Property<K>, Self, Self, ChildFormerPropertiesEnd<Definition>>: former::FormerDefinition<
      Storage = collection_tools::HashMap<K, Property<K>>,
      Context = ChildFormer<K, Definition>,
      End = ChildFormerPropertiesEnd<Definition>,
    >,
    ChildFormerPropertiesEnd<Definition>:
      former::FormingEnd<<collection_tools::HashMap<K, Property<K>> as former::EntityToDefinitionTypes<Self, Self>>::Types>,
  {
    self._properties_assign::<'a, former::CollectionFormer<
      (K, Property<K>),
      former::HashMapDefinition<K, Property<K>, Self, Self, ChildFormerPropertiesEnd<Definition>>,
    >>()
  }
}

//

impl<K, Definition> ChildFormer<K, Definition>
where
  K: core::hash::Hash + core::cmp::Eq,
  Definition: former::FormerDefinition<Storage = ChildFormerStorage<K>, Formed = Child<K>>,
  // Definition::Types : former::FormerDefinitionTypes< Storage = ChildFormerStorage< K, >, Formed = Child< K, > >,
  Definition::Storage: former::StoragePreform<Preformed = Child<K>>,
{
  pub fn preform(self) -> <Definition::Types as former::FormerDefinitionTypes>::Formed {
    former::StoragePreform::preform(self.storage)
  }
}

pub struct ChildFormerPropertiesEnd<Definition> {
  _phantom: core::marker::PhantomData<(Definition,)>,
}

impl<Definition> Default for ChildFormerPropertiesEnd<Definition> {
  fn default() -> Self {
    Self {
      _phantom: core::marker::PhantomData,
    }
  }
}

impl<K, Definition>
  former::FormingEnd<former::HashMapDefinitionTypes<K, Property<K>, ChildFormer<K, Definition>, ChildFormer<K, Definition>>>
  for ChildFormerPropertiesEnd<Definition>
where
  K: core::hash::Hash + core::cmp::Eq,
  Definition: former::FormerDefinition<Storage = ChildFormerStorage<K>>,
  Definition::Types: former::FormerDefinitionTypes<Storage = ChildFormerStorage<K>>,
{
  #[ inline( always ) ]
  fn call(
    &self,
    storage: collection_tools::HashMap<K, Property<K>>,
    super_former: Option<ChildFormer<K, Definition>>,
  ) -> ChildFormer<K, Definition> {
    let mut super_former = super_former.unwrap();
    if let Some(ref mut field) = super_former.storage.properties {
      former::CollectionAssign::assign(field, storage);
    } else {
      super_former.storage.properties = Some(storage);
    }
    super_former
  }
}

impl<'a, K, Definition> former::FormerBegin<'a, Definition> for ChildFormer<K, Definition>
where
  K: core::hash::Hash + core::cmp::Eq + 'a,
  Definition: former::FormerDefinition<Storage = ChildFormerStorage<K>>,
  Definition::Context: 'a,
  Definition::End: 'a,
{
  #[ inline( always ) ]
  fn former_begin(
    storage: ::core::option::Option<Definition::Storage>,
    context: ::core::option::Option<Definition::Context>,
    on_end: Definition::End,
  ) -> Self {
    debug_assert!(storage.is_none());
    Self::begin(None, context, on_end)
  }
}

// == end of generated

include!("./only_test/parametrized_struct.rs");