From 45f23d9414ca9442bcfe34be7631e0b1e662ea53 Mon Sep 17 00:00:00 2001 From: wandalen Date: Sat, 30 Mar 2024 10:26:27 +0200 Subject: [PATCH] former : experimenting --- .../inc/former_tests/a_primitives_expanded.rs | 199 ------------------ .../inc/former_tests/a_primitives_manual.rs | 24 ++- .../former_tests/container_former_common.rs | 2 - .../inc/former_tests/only_test/primitives.rs | 11 +- module/core/former_meta/src/derive/former.rs | 33 ++- 5 files changed, 51 insertions(+), 218 deletions(-) diff --git a/module/core/former/tests/inc/former_tests/a_primitives_expanded.rs b/module/core/former/tests/inc/former_tests/a_primitives_expanded.rs index 8c87153d6d..c34dc11b37 100644 --- a/module/core/former/tests/inc/former_tests/a_primitives_expanded.rs +++ b/module/core/former/tests/inc/former_tests/a_primitives_expanded.rs @@ -13,205 +13,6 @@ pub struct Struct1 // = generated -// #[automatically_derived] impl Struct1 -// { -// #[doc = r""] -// #[doc = -// r" Make former, variation of builder pattern to form structure defining values of fields step by step."] -// #[doc = r""] #[inline(always)] pub fn former() -> Struct1Former < > -// { Struct1Former :: new() } -// } #[derive(Debug)] pub struct Struct1FormerDefinition < Context = (), Formed = -// Struct1, End = former :: ReturnPreformed > -// { _phantom : core :: marker :: PhantomData < (Context, Formed, End) >, } impl -// < Context, Formed, End > Default for Struct1FormerDefinition < Context, -// Formed, End > -// { -// fn default() -> Self -// { Self { _phantom : core :: marker :: PhantomData, } } -// } impl < Context, Formed > former :: FormerDefinitionTypes for -// Struct1FormerDefinition < Context, Formed, former :: NoEnd > -// { -// type Storage = Struct1FormerStorage ; type Formed = Formed ; type Context -// = Context ; -// } impl < Context, Formed, End > former :: FormerDefinition for -// Struct1FormerDefinition < Context, Formed, End > where End : former :: -// FormingEnd < Struct1FormerDefinition < Context, Formed, former :: NoEnd > >, -// { -// type Types = Struct1FormerDefinition < Context, Formed, former :: NoEnd > -// ; type End = End ; -// } #[doc = "Container of a corresponding former."] pub struct -// Struct1FormerStorage -// { -// #[doc = r" A field"] pub int_1 : :: core :: option :: Option < i32 >, -// #[doc = r" A field"] pub string_1 : :: core :: option :: Option < String -// >, #[doc = r" A field"] pub int_optional_1 : core :: option :: Option < -// i32 >, #[doc = r" A field"] pub string_optional_1 : Option < String >, -// } impl :: core :: default :: Default for Struct1FormerStorage -// { -// #[inline(always)] fn default() -> Self -// { -// Self -// { -// int_1 : :: core :: option :: Option :: None, string_1 : :: core :: -// option :: Option :: None, int_optional_1 : :: core :: option :: -// Option :: None, string_optional_1 : :: core :: option :: Option :: -// None, -// } -// } -// } impl former :: Storage for Struct1FormerStorage { type Formed = Struct1 ; } -// impl former :: StoragePerform for Struct1FormerStorage -// { -// fn preform(mut self) -> < Self as former :: Storage > :: Formed -// { -// let int_1 = if self.int_1.is_some() { self.int_1.take().unwrap() } -// else -// { -// { -// trait MaybeDefault < T > -// { -// fn maybe_default(self : & Self) -> T -// { panic! ("Field 'int_1' 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 : & Self) -> T { T :: default() } } -// (& :: core :: marker :: PhantomData :: < i32 -// >).maybe_default() -// } -// } ; let string_1 = if self.string_1.is_some() -// { self.string_1.take().unwrap() } else -// { -// { -// trait MaybeDefault < T > -// { -// fn maybe_default(self : & Self) -> T -// { panic! ("Field 'string_1' 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 : & Self) -> T { T :: default() } } -// (& :: core :: marker :: PhantomData :: < String -// >).maybe_default() -// } -// } ; let int_optional_1 = if self.int_optional_1.is_some() -// { -// :: core :: option :: Option :: -// Some(self.int_optional_1.take().unwrap()) -// } else { :: core :: option :: Option :: None } ; let string_optional_1 -// = if self.string_optional_1.is_some() -// { -// :: core :: option :: Option :: -// Some(self.string_optional_1.take().unwrap()) -// } else { :: core :: option :: Option :: None } ; let result = Struct1 -// { int_1, string_1, int_optional_1, string_optional_1, } ; return -// result ; -// } -// } -// #[doc = -// " Object to form [Struct1]. If field's values is not set then default value of the field is set.\n\nFor specifying custom default value use attribute `default`. For example:\n```\n\nuse former::Former;\n#[ derive( Former ) ]\npub struct Struct1\n{\n #[default( 31 ) ]\n field1 : i32,\n}\n\n```\n"] -// pub struct Struct1Former < Definition = Struct1FormerDefinition > where -// Definition : former :: FormerDefinition, < Definition :: Types as former :: -// FormerDefinitionTypes > :: Storage : former :: StoragePerform, Definition :: -// Types : former :: FormerDefinitionTypes < Storage = Struct1FormerStorage >, -// { -// storage : < Definition :: Types as former :: FormerDefinitionTypes > :: -// Storage, context : core :: option :: Option < < Definition :: Types as -// former :: FormerDefinitionTypes > :: Context >, on_end : core :: option :: -// Option < Definition :: End >, -// } #[automatically_derived] impl < Definition > Struct1Former < Definition > -// where Definition : former :: FormerDefinition, < Definition :: Types as former -// :: FormerDefinitionTypes > :: Storage : former :: StoragePerform, Definition -// :: Types : former :: FormerDefinitionTypes < Storage = Struct1FormerStorage >, -// { -// #[doc = r""] -// #[doc = r" Finish setting options and call perform on formed entity."] -// #[doc = r""] -// #[doc = -// r" If `perform` defined then associated method is called and its result returned instead of entity."] -// #[doc = -// r" For example `perform()` of structure with : `#[ perform( fn after1() -> &str > )` returns `&str`."] -// #[doc = r""] #[inline(always)] pub fn perform(self) -> < Definition :: -// Types as former :: FormerDefinitionTypes > :: Formed -// { let result = self.form() ; return result ; } #[doc = r""] -// #[doc = -// r" Begin the process of forming. Expects context of forming to return it after forming."] -// #[doc = r""] #[inline(always)] pub fn -// begin(mut storage : core :: option :: Option < < Definition :: Types as -// former :: FormerDefinitionTypes > :: Storage >, context : core :: option -// :: Option < < Definition :: Types as former :: FormerDefinitionTypes > :: -// Context >, on_end : < Definition as former :: FormerDefinition > :: End,) -// -> Self -// { -// if storage.is_none() -// { storage = Some(:: core :: default :: Default :: default()) ; } Self -// { -// storage : storage.unwrap(), context : context, on_end : :: core :: -// option :: Option :: Some(on_end), -// } -// } #[doc = r""] -// #[doc = -// r" End the process of forming returning original context of forming."] -// #[doc = r""] #[inline(always)] pub fn form(self) -> < Definition :: Types -// as former :: FormerDefinitionTypes > :: Formed { self.end() } #[doc = r""] -// #[doc = -// r" End the process of forming returning original context of forming."] -// #[doc = r""] #[inline(always)] pub fn end(mut self) -> < Definition :: -// Types as former :: FormerDefinitionTypes > :: Formed -// { -// let on_end = self.on_end.take().unwrap() ; let context = -// self.context.take() ; former :: FormingEnd :: < Definition :: Types > -// :: call(& on_end, self.storage, context) -// } #[doc = "Setter for the 'int_1' field."] #[inline] pub fn int_1 < Src > -// (mut self, src : Src) -> Self where Src : :: core :: convert :: Into < i32 -// >, -// { -// debug_assert! (self.storage.int_1.is_none()) ; self.storage.int_1 = :: -// core :: option :: Option :: Some(src.into()) ; self -// } #[doc = "Setter for the 'string_1' field."] #[inline] pub fn string_1 < -// Src > (mut self, src : Src) -> Self where Src : :: core :: convert :: Into -// < String >, -// { -// debug_assert! (self.storage.string_1.is_none()) ; -// self.storage.string_1 = :: core :: option :: Option :: -// Some(src.into()) ; self -// } #[doc = "Setter for the 'int_optional_1' field."] #[inline] pub fn -// int_optional_1 < Src > (mut self, src : Src) -> Self where Src : :: core -// :: convert :: Into < i32 >, -// { -// debug_assert! (self.storage.int_optional_1.is_none()) ; -// self.storage.int_optional_1 = :: core :: option :: Option :: -// Some(src.into()) ; self -// } #[doc = "Setter for the 'string_optional_1' field."] #[inline] pub fn -// string_optional_1 < Src > (mut self, src : Src) -> Self where Src : :: -// core :: convert :: Into < String >, -// { -// debug_assert! (self.storage.string_optional_1.is_none()) ; -// self.storage.string_optional_1 = :: core :: option :: Option :: -// Some(src.into()) ; self -// } -// } #[automatically_derived] impl Struct1Former < > -// { -// #[doc = r""] -// #[doc = r" Construct new instance of former with default parameters."] -// #[doc = r""] #[inline(always)] pub fn new() -> Self -// { Self :: begin(None, None, former :: ReturnPreformed,) } -// } -// -// impl < Definition > Struct1FormerDefinition < Definition > -// where -// Definition : former :: FormerDefinition, -// < -// Definition :: Types as former :: FormerDefinitionTypes > :: Storage : former :: StoragePerform, Definition :: Types : former :: FormerDefinitionTypes < Storage = Struct1FormerStorage, -// Formed = Struct1 >, -// { -// pub fn preform(self) -> < Definition :: Types as former :: FormerDefinitionTypes > :: Formed -// { -// former :: StoragePerform :: preform(self.storage) -// } -// } - // = end of generated // include!( "./only_test/primitives.rs" ); diff --git a/module/core/former/tests/inc/former_tests/a_primitives_manual.rs b/module/core/former/tests/inc/former_tests/a_primitives_manual.rs index bbf63a4a65..8cc4d60574 100644 --- a/module/core/former/tests/inc/former_tests/a_primitives_manual.rs +++ b/module/core/former/tests/inc/former_tests/a_primitives_manual.rs @@ -43,8 +43,26 @@ for Struct1FormerDefinition< Context, Formed, End > } } +#[ derive( Debug ) ] +pub struct Struct1FormerDefinitionTypes< Context = (), Formed = Struct1 > +{ + _phantom : core::marker::PhantomData< ( Context, Formed ) >, +} + +impl< Context, Formed > Default +for Struct1FormerDefinitionTypes< Context, Formed > +{ + fn default() -> Self + { + Self + { + _phantom : core::marker::PhantomData, + } + } +} + impl< Context, Formed > former::FormerDefinitionTypes -for Struct1FormerDefinition< Context, Formed, former::NoEnd > +for Struct1FormerDefinitionTypes< Context, Formed > { type Storage = Struct1FormerStorage; type Formed = Formed; @@ -54,9 +72,9 @@ for Struct1FormerDefinition< Context, Formed, former::NoEnd > impl< Context, Formed, End > former::FormerDefinition for Struct1FormerDefinition< Context, Formed, End > where - End : former::FormingEnd< Struct1FormerDefinition< Context, Formed, former::NoEnd > >, + End : former::FormingEnd< Struct1FormerDefinitionTypes< Context, Formed > >, { - type Types = Struct1FormerDefinition< Context, Formed, former::NoEnd >; + type Types = Struct1FormerDefinitionTypes< Context, Formed >; type End = End; } diff --git a/module/core/former/tests/inc/former_tests/container_former_common.rs b/module/core/former/tests/inc/former_tests/container_former_common.rs index 954e887892..a4bb1ca1e1 100644 --- a/module/core/former/tests/inc/former_tests/container_former_common.rs +++ b/module/core/former/tests/inc/former_tests/container_former_common.rs @@ -250,8 +250,6 @@ fn custom_definition_custom_end() type End = former::FormingEndWrapper< < Self as former::FormerDefinition >::Types >; } - // - fn return_13( _storage : Vec< String >, _context : Option< () > ) -> i32 { 13 diff --git a/module/core/former/tests/inc/former_tests/only_test/primitives.rs b/module/core/former/tests/inc/former_tests/only_test/primitives.rs index 3ea0a9aafc..973bc04dd8 100644 --- a/module/core/former/tests/inc/former_tests/only_test/primitives.rs +++ b/module/core/former/tests/inc/former_tests/only_test/primitives.rs @@ -86,17 +86,18 @@ tests_impls! // let _default = Struct1FormerDefinition::default(); // why does not work? // definition types exists and has Formed - let got = < Struct1FormerDefinition< (), Struct1, former::NoEnd > as the_module::FormerDefinitionTypes >::Formed::former().form(); - // let got = < Struct1FormerDefinition<> as the_module::FormerDefinitionTypes >::Formed::former().form(); + let got = < Struct1FormerDefinitionTypes< (), Struct1 > as the_module::FormerDefinitionTypes >::Formed::former().form(); let exp = Struct1::former().form(); a_id!( got, exp ); - // xxx : uncomment - // < Context = (), Formed = Struct1, End = former::ReturnPreformed > + // definition types exists and has Formed + let got = < Struct1FormerDefinitionTypes as the_module::FormerDefinitionTypes >::Formed::former().form(); + let exp = Struct1::former().form(); + a_id!( got, exp ); // definition types exists and has Storage use former::StoragePerform; - let got = < Struct1FormerDefinition< (), Struct1, former::NoEnd > as the_module::FormerDefinitionTypes >::Storage::preform( Struct1::former().storage ); + let got = < Struct1FormerDefinitionTypes as the_module::FormerDefinitionTypes >::Storage::preform( Struct1::former().storage ); let exp = Struct1::former().form(); a_id!( got, exp ); diff --git a/module/core/former_meta/src/derive/former.rs b/module/core/former_meta/src/derive/former.rs index 98befec60b..07d43f1b2b 100644 --- a/module/core/former_meta/src/derive/former.rs +++ b/module/core/former_meta/src/derive/former.rs @@ -743,8 +743,8 @@ For specifying custom default value use attribute `default`. For example: pub fn performer< 'a > ( _name_ident : &syn::Ident, - former_definition_name_ident : &syn::Ident, - generics_ty : &syn::TypeGenerics< '_ >, + _former_definition_name_ident : &syn::Ident, + _generics_ty : &syn::TypeGenerics< '_ >, attrs : impl Iterator< Item = &'a syn::Attribute >, ) -> Result< ( TokenStream, TokenStream, TokenStream ) > @@ -829,6 +829,8 @@ pub fn former( input : proc_macro::TokenStream ) -> Result< TokenStream > let former_storage_name_ident = syn::Ident::new( &former_storage_name, name_ident.span() ); let former_definition_name = format!( "{}FormerDefinition", name_ident ); let former_definition_name_ident = syn::Ident::new( &former_definition_name, name_ident.span() ); + let former_definition_types_name = format!( "{}FormerDefinitionTypes", name_ident ); + let former_definition_types_name_ident = syn::Ident::new( &former_definition_types_name, name_ident.span() ); /* generic parameters */ @@ -848,13 +850,11 @@ pub fn former( input : proc_macro::TokenStream ) -> Result< TokenStream > // add embedded generic parameters let mut extra_generics : syn::Generics = parse_quote! { - // < __FormerContext = #name_ident #generics_ty, __FormerEnd = former::ReturnPreformed > < Definition = #former_definition_name_ident #generics_ty > }; extra_generics.where_clause = parse_quote! { where - // __FormerEnd : former::FormingEnd< #former_definition_name_ident #generics_ty >, Definition : former::FormerDefinition, < Definition::Types as former::FormerDefinitionTypes >::Storage : former::StoragePerform, Definition::Types : former::FormerDefinitionTypes< Storage = #former_storage_name_ident #generics_ty >, @@ -945,8 +945,23 @@ pub fn former( input : proc_macro::TokenStream ) -> Result< TokenStream > // = definition - // #[ derive( Debug, Default ) ] - // pub struct #former_definition_name_ident #generics_ty; + #[ derive( Debug ) ] + pub struct #former_definition_types_name_ident< Context = (), Formed = #name_ident #generics_ty > + { + _phantom : core::marker::PhantomData< ( Context, Formed ) >, + } + + impl< Context, Formed > Default + for #former_definition_types_name_ident< Context, Formed > + { + fn default() -> Self + { + Self + { + _phantom : core::marker::PhantomData, + } + } + } #[ derive( Debug ) ] pub struct #former_definition_name_ident< Context = (), Formed = #name_ident #generics_ty, End = former::ReturnPreformed > @@ -969,7 +984,7 @@ pub fn former( input : proc_macro::TokenStream ) -> Result< TokenStream > } impl< Context, Formed > former::FormerDefinitionTypes - for #former_definition_name_ident< Context, Formed, former::NoEnd > + for #former_definition_types_name_ident< Context, Formed > { type Storage = #former_storage_name_ident #generics_ty; type Formed = Formed; @@ -979,9 +994,9 @@ pub fn former( input : proc_macro::TokenStream ) -> Result< TokenStream > impl< Context, Formed, End > former::FormerDefinition for #former_definition_name_ident< Context, Formed, End > where - End : former::FormingEnd< #former_definition_name_ident< Context, Formed, former::NoEnd > >, + End : former::FormingEnd< #former_definition_types_name_ident< Context, Formed > >, { - type Types = #former_definition_name_ident< Context, Formed, former::NoEnd >; + type Types = #former_definition_types_name_ident< Context, Formed >; type End = End; }