diff --git a/module/core/former/Readme.md b/module/core/former/Readme.md index ec0a4f16ec..5b5d19034e 100644 --- a/module/core/former/Readme.md +++ b/module/core/former/Readme.md @@ -86,9 +86,9 @@ pub struct UserProfile impl UserProfile { #[ inline( always ) ] - pub fn former() -> UserProfileFormer< UserProfile, former::ReturnFormed > + pub fn former() -> UserProfileFormer< UserProfile, former::ReturnStorage > { - UserProfileFormer::< UserProfile, former::ReturnFormed >::new() + UserProfileFormer::< UserProfile, former::ReturnStorage >::new() } } @@ -103,7 +103,7 @@ pub struct UserProfileFormerStorage pub struct UserProfileFormer < Context = UserProfile, - End = former::ReturnFormed, + End = former::ReturnStorage, > where End : former::FormingEnd< UserProfile, Context >, @@ -205,9 +205,9 @@ where } #[ inline( always ) ] - pub fn new() -> UserProfileFormer< UserProfile, former::ReturnFormed > + pub fn new() -> UserProfileFormer< UserProfile, former::ReturnStorage > { - UserProfileFormer::< UserProfile, former::ReturnFormed >::begin( None, former::ReturnFormed ) + UserProfileFormer::< UserProfile, former::ReturnStorage >::begin( None, former::ReturnStorage ) } #[ inline( always ) ] @@ -226,7 +226,7 @@ where } #[ inline( always ) ] - pub fn end( mut self ) -> Context + pub fn end( mut self ) -> Formed { let on_end = self.on_end.take().unwrap(); let context = self.context.take(); diff --git a/module/core/former/examples/former_trivial_expaned.rs b/module/core/former/examples/former_trivial_expaned.rs index 5b8d8cd8c2..08d0f0f9f3 100644 --- a/module/core/former/examples/former_trivial_expaned.rs +++ b/module/core/former/examples/former_trivial_expaned.rs @@ -38,9 +38,9 @@ fn main() impl UserProfile { #[ inline( always ) ] - pub fn former() -> UserProfileFormer< UserProfile, former::ReturnFormed > + pub fn former() -> UserProfileFormer< UserProfile, former::ReturnStorage > { - UserProfileFormer::< UserProfile, former::ReturnFormed >::new() + UserProfileFormer::< UserProfile, former::ReturnStorage >::new() } } @@ -55,7 +55,7 @@ fn main() pub struct UserProfileFormer < Context = UserProfile, - End = former::ReturnFormed, + End = former::ReturnStorage, > where End : former::FormingEnd< UserProfile, Context >, @@ -157,9 +157,9 @@ fn main() } #[ inline( always ) ] - pub fn new() -> UserProfileFormer< UserProfile, former::ReturnFormed > + pub fn new() -> UserProfileFormer< UserProfile, former::ReturnStorage > { - UserProfileFormer::< UserProfile, former::ReturnFormed >::begin( None, former::ReturnFormed ) + UserProfileFormer::< UserProfile, former::ReturnStorage >::begin( None, former::ReturnStorage ) } #[ inline( always ) ] @@ -178,7 +178,7 @@ fn main() } #[ inline( always ) ] - pub fn end( mut self ) -> Context + pub fn end( mut self ) -> Formed { let on_end = self.on_end.take().unwrap(); let context = self.context.take(); diff --git a/module/core/former/src/axiomatic.rs b/module/core/former/src/axiomatic.rs index 161a0f3ea8..4338bb73bb 100644 --- a/module/core/former/src/axiomatic.rs +++ b/module/core/former/src/axiomatic.rs @@ -6,9 +6,9 @@ /// Implementors can define how to transform or pass through the context during the forming process's completion. /// /// # Parameters -/// - `Formed`: The type of the container being processed. +/// - `Storage`: The type of the container being processed. /// - `Context`: The type of the context that might be altered or returned upon completion. -pub trait FormingEnd< Formed, Context > +pub trait FormingEnd< Storage, Context, Formed > { /// Called at the end of the subforming process to return the modified or original context. /// @@ -18,21 +18,40 @@ pub trait FormingEnd< Formed, Context > /// /// # Returns /// Returns the transformed or original context based on the implementation. - #[ allow( dead_code ) ] - fn call( &self, storage : Formed, context : core::option::Option< Context > ) -> Context; + // #[ allow( dead_code ) ] + fn call( &self, storage : Storage, context : core::option::Option< Context > ) -> Formed; } -impl< Storage, Context, F > FormingEnd< Storage, Context > for F +impl< Storage, Context, Formed, F > FormingEnd< Storage, Context, Formed > for F where - F : Fn( Storage, core::option::Option< Context > ) -> Context, + F : Fn( Storage, core::option::Option< Context > ) -> Formed, { #[ inline( always ) ] - fn call( &self, storage : Storage, context : core::option::Option< Context > ) -> Context + fn call( &self, storage : Storage, context : core::option::Option< Context > ) -> Formed { self( storage, context ) } } +/// A `FormingEnd` implementation that returns the formed container itself instead of the context. +/// +/// This struct is useful when the forming process should result in the formed container being returned directly, +/// bypassing any additional context processing. It simplifies scenarios where the formed container is the final result. +#[ derive( Debug, Default ) ] +pub struct ReturnStorage; + +impl< Storage, Formed > FormingEnd< Storage, (), Formed > +for ReturnStorage +// where + // Storage : StoragePreform<>, +{ + #[ inline( always ) ] + fn call( &self, storage : Storage, _context : core::option::Option< () > ) -> Formed + { + storage.preform() + } +} + /// A wrapper around a closure to be used as a `FormingEnd`. /// /// This struct allows for dynamic dispatch of a closure that matches the @@ -47,14 +66,14 @@ where /// * `Context` - The type of the context that may be altered or returned by the closure. /// This allows for flexible manipulation of context based on the container. #[ cfg( not( feature = "no_std" ) ) ] -pub struct FormingEndWrapper< Storage, Context > +pub struct FormingEndWrapper< Storage, Context, Formed > { - closure : Box< dyn Fn( Storage, Option< Context > ) -> Context >, + closure : Box< dyn Fn( Storage, Option< Context > ) -> Formed >, _marker : std::marker::PhantomData< Storage >, } #[ cfg( not( feature = "no_std" ) ) ] -impl< Storage, Context > FormingEndWrapper< Storage, Context > +impl< Storage, Context, Formed > FormingEndWrapper< Storage, Context, Formed > { /// Constructs a new `FormingEndWrapper` with the provided closure. /// @@ -67,7 +86,7 @@ impl< Storage, Context > FormingEndWrapper< Storage, Context > /// # Returns /// /// Returns an instance of `FormingEndWrapper` encapsulating the provided closure. - pub fn new( closure : impl Fn( Storage, Option< Context > ) -> Context + 'static ) -> Self + pub fn new( closure : impl Fn( Storage, Option< Context > ) -> Formed + 'static ) -> Self { Self { @@ -80,7 +99,7 @@ impl< Storage, Context > FormingEndWrapper< Storage, Context > #[ cfg( not( feature = "no_std" ) ) ] use std::fmt; #[ cfg( not( feature = "no_std" ) ) ] -impl< Storage, Context > fmt::Debug for FormingEndWrapper< Storage, Context > +impl< Storage, Context, Formed > fmt::Debug for FormingEndWrapper< Storage, Context, Formed > { fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result { @@ -92,96 +111,68 @@ impl< Storage, Context > fmt::Debug for FormingEndWrapper< Storage, Context > } #[ cfg( not( feature = "no_std" ) ) ] -impl< Storage, Context > FormingEnd< Storage, Context > -for FormingEndWrapper< Storage, Context > +impl< Storage, Context, Formed > FormingEnd< Storage, Context, Formed > +for FormingEndWrapper< Storage, Context, Formed > { - fn call( &self, storage : Storage, context : Option< Context > ) -> Context + fn call( &self, storage : Storage, context : Option< Context > ) -> Formed { ( self.closure )( storage, context ) } } -// /// A `FormingEnd` implementation that returns the original context without any modifications. -// /// -// /// This struct is used when no end-of-forming processing is needed, and the original context is to be returned as-is. -// #[ derive( Debug, Default ) ] -// pub struct NoEnd; // -// impl< Formed, Context > FormingEnd< Formed, Context > -// for NoEnd -// { -// #[ inline( always ) ] -// fn call( &self, _formed : Formed, context : core::option::Option< Context > ) -> Context -// { -// context.unwrap() -// } -// } -/// A `FormingEnd` implementation that returns the formed container itself instead of the context. +/// A trait for initiating a structured subforming process with contextual and intermediary storage linkage. /// -/// This struct is useful when the forming process should result in the formed container being returned directly, -/// bypassing any additional context processing. It simplifies scenarios where the formed container is the final result. -#[ derive( Debug, Default ) ] -pub struct ReturnFormed; - -impl< Storage > FormingEnd< Storage, Storage > -for ReturnFormed -{ - #[ inline( always ) ] - fn call( &self, storage : Storage, _context : core::option::Option< Storage > ) -> Storage - { - storage - } -} - -// - -/// A trait defining the initialization process for a subformer with contextual linkage. -/// -/// This trait is designed for types that need to initiate a subforming process, -/// passing themselves as the context and specifying a closure or handler (`on_end`) to be -/// called upon completion. It facilitates the construction of builder pattern chains -/// that maintain stateful context through each step of the process. +/// This trait facilitates the creation of a subformer that carries through a builder pattern chain, +/// utilizing intermediary storage for accumulating state or data before finally transforming it into +/// a `Formed` structure. It is designed for scenarios where a multi-step construction or transformation +/// process benefits from maintaining both transient state (`Storage`) and contextual information (`Context`), +/// before concluding with the generation of a final product (`Formed`). /// /// # Type Parameters /// -/// * `Formed` - Represents the type that is being constructed or transformed by the subformer. -/// * `Context` - Denotes the contextual information or the environment in which `Formed` is being formed. -/// This could be a reference to a parent builder, configuration settings, or any other -/// relevant state. +/// * `Storage` - Represents a mutable intermediary storage structure used throughout the subforming process +/// to accumulate data, state, or partial computations. This storage is internal to the +/// subformer and is eventually converted into the final `Formed` structure by the subformer, +/// not directly by implementations of this trait. +/// +/// * `Formed` - Denotes the final type that results from the subforming process. This is the intended outcome +/// of the builder chain, constructed or transformed from the `Storage` with consideration of +/// the provided `Context`. +/// +/// * `Context` - Specifies the contextual backdrop against which the subforming process unfolds. This could +/// encompass references to parent builders, configuration data, or any state influencing how +/// `Storage` transitions into `Formed`. /// -/// # Associated Types +/// # Functions /// -/// * `End` - Specifies the trait bound for the closure or handler that gets called at the completion -/// of the subforming process. This type must implement the `FormingEnd` -/// trait, which defines how the final transformation or construction of `Formed` is handled, -/// potentially using the provided `Context`. +/// * `_begin` - This function launches the subforming process, marking the start of a construction or transformation +/// sequence defined by the implementing type. It establishes the foundational `Storage` and `Context`, +/// alongside specifying an `on_end` completion handler that dictates the final conversion into `Formed`. /// +/// The `FormerBegin` trait, by decoupling `Storage` from `Formed` and introducing a contextual layer, enables +/// sophisticated and flexible construction patterns conducive to complex data transformations or object creation +/// sequences within builder patterns. +// xxx2 : change sequence pub trait FormerBegin< Storage, Formed, Context > { - /// * `End` - Specifies the trait bound for the closure or handler that gets called at the completion - /// of the subforming process. This type must implement the `FormingEnd` - /// trait, which defines how the final transformation or construction of `Formed` is handled, - /// potentially using the provided `Context`. - type End : FormingEnd< Formed, Context >; + /// * `End` - A trait bound marking the closure or handler invoked upon completing the subforming process. Implementers + /// of this trait (`End`) are tasked with applying the final transformations that transition `Storage` + /// into `Formed`, optionally utilizing `Context` to guide this transformation. It is crucial that this + /// associated type satisfies the `FormingEnd` trait, defining the precise mechanics of + /// how the subformer concludes its operation. + type End : FormingEnd< Storage, Context, Formed >; - /// Initializes the subforming process by setting the context and specifying an `on_end` completion handler. - /// - /// This function is the entry point for initiating a subforming sequence, allowing the caller - /// to establish initial contextual information and define how the process concludes. + /// Launches the subforming process with an initial storage and context, setting up an `on_end` completion handler. /// /// # Parameters /// - /// * `context` - An optional parameter providing initial context for the subforming process. This - /// might include configuration data, references to parent structures, or any state - /// relevant to the formation of `Formed`. - /// - /// * `on_end` - A closure or handler of type `Self::End` that is invoked at the completion of - /// the subforming process. This handler is responsible for applying any final transformations - /// to `Formed` and potentially utilizing `Context` to influence the outcome. - /// + /// * `storage` - An optional initial state for the intermediary storage structure. + /// * `context` - An optional initial setting providing contextual information for the subforming process. + /// * `on_end` - A completion handler responsible for transforming the accumulated `Storage` into the final `Formed` structure. fn _begin ( storage : core::option::Option< Storage >, diff --git a/module/core/former/src/hash_map.rs b/module/core/former/src/hash_map.rs index 14c0831370..9524fc0384 100644 --- a/module/core/former/src/hash_map.rs +++ b/module/core/former/src/hash_map.rs @@ -22,20 +22,11 @@ where /// Return former. #[ inline( always ) ] fn former( self ) - -> HashMapSubformer< K, E, Self, Self, impl FormingEnd< Self, Self > > + -> HashMapSubformer< K, E, (), Self, impl FormingEnd< Self, (), Self > > { - HashMapSubformer::begin( Some( self ), None, ReturnFormed ) + HashMapSubformer::begin( Some( self ), None, ReturnStorage ) } - // /// Return former with a custom context. - // #[ inline( always ) ] - // fn former_begin< Context, End >( self, context : Context, end : End ) - // -> HashMapSubformer< K, E, Self, Context, End > - // where End : FormingEnd< Self, Context > - // { - // HashMapSubformer::begin( Some( self ), Some( context ), end ) - // } - } impl< K, E > HashMapLike< K, E > for HashMap< K, E > @@ -91,11 +82,11 @@ where /// ``` #[ derive( Debug, Default ) ] -pub struct HashMapSubformer< K, E, Formed, Context, End > +pub struct HashMapSubformer< K, E, Context, Formed, End > where K : core::cmp::Eq + core::hash::Hash, Formed : HashMapLike< K, E > + core::default::Default, - End : FormingEnd< Formed, Context >, + End : FormingEnd< Formed, Context, Formed >, { formed : core::option::Option< Formed >, context : core::option::Option< Context >, @@ -104,12 +95,12 @@ where _k_phantom : core::marker::PhantomData< K >, } -impl< K, E, Formed, Context, End > -HashMapSubformer< K, E, Formed, Context, End > +impl< K, E, Context, Formed, End > +HashMapSubformer< K, E, Context, Formed, End > where K : core::cmp::Eq + core::hash::Hash, Formed : HashMapLike< K, E > + core::default::Default, - End : FormingEnd< Formed, Context >, + End : FormingEnd< Formed, Context, Formed >, { /// Form current former into target structure. @@ -150,7 +141,7 @@ where /// Return context of your struct moving formed there. Should be called after configuring the formed. #[ inline( always ) ] - pub fn end( mut self ) -> Context + pub fn end( mut self ) -> Formed { let on_end = self.on_end.take().unwrap(); let context = self.context.take(); @@ -168,12 +159,8 @@ where } -// impl< E, Formed > VectorSubformer< E, Formed, Formed, crate::ReturnFormed > -// where -// Formed : VectorLike< E > + core::default::Default, - impl< K, E, Formed > -HashMapSubformer< K, E, Formed, Formed, crate::ReturnFormed > +HashMapSubformer< K, E, (), Formed, crate::ReturnStorage > where K : core::cmp::Eq + core::hash::Hash, Formed : HashMapLike< K, E > + core::default::Default, @@ -187,18 +174,18 @@ where ( None, None, - crate::ReturnFormed, + crate::ReturnStorage, ) } } -impl< K, E, Formed, Context, End > -HashMapSubformer< K, E, Formed, Context, End > +impl< K, E, Context, Formed, End > +HashMapSubformer< K, E, Context, Formed, End > where K : core::cmp::Eq + core::hash::Hash, Formed : HashMapLike< K, E > + core::default::Default, - End : FormingEnd< Formed, Context >, + End : FormingEnd< Formed, Context, Formed >, { /// Inserts a key-value pair into the formed. If the formed doesn't exist, it is created. diff --git a/module/core/former/src/hash_set.rs b/module/core/former/src/hash_set.rs index 20a521d3f6..989d9a0a12 100644 --- a/module/core/former/src/hash_set.rs +++ b/module/core/former/src/hash_set.rs @@ -65,12 +65,13 @@ where /// # } /// ``` +// xxx2 : change sequence #[ derive( Debug, Default ) ] pub struct HashSetSubformer< E, Formed, Context, ContainerEnd > where E : core::cmp::Eq + core::hash::Hash, Formed : HashSetLike< E > + core::default::Default, - ContainerEnd : FormingEnd< Formed, Context >, + ContainerEnd : FormingEnd< Formed, Context, Formed >, { formed : core::option::Option< Formed >, context : core::option::Option< Context >, @@ -83,12 +84,12 @@ HashSetSubformer< E, Formed, Context, ContainerEnd > where E : core::cmp::Eq + core::hash::Hash, Formed : HashSetLike< E > + core::default::Default, - ContainerEnd : FormingEnd< Formed, Context >, + ContainerEnd : FormingEnd< Formed, Context, Formed >, { /// Form current former into target structure. #[ inline( always ) ] - pub fn form( mut self ) -> Formed + pub fn preform( mut self ) -> Formed { let formed = if self.formed.is_some() { @@ -140,11 +141,27 @@ where /// constructed formed or a context that incorporates the formed. /// #[ inline( always ) ] - pub fn end( mut self ) -> Context + pub fn form( self ) -> Formed + { + self.end() + } + + /// Finalizes the building process and returns the constructed formed or a context. + /// + /// This method concludes the building process by applying the `on_end` handler to transform + /// the formed or incorporate it into a given context. It's typically called at the end + /// of the builder chain to retrieve the final product of the building process. + /// + /// # Returns + /// Depending on the `on_end` handler's implementation, this method can return either the + /// constructed formed or a context that incorporates the formed. + /// + #[ inline( always ) ] + pub fn end( mut self ) -> Formed { let on_end = self.on_end.take().unwrap(); let context = self.context.take(); - let formed = self.form(); + let formed = self.preform(); on_end.call( formed, context ) } @@ -169,17 +186,11 @@ where } -// impl< E, Formed > VectorSubformer< E, Formed, Formed, crate::ReturnFormed > -// where -// Formed : VectorLike< E > + core::default::Default, -// { - impl< E, Formed > -HashSetSubformer< E, Formed, Formed, crate::ReturnFormed > +HashSetSubformer< E, Formed, (), crate::ReturnStorage > where E : core::cmp::Eq + core::hash::Hash, Formed : HashSetLike< E > + core::default::Default, - // ContainerEnd : FormingEnd< Formed, Context >, { /// Initializes a new instance of the builder with default settings. @@ -197,7 +208,7 @@ where ( None, None, - crate::ReturnFormed, + crate::ReturnStorage, ) } @@ -208,7 +219,7 @@ HashSetSubformer< E, Formed, Context, ContainerEnd > where E : core::cmp::Eq + core::hash::Hash, Formed : HashSetLike< E > + core::default::Default, - ContainerEnd : FormingEnd< Formed, Context >, + ContainerEnd : FormingEnd< Formed, Context, Formed >, { /// Inserts an element into the set, possibly replacing an existing element. diff --git a/module/core/former/src/vector.rs b/module/core/former/src/vector.rs index 46426d8733..d01c6c9011 100644 --- a/module/core/former/src/vector.rs +++ b/module/core/former/src/vector.rs @@ -46,11 +46,12 @@ impl< E > VectorLike< E > for Vec< E > /// assert_eq!( instance, StructWithVec { vec: vec![ "apple", "banana" ] } ); ///``` /// +// xxx2 : change sequence of parameters #[ derive( Debug, Default ) ] pub struct VectorSubformer< E, Formed, Context, ContainerEnd > where Formed : VectorLike< E > + core::default::Default, - ContainerEnd : FormingEnd< Formed, Context >, + ContainerEnd : FormingEnd< Formed, Context, Formed >, { formed : core::option::Option< Formed >, context : core::option::Option< Context >, @@ -61,7 +62,7 @@ where impl< E, Formed, Context, ContainerEnd > VectorSubformer< E, Formed, Context, ContainerEnd > where Formed : VectorLike< E > + core::default::Default, - ContainerEnd : FormingEnd< Formed, Context >, + ContainerEnd : FormingEnd< Formed, Context, Formed >, { /// Form current former into target structure. @@ -80,23 +81,6 @@ where formed } - // /// Initializes a new `VectorSubformer` instance, starting with an empty formed. - // /// This function serves as the entry point for the builder pattern. - // /// - // /// # Returns - // /// A new instance of `VectorSubformer` with an empty internal formed. - // /// - // #[ inline( always ) ] - // pub fn new() -> VectorSubformer< E, Formed, Formed, impl FormingEnd< Formed, Formed > > - // { - // VectorSubformer::begin - // ( - // None, - // None, - // crate::ReturnFormed, - // ) - // } - /// Begins the building process, optionally initializing with a context and formed. #[ inline( always ) ] pub fn begin @@ -117,7 +101,7 @@ where /// Finalizes the building process, returning the formed or a context incorporating it. #[ inline( always ) ] - pub fn end( mut self ) -> Context + pub fn end( mut self ) -> Formed { let on_end = self.on_end.take().unwrap(); let context = self.context.take(); @@ -135,7 +119,7 @@ where } -impl< E, Formed > VectorSubformer< E, Formed, Formed, crate::ReturnFormed > +impl< E, Formed > VectorSubformer< E, Formed, (), crate::ReturnStorage > where Formed : VectorLike< E > + core::default::Default, { @@ -153,7 +137,7 @@ where ( None, None, - crate::ReturnFormed, + crate::ReturnStorage, ) } @@ -162,7 +146,7 @@ where impl< E, Formed, Context, ContainerEnd > VectorSubformer< E, Formed, Context, ContainerEnd > where Formed : VectorLike< E > + core::default::Default, - ContainerEnd : FormingEnd< Formed, Context >, + ContainerEnd : FormingEnd< Formed, Context, Formed >, { /// Appends an element to the end of the formed, expanding the internal collection. @@ -188,7 +172,7 @@ where impl< E, Formed, Context, End > FormerBegin< Formed, Formed, Context > for VectorSubformer< E, Formed, Context, End > where - End : FormingEnd< Formed, Context >, + End : FormingEnd< Formed, Context, Formed >, Formed : VectorLike< E > + Default, { type End = End; diff --git a/module/core/former/tests/inc/former_tests/a_containers_with_runtime_manual.rs b/module/core/former/tests/inc/former_tests/a_containers_with_runtime_manual.rs index 87a3bec1eb..332b19f523 100644 --- a/module/core/former/tests/inc/former_tests/a_containers_with_runtime_manual.rs +++ b/module/core/former/tests/inc/former_tests/a_containers_with_runtime_manual.rs @@ -13,9 +13,9 @@ pub struct Struct1 impl Struct1 { - pub fn former() -> Struct1Former< Struct1, the_module::ReturnFormed > + pub fn former() -> Struct1Former< Struct1, the_module::ReturnStorage > { - Struct1Former::< Struct1, the_module::ReturnFormed >::new() + Struct1Former::< Struct1, the_module::ReturnStorage >::new() } } @@ -48,7 +48,7 @@ impl Default for Struct1FormerStorage pub struct Struct1Former < Context = Struct1, - End = the_module::ReturnFormed, + End = the_module::ReturnStorage, > where End : the_module::FormingEnd< Struct1, Context >, @@ -113,16 +113,6 @@ where return result; } - // #[ inline( always ) ] - // pub fn new() -> Struct1Former - // { - // Struct1Former:: - // < - // Struct1, - // the_module::ReturnFormed, - // >::begin(None, the_module::ReturnFormed) - // } - #[ inline( always ) ] pub fn begin ( @@ -144,7 +134,7 @@ where } #[ inline( always ) ] - pub fn end( mut self ) -> Context + pub fn end( mut self ) -> Formed { let on_end = self.on_end.take().unwrap(); let context = self.context.take(); @@ -160,7 +150,8 @@ where < Vec< String >, Vec< String >, - Self, End = former::FormingEndWrapper< Vec< String >, Self >, + Self, + End = former::FormingEndWrapper< Vec< String >, Self >, >, { let on_end = | formed : Vec< String >, super_former : ::core::option::Option< Self > | -> Self @@ -252,23 +243,19 @@ where // where // End: the_module::FormingEnd, -impl Struct1Former< Struct1, the_module::ReturnFormed > +impl Struct1Former< Struct1, the_module::ReturnStorage > { #[ inline( always ) ] pub fn new() -> Self { - Self::begin( None, None, the_module::ReturnFormed ) + Self::begin( None, None, the_module::ReturnStorage ) } } // -// impl< Context, End > Struct1Former< Context, End > -// where -// End : the_module::FormingEnd< Struct1, Context >, - impl< Context, End > former::FormerBegin< Struct1FormerStorage, Struct1, Context > for Struct1Former< Context, End > where @@ -279,7 +266,7 @@ where #[ inline( always ) ] fn _begin ( - storage : core::option::Option< Struct1FormerStorage >, /* xxx2 : that should be storage */ + storage : core::option::Option< Struct1FormerStorage >, context : core::option::Option< Context >, on_end : End, ) -> Self diff --git a/module/core/former/tests/inc/former_tests/a_containers_without_runtime_manual.rs b/module/core/former/tests/inc/former_tests/a_containers_without_runtime_manual.rs index 9bea46cff7..fa6236f5d1 100644 --- a/module/core/former/tests/inc/former_tests/a_containers_without_runtime_manual.rs +++ b/module/core/former/tests/inc/former_tests/a_containers_without_runtime_manual.rs @@ -13,9 +13,9 @@ pub struct Struct1 impl Struct1 { - pub fn former() -> Struct1Former< Struct1, the_module::ReturnFormed > + pub fn former() -> Struct1Former< Struct1, the_module::ReturnStorage > { - Struct1Former::< Struct1, the_module::ReturnFormed >::new() + Struct1Former::< Struct1, the_module::ReturnStorage >::new() } } @@ -48,7 +48,7 @@ impl Default for Struct1FormerStorage pub struct Struct1Former < __FormerContext = Struct1, - __FormerEnd = the_module::ReturnFormed, + __FormerEnd = the_module::ReturnStorage, > where __FormerEnd : the_module::FormingEnd< Struct1, __FormerContext >, @@ -114,13 +114,13 @@ where } #[ inline( always ) ] - pub fn new() -> Struct1Former + pub fn new() -> Struct1Former { Struct1Former:: < Struct1, - the_module::ReturnFormed, - >::begin(None, the_module::ReturnFormed) + the_module::ReturnStorage, + >::begin(None, the_module::ReturnStorage) } #[ inline( always ) ] 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 0c5a011178..5461d21cff 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 @@ -15,9 +15,9 @@ pub struct Struct1 // generated by former impl Struct1 { - pub fn former() -> Struct1Former< Struct1, the_module::ReturnFormed > + pub fn former() -> Struct1Former< Struct1, the_module::ReturnStorage > { - Struct1Former::< Struct1, the_module::ReturnFormed >::new() + Struct1Former::< Struct1, the_module::ReturnStorage >::new() } } @@ -54,7 +54,7 @@ impl Default for Struct1FormerStorage pub struct Struct1Former < __FormerContext = Struct1, - __FormerEnd = the_module::ReturnFormed, + __FormerEnd = the_module::ReturnStorage, > where __FormerEnd : the_module::FormingEnd< Struct1, __FormerContext >, @@ -128,13 +128,13 @@ where } #[ inline( always ) ] - pub fn new() -> Struct1Former + pub fn new() -> Struct1Former { Struct1Former:: < Struct1, - the_module::ReturnFormed, - >::begin(None, the_module::ReturnFormed) + the_module::ReturnStorage, + >::begin(None, the_module::ReturnStorage) } #[ inline( always ) ] diff --git a/module/core/former/tests/inc/former_tests/parametrized_struct_manual.rs b/module/core/former/tests/inc/former_tests/parametrized_struct_manual.rs index ae57ca7e08..1e7abc8099 100644 --- a/module/core/former/tests/inc/former_tests/parametrized_struct_manual.rs +++ b/module/core/former/tests/inc/former_tests/parametrized_struct_manual.rs @@ -73,7 +73,7 @@ where // generated by former // #[ derive( Debug, Default ) ] -pub struct CommandFormer< K, Context = Command< K >, End = the_module::ReturnFormed > +pub struct CommandFormer< K, Context = Command< K >, End = the_module::ReturnStorage > where K : core::hash::Hash + std::cmp::Eq, End : the_module::FormingEnd< Command< K >, Context >, @@ -135,7 +135,7 @@ where ( None, None, - the_module::ReturnFormed, + the_module::ReturnStorage, ) } @@ -159,7 +159,7 @@ where /// Return former of your struct moving container there. Should be called after configuring the container. #[ inline( always ) ] - pub fn end( mut self ) -> Context + pub fn end( mut self ) -> Formed { let on_end = self.on_end.take().unwrap(); let context = self.context.take(); diff --git a/module/core/former/tests/inc/former_tests/subformer_basic_manual.rs b/module/core/former/tests/inc/former_tests/subformer_basic_manual.rs index cbb94f6e73..d9332e0d82 100644 --- a/module/core/former/tests/inc/former_tests/subformer_basic_manual.rs +++ b/module/core/former/tests/inc/former_tests/subformer_basic_manual.rs @@ -93,7 +93,7 @@ where // generated by former // #[ derive( Debug, Default ) ] -pub struct CommandFormer< K, Context = Command< K >, End = the_module::ReturnFormed > +pub struct CommandFormer< K, Context = Command< K >, End = the_module::ReturnStorage > where K : core::hash::Hash + std::cmp::Eq, End : the_module::FormingEnd< Command< K >, Context >, @@ -166,7 +166,7 @@ where ( None, None, - the_module::ReturnFormed, + the_module::ReturnStorage, ) } @@ -190,7 +190,7 @@ where /// Return former of your struct moving container there. Should be called after configuring the container. #[ inline( always ) ] - pub fn end( mut self ) -> Context + pub fn end( mut self ) -> Formed { let on_end = self.on_end.take().unwrap(); let context = self.context.take(); @@ -301,7 +301,7 @@ where // generated by former // #[ derive( Debug, Default ) ] -pub struct AggregatorFormer< K, Context = Aggregator< K >, End = the_module::ReturnFormed > +pub struct AggregatorFormer< K, Context = Aggregator< K >, End = the_module::ReturnStorage > where K : core::hash::Hash + std::cmp::Eq, End : the_module::FormingEnd< Aggregator< K >, Context >, @@ -363,7 +363,7 @@ where AggregatorFormer::< K >::begin ( None, - the_module::ReturnFormed, + the_module::ReturnStorage, ) } @@ -385,7 +385,7 @@ where /// Return former of your struct moving container there. Should be called after configuring the container. #[ inline( always ) ] - pub fn end( mut self ) -> Context + pub fn end( mut self ) -> Formed { let on_end = self.on_end.take().unwrap(); let context = self.context.take(); diff --git a/module/core/former/tests/inc/former_tests/subformer_shortcut.rs b/module/core/former/tests/inc/former_tests/subformer_shortcut.rs index bf8c825690..003ff53e29 100644 --- a/module/core/former/tests/inc/former_tests/subformer_shortcut.rs +++ b/module/core/former/tests/inc/former_tests/subformer_shortcut.rs @@ -36,7 +36,7 @@ where #[ inline( always ) ] fn _begin ( - storage : core::option::Option< TemplateParameterDescriptorFormerStorage >, /* xxx2 : that should be storage */ + storage : core::option::Option< TemplateParameterDescriptorFormerStorage >, context : core::option::Option< Context >, on_end : End, ) -> Self diff --git a/module/core/former/tests/inc/only_test/containers_with_runtime.rs b/module/core/former/tests/inc/only_test/containers_with_runtime.rs index ceeb84abaa..02986f54d0 100644 --- a/module/core/former/tests/inc/only_test/containers_with_runtime.rs +++ b/module/core/former/tests/inc/only_test/containers_with_runtime.rs @@ -18,8 +18,8 @@ tests_impls_optional! a_id!( former.storage.hashmap_strings_1, None ); a_id!( former.storage.hashset_strings_1, None ); a_id!( former.context, None ); - a_id!( print!( "{:?}", former.on_end ), print!( "{:?}", Some( the_module::ReturnFormed ) ) ); - let former2 = Struct1Former::< Struct1, the_module::ReturnFormed >::new(); + a_id!( print!( "{:?}", former.on_end ), print!( "{:?}", Some( the_module::ReturnStorage ) ) ); + let former2 = Struct1Former::< Struct1, the_module::ReturnStorage >::new(); a_id!( std::mem::size_of_val( &former ), std::mem::size_of_val( &former2 ) ); let command = Struct1::former().form(); diff --git a/module/core/former/tests/inc/only_test/containers_without_runtime.rs b/module/core/former/tests/inc/only_test/containers_without_runtime.rs index 8141ddc9fd..3ffdb7dc94 100644 --- a/module/core/former/tests/inc/only_test/containers_without_runtime.rs +++ b/module/core/former/tests/inc/only_test/containers_without_runtime.rs @@ -18,8 +18,8 @@ tests_impls! a_id!( former.storage.hashmap_strings_1, None ); a_id!( former.storage.hashset_strings_1, None ); a_id!( former.context, None ); - a_id!( print!( "{:?}", former.on_end ), print!( "{:?}", Some( the_module::ReturnFormed ) ) ); - let former2 = Struct1Former::< Struct1, the_module::ReturnFormed >::new(); + a_id!( print!( "{:?}", former.on_end ), print!( "{:?}", Some( the_module::ReturnStorage ) ) ); + let former2 = Struct1Former::< Struct1, the_module::ReturnStorage >::new(); a_id!( std::mem::size_of_val( &former ), std::mem::size_of_val( &former2 ) ); let command = Struct1::former().form(); diff --git a/module/core/former/tests/inc/only_test/primitives.rs b/module/core/former/tests/inc/only_test/primitives.rs index 728105b6ad..1f82d9c424 100644 --- a/module/core/former/tests/inc/only_test/primitives.rs +++ b/module/core/former/tests/inc/only_test/primitives.rs @@ -23,8 +23,8 @@ tests_impls! a_id!( former.storage.int_optional_1, None ); a_id!( former.storage.string_optional_1, None ); a_id!( former.context, None ); - a_id!( print!( "{:?}", former.on_end ), print!( "{:?}", Some( the_module::ReturnFormed ) ) ); - let former2 = Struct1Former::< Struct1, the_module::ReturnFormed >::new(); + a_id!( print!( "{:?}", former.on_end ), print!( "{:?}", Some( the_module::ReturnStorage ) ) ); + let former2 = Struct1Former::< Struct1, the_module::ReturnStorage >::new(); a_id!( std::mem::size_of_val( &former ), std::mem::size_of_val( &former2 ) ); let command = Struct1::former().form(); diff --git a/module/core/former_meta/src/derive/former.rs b/module/core/former_meta/src/derive/former.rs index 5ca9feb66c..9613bce797 100644 --- a/module/core/former_meta/src/derive/former.rs +++ b/module/core/former_meta/src/derive/former.rs @@ -404,9 +404,9 @@ fn field_form_map( field : &FormerField< '_ > ) -> Result< TokenStream > qt! { - let #ident = if self.storage.#ident.is_some() + let #ident = if self.#ident.is_some() { - ::core::option::Option::Some( self.storage.#ident.take().unwrap() ) + ::core::option::Option::Some( self.#ident.take().unwrap() ) } else { @@ -464,9 +464,9 @@ fn field_form_map( field : &FormerField< '_ > ) -> Result< TokenStream > qt! { - let #ident = if self.storage.#ident.is_some() + let #ident = if self.#ident.is_some() { - self.storage.#ident.take().unwrap() + self.#ident.take().unwrap() } else { @@ -816,7 +816,6 @@ pub fn former( input : proc_macro::TokenStream ) -> Result< TokenStream > let has_debug = attr::has_debug( ast.attrs.iter() )?; let example_of_custom_setter = false; - /* names */ let name_ident = &ast.ident; @@ -839,14 +838,17 @@ pub fn former( input : proc_macro::TokenStream ) -> Result< TokenStream > qt!{ #_generics_params, } }; +// __Formed + // add embedded generic parameters let mut extra_generics : syn::Generics = parse_quote! { - < __FormerContext = #name_ident #generics_ty, __FormerEnd = former::ReturnFormed > + < __FormerContext = (), __FormerEnd = former::ReturnStorage > + // < __FormerContext = #name_ident #generics_ty, __FormerEnd = former::ReturnStorage > }; extra_generics.where_clause = parse_quote! { - where __FormerEnd : former::FormingEnd< #name_ident #generics_ty, __FormerContext >, + where __FormerEnd : former::FormingEnd< #former_storage_name_ident, __FormerContext, #name_ident #generics_ty >, }; // xxx : write helper to fix bug with where let generics_of_former = generics::merge( &generics, &extra_generics ); @@ -922,9 +924,9 @@ pub fn former( input : proc_macro::TokenStream ) -> Result< TokenStream > /// Make former, variation of builder pattern to form structure defining values of fields step by step. /// #[ inline( always ) ] - pub fn former() -> #former_name_ident < #generics_params #name_ident #generics_ty, former::ReturnFormed > + pub fn former() -> #former_name_ident < #generics_params #name_ident #generics_ty, former::ReturnStorage > { - #former_name_ident :: < #generics_params #name_ident #generics_ty, former::ReturnFormed > :: new() + #former_name_ident :: < #generics_params #name_ident #generics_ty, former::ReturnStorage > :: new() } } @@ -939,6 +941,29 @@ pub fn former( input : proc_macro::TokenStream ) -> Result< TokenStream > )* } + #[ automatically_derived ] + impl #generics_impl #former_storage_name_ident #generics_ty + #generics_where + { + + /// + /// Finish setting options and return formed entity. + /// + /// `perform` has no effect on method `form`, but change behavior and returned type of method `perform`. + /// + #[ inline( always ) ] + pub fn preform( mut self ) -> #name_ident #generics_ty + { + #( #fields_form )* + let result = #name_ident + { + #( #fields_names, )* + }; + return result; + } + + } + impl #generics_impl ::core::default::Default for #former_storage_name_ident #generics_ty #generics_where { @@ -975,14 +1000,15 @@ pub fn former( input : proc_macro::TokenStream ) -> Result< TokenStream > /// `perform` has no effect on method `form`, but change behavior and returned type of method `perform`. /// #[ inline( always ) ] - pub fn form( mut self ) -> #name_ident #generics_ty + pub fn preform( self ) -> #name_ident #generics_ty { - #( #fields_form )* - let result = #name_ident - { - #( #fields_names, )* - }; - return result; + self.storage.preform() + // #( #fields_form )* + // let result = #name_ident + // { + // #( #fields_names, )* + // }; + // return result; } /// @@ -1025,7 +1051,17 @@ pub fn former( input : proc_macro::TokenStream ) -> Result< TokenStream > /// End the process of forming returning original context of forming. /// #[ inline( always ) ] - pub fn end( mut self ) -> __FormerContext + pub fn form( mut self ) -> #name_ident #generics_ty + { + self.end() + } + + // xxx : improve documentation + /// + /// End the process of forming returning original context of forming. + /// + #[ inline( always ) ] + pub fn end( mut self ) -> #name_ident #generics_ty { let on_end = self.on_end.take().unwrap(); let context = self.context.take(); @@ -1040,7 +1076,8 @@ pub fn former( input : proc_macro::TokenStream ) -> Result< TokenStream > } #[ automatically_derived ] - impl #generics_impl #former_name_ident < #generics_params #name_ident #generics_ty, former::ReturnFormed > + // impl #generics_impl #former_name_ident < #generics_params #name_ident #generics_ty, former::ReturnStorage > + impl #generics_impl #former_name_ident < #generics_params (), former::ReturnStorage > #generics_where { @@ -1050,12 +1087,12 @@ pub fn former( input : proc_macro::TokenStream ) -> Result< TokenStream > #[ inline( always ) ] pub fn new() -> Self { - // #former_name_ident :: < #generics_params #name_ident #generics_ty, former::ReturnFormed > :: begin + // #former_name_ident :: < #generics_params #name_ident #generics_ty, former::ReturnStorage > :: begin Self :: begin ( None, None, - former::ReturnFormed, + former::ReturnStorage, ) } @@ -1075,7 +1112,7 @@ pub fn former( input : proc_macro::TokenStream ) -> Result< TokenStream > r#" impl< Context, End > UserProfileFormer< Context, End > where - End : former::FormingEnd< UserProfile, Context >, + End : former::FormingEnd< UserProfileFormerStorage, Context, UserProfile >, { pub fn age< Src >( mut self, src : Src ) -> Self where diff --git a/module/core/former_meta/src/lib.rs b/module/core/former_meta/src/lib.rs index e02c582eaf..2ba87383c6 100644 --- a/module/core/former_meta/src/lib.rs +++ b/module/core/former_meta/src/lib.rs @@ -119,9 +119,9 @@ mod derive /// impl UserProfile /// { /// #[ inline( always ) ] -/// pub fn former() -> UserProfileFormer< UserProfile, former::ReturnFormed > +/// pub fn former() -> UserProfileFormer< UserProfile, former::ReturnStorage > /// { -/// UserProfileFormer::< UserProfile, former::ReturnFormed >::new() +/// UserProfileFormer::< UserProfile, former::ReturnStorage >::new() /// } /// } /// @@ -136,7 +136,7 @@ mod derive /// pub struct UserProfileFormer /// < /// Context = UserProfile, -/// End = former::ReturnFormed, +/// End = former::ReturnStorage, /// > /// where /// End : former::FormingEnd< UserProfile, Context >, @@ -189,9 +189,9 @@ mod derive /// /// // qqq : xxx : outdated, update /// #[ inline( always ) ] -/// pub fn new() -> UserProfileFormer< UserProfile, former::ReturnFormed > +/// pub fn new() -> UserProfileFormer< UserProfile, former::ReturnStorage > /// { -/// UserProfileFormer::< UserProfile, former::ReturnFormed >::begin( None, former::ReturnFormed ) +/// UserProfileFormer::< UserProfile, former::ReturnStorage >::begin( None, former::ReturnStorage ) /// } /// /// #[ inline( always ) ] @@ -206,7 +206,7 @@ mod derive /// } /// /// #[ inline( always ) ] -/// pub fn end( mut self ) -> Context +/// pub fn end( mut self ) -> Formed /// { /// let on_end = self.on_end.take().unwrap(); /// let context = self.context.take(); diff --git a/module/core/process_tools/src/process.rs b/module/core/process_tools/src/process.rs index f41e3bf120..fe8736d5e7 100644 --- a/module/core/process_tools/src/process.rs +++ b/module/core/process_tools/src/process.rs @@ -179,10 +179,10 @@ pub( crate ) mod private let err = String::from_utf8( output.stderr ) .context( "Found invalid UTF-8" ) .map_err( | e | - { - report.error = Err( e.into() ); - Err::< (), () >( () ) - }); + { + report.error = Err( e.into() ); + Err::< (), () >( () ) + }); if err.is_err() { @@ -205,7 +205,8 @@ pub( crate ) mod private } /// Option for `run` function - #[ derive( Debug, Former ) ] + // #[ derive( Debug, Former ) ] + #[ derive( Debug ) ] // #[ debug ] pub struct Run { @@ -217,6 +218,12 @@ pub( crate ) mod private env_variable : HashMap< String, String >, } +// xxx + + + +// xxx + impl RunFormer { pub fn run( self ) -> Result< Report, Report > diff --git a/module/core/process_tools/tests/inc/process_run.rs b/module/core/process_tools/tests/inc/process_run.rs index 0aca11a047..92c76d3131 100644 --- a/module/core/process_tools/tests/inc/process_run.rs +++ b/module/core/process_tools/tests/inc/process_run.rs @@ -12,7 +12,7 @@ mod asset; // xxx : qqq : ? -// xxx2 : eliminate the function and use test_tools/process_tools instead +// xxx : eliminate the function and use test_tools/process_tools instead /// Poorly named function pub fn path_to_exe( name : &Path, temp_path : &Path ) -> PathBuf { diff --git a/module/core/process_tools/tests/tool/asset.rs b/module/core/process_tools/tests/tool/asset.rs index 7261904225..2aaa7e0c0d 100644 --- a/module/core/process_tools/tests/tool/asset.rs +++ b/module/core/process_tools/tests/tool/asset.rs @@ -46,7 +46,7 @@ pub fn path() -> std::io::Result< std::path::PathBuf > // // xxx2 : adjust Former to generate required code easier -// xxx2 : implement the interface +// xxx : implement the interface use former::Former; use std::