From 11ef4a418c58a431b45f3eec6096d916c5691533 Mon Sep 17 00:00:00 2001 From: wandalen Date: Sat, 13 Apr 2024 14:18:44 +0300 Subject: [PATCH] former : experimenting --- .../tests/inc/former_tests/string_slice.rs | 4 +- .../inc/former_tests/string_slice_manual.rs | 242 ++++++++++++++++++ module/core/former/tests/inc/mod.rs | 2 +- 3 files changed, 245 insertions(+), 3 deletions(-) diff --git a/module/core/former/tests/inc/former_tests/string_slice.rs b/module/core/former/tests/inc/former_tests/string_slice.rs index 729e995a4f..abd69806c1 100644 --- a/module/core/former/tests/inc/former_tests/string_slice.rs +++ b/module/core/former/tests/inc/former_tests/string_slice.rs @@ -1,7 +1,7 @@ use super::*; -#[ derive( Debug, PartialEq, the_module::Former ) ] -// #[ derive( Debug, PartialEq, the_module::Former ) ] #[ debug ] +// #[ derive( Debug, PartialEq, the_module::Former ) ] +#[ derive( Debug, PartialEq, the_module::Former ) ] #[ debug ] pub struct Struct1< 'a > { pub string_slice_1 : &'a str, diff --git a/module/core/former/tests/inc/former_tests/string_slice_manual.rs b/module/core/former/tests/inc/former_tests/string_slice_manual.rs index 96ba351334..10643b0701 100644 --- a/module/core/former/tests/inc/former_tests/string_slice_manual.rs +++ b/module/core/former/tests/inc/former_tests/string_slice_manual.rs @@ -9,7 +9,249 @@ pub struct Struct1< 'a > // === begin of generated +#[ automatically_derived ] +impl< 'a > Struct1< 'a > +{ + #[ 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< 'a, > + { + Struct1Former::< 'a, >::new( former::ReturnPreformed ) + } +} + +#[ derive( Debug ) ] +pub struct Struct1FormerDefinitionTypes< Context = (), Formed = Struct1< 'a > > +{ + _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 Struct1FormerDefinitionTypes< Context, Formed > +{ + type Storage = Struct1FormerStorage< 'a >; + type Formed = Formed; + type Context = Context; +} + +#[ derive( Debug ) ] +pub struct Struct1FormerDefinition< Context = (), Formed = Struct1< 'a >, 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, End > former::FormerDefinition for Struct1FormerDefinition< Context, Formed, End > +where End : former::FormingEnd< Struct1FormerDefinitionTypes< Context, Formed > > +{ + type Types = Struct1FormerDefinitionTypes< Context, Formed >; + type End = End; +} + +pub type Struct1FormerWithClosure< Context, Formed > = + Struct1FormerDefinition< Context, Formed, former::FormingEndClosure< Struct1FormerDefinitionTypes< Context, Formed > > >; + +#[ doc = "Container of a corresponding former." ] +pub struct Struct1FormerStorage< 'a > +{ + #[ doc = r" A field" ] + pub string_slice_1 : ::core::option::Option< &'a str >, +} + +impl< 'a > ::core::default::Default for Struct1FormerStorage< 'a > +{ + #[ inline( always ) ] + fn default() -> Self + { + Self { string_slice_1 : ::core::option::Option::None, } + } +} + +impl< 'a > former::Storage for Struct1FormerStorage< 'a > +{ + type Formed = Struct1< 'a >; +} + +impl former::StoragePreform for Struct1FormerStorage< 'a > +{ + fn preform( mut self ) -> < Self as former::Storage >::Formed + { + let string_slice_1 = if self.string_slice_1.is_some() + { + self.string_slice_1.take().unwrap() + } + else + { + { + trait MaybeDefault< T > + { + fn maybe_default( self : & Self ) -> T + { + panic!( "Field 'string_slice_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::< &'a str >).maybe_default() + } + }; + let result = Struct1< 'a > { string_slice_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< 'a, Definition = Struct1FormerDefinition< 'a > > +where Definition : former::FormerDefinition, Definition::Types : former::FormerDefinitionTypes< Storage = Struct1FormerStorage< 'a > >, +{ + 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< 'a, Definition > Struct1Former< 'a, Definition > +where Definition : former::FormerDefinition, Definition::Types : former::FormerDefinitionTypes< Storage = Struct1FormerStorage< 'a > >, +{ + #[ 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" Construct new instance of former with default parameters." ] + #[ doc = r"" ] + #[ inline( always ) ] + pub fn _new_precise( on_end : Definition::End ) -> Self + { + Self::begin( None, None, on_end ) + } + + #[ doc = r"" ] + #[ doc = r" Construct new instance of former with default parameters." ] + #[ doc = r"" ] + #[ inline( always ) ] + pub fn new< IntoEnd >( end : IntoEnd ) -> Self + where IntoEnd : Into< Definition::End >, + { + Self::begin( None, None, end, ) + } + + #[ 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_precise( + 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" Begin the process of forming. Expects context of forming to return it after forming." ] + #[ doc = r"" ] + #[ inline( always ) ] + pub fn begin< IntoEnd > + ( + mut storage : core::option::Option< < Definition::Types as former::FormerDefinitionTypes >::Storage >, + context : core::option::Option< < Definition::Types as former::FormerDefinitionTypes >::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 : context, + on_end : ::core::option::Option::Some( ::core::convert::Into::into( 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 'string_slice_1' field." ] + #[ inline ] + pub fn string_slice_1< Src >( mut self, src : Src ) -> Self + where Src : ::core::convert::Into< &'a str >, + { + debug_assert!( self.storage.string_slice_1.is_none() ); + self.storage.string_slice_1 = ::core::option::Option::Some( ::core::convert::Into::into( src ) ); + self + } +} + +impl< Definition > Struct1Former< Definition > +where Definition : former::FormerDefinition, Definition::Types : former::FormerDefinitionTypes< Storage = Struct1FormerStorage< 'a >, Formed = Struct1< 'a > >, < Definition::Types as former::FormerDefinitionTypes >::Storage : former::StoragePreform, +{ + pub fn preform( self ) -> < Definition::Types as former::FormerDefinitionTypes >::Formed + { + former::StoragePreform::preform( self.storage ) + } +} // === end of generated diff --git a/module/core/former/tests/inc/mod.rs b/module/core/former/tests/inc/mod.rs index 8e35ae8e1c..92d71d877d 100644 --- a/module/core/former/tests/inc/mod.rs +++ b/module/core/former/tests/inc/mod.rs @@ -32,7 +32,7 @@ mod former_tests mod attribute_setter; mod attribute_alias; - mod string_slice_manual; + // mod string_slice_manual; // mod string_slice; mod unsigned_primitive_types; mod default_user_type;