Skip to content

Commit

Permalink
former : experimenting
Browse files Browse the repository at this point in the history
  • Loading branch information
Wandalen committed May 2, 2024
1 parent 02fd5d1 commit 1f00d0c
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ where
}

#[ test ]
fn basic()
fn container()
{

let got = Parent::former()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#![ allow( dead_code ) ]

use super::*;

/// Child
#[ derive( Debug, Default, PartialEq, the_module::Former ) ]
pub struct Child
{
name : String,
is_mandatory : bool,
}

/// Parent
#[ derive( Debug, Default, PartialEq, the_module::Former ) ]
// #[ debug ]
// #[ derive( Debug, Default, PartialEq ) ]
pub struct Parent
{
// Such parameters switch off generation of front-end container setter and switch on scalar setter.
// Without explicit scalar_setter( true ) scalar setter is not generated.
#[ subform( setter = false ) ]
#[ scalar_setter( true ) ]
children : Vec< Child >,
}

impl< Definition > ParentFormer< Definition >
where
Definition : former::FormerDefinition< Storage = < Parent as former::EntityToStorage >::Storage >,
{

#[ inline( always ) ]
pub fn children2( self ) -> former::ContainerSubformer::
<
Child,
former::VectorDefinition< Child, Self, Self, ParentFormerAssignChildrenEnd< Definition >, >
>
{
self._children_assign::< _ >()
}

}

#[ test ]
fn scalar()
{

let children = vec!
[
Child { name : "a".to_string(), is_mandatory : false },
Child { name : "b".to_string(), is_mandatory : false },
];
let got = Parent::former()
.children( children )
.form();

let children = vec!
[
Child { name : "a".to_string(), is_mandatory : false },
Child { name : "b".to_string(), is_mandatory : false },
];
let exp = Parent { children };
a_id!( got, exp );

}

#[ test ]
fn container()
{

let got = Parent::former()
.children2()
.add( Child::former().name( "a" ).form() )
.add( Child::former().name( "b" ).form() )
.end()
.form();

let children = vec!
[
Child { name : "a".to_string(), is_mandatory : false },
Child { name : "b".to_string(), is_mandatory : false },
];
let exp = Parent { children };
a_id!( got, exp );

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ where
Definition : former::FormerDefinition< Storage = < Parent as former::EntityToStorage >::Storage >,
{

// xxx
#[ inline( always ) ]
pub fn children( self ) -> &'static str
{
Expand All @@ -51,7 +50,7 @@ where
}

#[ test ]
fn children()
fn subform()
{

let got = Parent::former()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#![ allow( dead_code ) ]

use super::*;

/// Child
#[ derive( Debug, Default, PartialEq, the_module::Former ) ]
pub struct Child
{
name : String,
is_mandatory : bool,
}

/// Parent
#[ derive( Debug, Default, PartialEq, the_module::Former ) ]
// #[ debug ]
// #[ derive( Debug, Default, PartialEq ) ]
pub struct Parent
{
// Such parameters switch off generation of front-end subform setter and switch on scalar setter.
// Without explicit scalar_setter( true ) scalar setter is not generated.
#[ subform( setter = false ) ]
#[ scalar_setter( true ) ]
children : Vec< Child >,
}

impl< Definition > ParentFormer< Definition >
where
Definition : former::FormerDefinition< Storage = < Parent as former::EntityToStorage >::Storage >,
{

#[ inline( always ) ]
pub fn children2( self, name : &str ) ->
ChildAsSubformer< Self, impl ChildAsSubformerEnd< Self > >
{
self._children_add
::< ChildFormer< _ >, _, >()
.name( name )
}

}

#[ test ]
fn scalar()
{

let children = vec!
[
Child { name : "a".to_string(), is_mandatory : false },
Child { name : "b".to_string(), is_mandatory : false },
];
let got = Parent::former()
.children( children )
.form();

let children = vec!
[
Child { name : "a".to_string(), is_mandatory : false },
Child { name : "b".to_string(), is_mandatory : false },
];
let exp = Parent { children };
a_id!( got, exp );

}

#[ test ]
fn subform()
{

let got = Parent::former()
.children2( "a" ).end()
.children2( "b" ).end()
.form();

let children = vec!
[
Child { name : "a".to_string(), is_mandatory : false },
Child { name : "b".to_string(), is_mandatory : false },
];
let exp = Parent { children };
a_id!( got, exp );

}
6 changes: 4 additions & 2 deletions module/core/former/tests/inc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ mod former_tests
// mod subformer_container;
// #[ cfg( any( not( feature = "no_std" ) ) ) ]
// mod subformer_container_manual;
// #[ cfg( any( not( feature = "no_std" ) ) ) ]
// mod subformer_container_implicit;
#[ cfg( any( not( feature = "no_std" ) ) ) ]
mod subformer_container_implicit;
#[ cfg( any( not( feature = "no_std" ) ) ) ]
mod subformer_container_setter_off;

Expand All @@ -80,6 +80,8 @@ mod former_tests
// mod subformer_subform_named_manual;
#[ cfg( any( not( feature = "no_std" ) ) ) ]
mod subformer_subform_setter_off;
#[ cfg( any( not( feature = "no_std" ) ) ) ]
mod subformer_subform_setter_on;

// #[ cfg( any( not( feature = "no_std" ) ) ) ]
// mod subformer_subform_hashmap;
Expand Down
2 changes: 1 addition & 1 deletion module/core/former_meta/src/derive/former.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use proc_macro2::TokenStream;
/// Definition of a field.
///

#[ allow( dead_code ) ]
struct FormerField< 'a >
{
pub attrs : Attributes,
Expand Down

0 comments on commit 1f00d0c

Please sign in to comment.