diff --git a/module/core/former/tests/inc/former_tests/subformer_container_setter_off.rs b/module/core/former/tests/inc/former_tests/subformer_container_setter_off.rs index b763e6a464..b20fe7e374 100644 --- a/module/core/former/tests/inc/former_tests/subformer_container_setter_off.rs +++ b/module/core/former/tests/inc/former_tests/subformer_container_setter_off.rs @@ -50,7 +50,7 @@ where } #[ test ] -fn basic() +fn container() { let got = Parent::former() diff --git a/module/core/former/tests/inc/former_tests/subformer_container_setter_on.rs b/module/core/former/tests/inc/former_tests/subformer_container_setter_on.rs new file mode 100644 index 0000000000..f8d963a732 --- /dev/null +++ b/module/core/former/tests/inc/former_tests/subformer_container_setter_on.rs @@ -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 ); + +} diff --git a/module/core/former/tests/inc/former_tests/subformer_subform_setter_off.rs b/module/core/former/tests/inc/former_tests/subformer_subform_setter_off.rs index 48f086e668..a467fd2bea 100644 --- a/module/core/former/tests/inc/former_tests/subformer_subform_setter_off.rs +++ b/module/core/former/tests/inc/former_tests/subformer_subform_setter_off.rs @@ -29,7 +29,6 @@ where Definition : former::FormerDefinition< Storage = < Parent as former::EntityToStorage >::Storage >, { - // xxx #[ inline( always ) ] pub fn children( self ) -> &'static str { @@ -51,7 +50,7 @@ where } #[ test ] -fn children() +fn subform() { let got = Parent::former() diff --git a/module/core/former/tests/inc/former_tests/subformer_subform_setter_on.rs b/module/core/former/tests/inc/former_tests/subformer_subform_setter_on.rs new file mode 100644 index 0000000000..2630abc97b --- /dev/null +++ b/module/core/former/tests/inc/former_tests/subformer_subform_setter_on.rs @@ -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 ); + +} diff --git a/module/core/former/tests/inc/mod.rs b/module/core/former/tests/inc/mod.rs index 77187d580d..8a4cf017f1 100644 --- a/module/core/former/tests/inc/mod.rs +++ b/module/core/former/tests/inc/mod.rs @@ -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; @@ -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; diff --git a/module/core/former_meta/src/derive/former.rs b/module/core/former_meta/src/derive/former.rs index 00e4445807..37be332632 100644 --- a/module/core/former_meta/src/derive/former.rs +++ b/module/core/former_meta/src/derive/former.rs @@ -8,7 +8,7 @@ use proc_macro2::TokenStream; /// Definition of a field. /// - +#[ allow( dead_code ) ] struct FormerField< 'a > { pub attrs : Attributes,