Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
former : making subforming more friendly
Browse files Browse the repository at this point in the history
Wandalen committed Mar 20, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 5b22bf2 commit 927e66a
Showing 36 changed files with 566 additions and 541 deletions.
58 changes: 29 additions & 29 deletions module/core/former/Readme.md
Original file line number Diff line number Diff line change
@@ -85,14 +85,14 @@ pub struct UserProfile
impl UserProfile
{
#[ inline( always ) ]
pub fn former() -> UserProfileFormer< UserProfile, former::ReturnContainer >
pub fn former() -> UserProfileFormer< UserProfile, former::ReturnFormed >
{
UserProfileFormer::< UserProfile, former::ReturnContainer >::new()
UserProfileFormer::< UserProfile, former::ReturnFormed >::new()
}
}

#[ derive( Debug, Default ) ]
pub struct UserProfileFormerContainer
pub struct UserProfileFormerStorage
{
age : Option< i32 >,
username : Option< String >,
@@ -102,12 +102,12 @@ pub struct UserProfileFormerContainer
pub struct UserProfileFormer
<
FormerContext = UserProfile,
FormerEnd = former::ReturnContainer,
FormerEnd = former::ReturnFormed,
>
where
FormerEnd : former::ToSuperFormer< UserProfile, FormerContext >,
{
container : UserProfileFormerContainer,
storage : UserProfileFormerStorage,
context : Option< FormerContext >,
on_end : Option< FormerEnd >,
}
@@ -119,9 +119,9 @@ where
#[ inline( always ) ]
pub fn form( mut self ) -> UserProfile
{
let age = if self.container.age.is_some()
let age = if self.storage.age.is_some()
{
self.container.age.take().unwrap()
self.storage.age.take().unwrap()
}
else
{
@@ -149,9 +149,9 @@ where
};
val
};
let username = if self.container.username.is_some()
let username = if self.storage.username.is_some()
{
self.container.username.take().unwrap()
self.storage.username.take().unwrap()
}
else
{
@@ -179,9 +179,9 @@ where
};
val
};
let bio_optional = if self.container.bio_optional.is_some()
let bio_optional = if self.storage.bio_optional.is_some()
{
Option::Some( self.container.bio_optional.take().unwrap() )
Option::Some( self.storage.bio_optional.take().unwrap() )
}
else
{
@@ -204,9 +204,9 @@ where
}

#[ inline( always ) ]
pub fn new() -> UserProfileFormer< UserProfile, former::ReturnContainer >
pub fn new() -> UserProfileFormer< UserProfile, former::ReturnFormed >
{
UserProfileFormer::< UserProfile, former::ReturnContainer >::begin( None, former::ReturnContainer )
UserProfileFormer::< UserProfile, former::ReturnFormed >::begin( None, former::ReturnFormed )
}

#[ inline( always ) ]
@@ -218,7 +218,7 @@ where
{
Self
{
container : core::default::Default::default(),
storage : core::default::Default::default(),
context : context,
on_end : Option::Some( on_end ),
}
@@ -229,17 +229,17 @@ where
{
let on_end = self.on_end.take().unwrap();
let context = self.context.take();
let container = self.form();
on_end.call( container, context )
let formed = self.form();
on_end.call( formed, context )
}

#[ inline ]
pub fn age< Src >( mut self, src : Src ) -> Self
where
Src : Into< i32 >,
{
debug_assert!( self.container.age.is_none() );
self.container.age = Option::Some( src.into() );
debug_assert!( self.storage.age.is_none() );
self.storage.age = Option::Some( src.into() );
self
}

@@ -248,8 +248,8 @@ where
where
Src : Into< String >,
{
debug_assert!( self.container.username.is_none() );
self.container.username = Option::Some( src.into() );
debug_assert!( self.storage.username.is_none() );
self.storage.username = Option::Some( src.into() );
self
}

@@ -258,8 +258,8 @@ where
where
Src : Into< String >,
{
debug_assert!( self.container.bio_optional.is_none() );
self.container.bio_optional = Option::Some( src.into() );
debug_assert!( self.storage.bio_optional.is_none() );
self.storage.bio_optional = Option::Some( src.into() );
self
}
}
@@ -305,8 +305,8 @@ impl StructWithCustomSettersFormer
// Custom alternative setter for `word`
pub fn word_exclaimed( mut self, value : impl Into< String > ) -> Self
{
debug_assert!( self.container.word.is_none() );
self.container.word = Some( format!( "{}!", value.into() ) );
debug_assert!( self.storage.word.is_none() );
self.storage.word = Some( format!( "{}!", value.into() ) );
self
}

@@ -351,8 +351,8 @@ impl StructWithCustomSettersFormer
// Custom alternative setter for `word`
pub fn word( mut self, value : impl Into< String > ) -> Self
{
debug_assert!( self.container.word.is_none() );
self.container.word = Some( format!( "{}!", value.into() ) );
debug_assert!( self.storage.word.is_none() );
self.storage.word = Some( format!( "{}!", value.into() ) );
self
}

@@ -553,19 +553,19 @@ fn main()
let on_end = | command : Command, super_former : core::option::Option< Self > | -> Self
{
let mut super_former = super_former.unwrap();
if let Some( ref mut commands ) = super_former.container.command
if let Some( ref mut commands ) = super_former.storage.command
{
commands.insert( command.name.clone(), command );
}
else
{
let mut commands: HashMap< String, Command > = Default::default();
commands.insert( command.name.clone(), command );
super_former.container.command = Some( commands );
super_former.storage.command = Some( commands );
}
super_former
};
let former = CommandFormer::begin( Some( self ), on_end );
let former = CommandFormer::begin( None, Some( self ), on_end );
former.name( name )
}
}
4 changes: 2 additions & 2 deletions module/core/former/examples/former_custom_setter.rs
Original file line number Diff line number Diff line change
@@ -25,8 +25,8 @@ fn main()
// Custom alternative setter for `word`
pub fn word_exclaimed( mut self, value : impl Into< String > ) -> Self
{
debug_assert!( self.container.word.is_none() );
self.container.word = Some( format!( "{}!", value.into() ) );
debug_assert!( self.storage.word.is_none() );
self.storage.word = Some( format!( "{}!", value.into() ) );
self
}

4 changes: 2 additions & 2 deletions module/core/former/examples/former_custom_setter_overriden.rs
Original file line number Diff line number Diff line change
@@ -25,8 +25,8 @@ fn main()
// Custom alternative setter for `word`
pub fn word( mut self, value : impl Into< String > ) -> Self
{
debug_assert!( self.container.word.is_none() );
self.container.word = Some( format!( "{}!", value.into() ) );
debug_assert!( self.storage.word.is_none() );
self.storage.word = Some( format!( "{}!", value.into() ) );
self
}

9 changes: 5 additions & 4 deletions module/core/former/examples/former_custom_subformer.rs
Original file line number Diff line number Diff line change
@@ -34,26 +34,27 @@ fn main()
#[ inline( always ) ]
pub fn command< IntoName >( self, name : IntoName ) -> CommandFormer< Self, impl former::ToSuperFormer< Command, Self > >
where
IntoName: core::convert::Into< String >,
IntoName : core::convert::Into< String >,
{
let on_end = | command : Command, super_former : core::option::Option< Self > | -> Self
{
let mut super_former = super_former.unwrap();
if let Some( ref mut commands ) = super_former.container.command
if let Some( ref mut commands ) = super_former.storage.command
{
commands.insert( command.name.clone(), command );
}
else
{
let mut commands: HashMap< String, Command > = Default::default();
commands.insert( command.name.clone(), command );
super_former.container.command = Some( commands );
super_former.storage.command = Some( commands );
}
super_former
};
let former = CommandFormer::begin( Some( self ), on_end );
let former = CommandFormer::begin( None, Some( self ), on_end );
former.name( name )
}
// xxx : review
}

let ca = Aggregator::former()
3 changes: 2 additions & 1 deletion module/core/former/examples/former_debug.rs
Original file line number Diff line number Diff line change
@@ -13,7 +13,8 @@ fn main()


#[ derive( Debug, PartialEq, Former ) ]
#[ debug ]
// #[ debug ]
// Uncomment to see what derive expand into
pub struct UserProfile
{
age : i32,
46 changes: 24 additions & 22 deletions module/core/former/examples/former_trivial_expaned.rs
Original file line number Diff line number Diff line change
@@ -16,6 +16,8 @@
//! This approach abstracts away the need for manually implementing a builder for each struct, making code more readable and maintainable.
//!
// xxx : regenerate

#![ allow( dead_code ) ]

#[ cfg( any( not( feature = "derive_former" ), not( feature = "enabled" ) ) ) ]
@@ -36,14 +38,14 @@ fn main()
impl UserProfile
{
#[ inline( always ) ]
pub fn former() -> UserProfileFormer< UserProfile, former::ReturnContainer >
pub fn former() -> UserProfileFormer< UserProfile, former::ReturnFormed >
{
UserProfileFormer::< UserProfile, former::ReturnContainer >::new()
UserProfileFormer::< UserProfile, former::ReturnFormed >::new()
}
}

#[ derive( Debug, Default ) ]
pub struct UserProfileFormerContainer
pub struct UserProfileFormerStorage
{
age : Option< i32 >,
username : Option< String >,
@@ -53,12 +55,12 @@ fn main()
pub struct UserProfileFormer
<
FormerContext = UserProfile,
FormerEnd = former::ReturnContainer,
FormerEnd = former::ReturnFormed,
>
where
FormerEnd : former::ToSuperFormer< UserProfile, FormerContext >,
{
container : UserProfileFormerContainer,
storage : UserProfileFormerStorage,
context : Option< FormerContext >,
on_end : Option< FormerEnd >,
}
@@ -70,9 +72,9 @@ fn main()
#[ inline( always ) ]
pub fn form( mut self ) -> UserProfile
{
let age = if self.container.age.is_some()
let age = if self.storage.age.is_some()
{
self.container.age.take().unwrap()
self.storage.age.take().unwrap()
}
else
{
@@ -100,9 +102,9 @@ fn main()
};
val
};
let username = if self.container.username.is_some()
let username = if self.storage.username.is_some()
{
self.container.username.take().unwrap()
self.storage.username.take().unwrap()
}
else
{
@@ -130,9 +132,9 @@ fn main()
};
val
};
let bio_optional = if self.container.bio_optional.is_some()
let bio_optional = if self.storage.bio_optional.is_some()
{
Option::Some( self.container.bio_optional.take().unwrap() )
Option::Some( self.storage.bio_optional.take().unwrap() )
}
else
{
@@ -155,9 +157,9 @@ fn main()
}

#[ inline( always ) ]
pub fn new() -> UserProfileFormer< UserProfile, former::ReturnContainer >
pub fn new() -> UserProfileFormer< UserProfile, former::ReturnFormed >
{
UserProfileFormer::< UserProfile, former::ReturnContainer >::begin( None, former::ReturnContainer )
UserProfileFormer::< UserProfile, former::ReturnFormed >::begin( None, former::ReturnFormed )
}

#[ inline( always ) ]
@@ -169,7 +171,7 @@ fn main()
{
Self
{
container : core::default::Default::default(),
storage : core::default::Default::default(),
context : context,
on_end : Option::Some( on_end ),
}
@@ -180,17 +182,17 @@ fn main()
{
let on_end = self.on_end.take().unwrap();
let context = self.context.take();
let container = self.form();
on_end.call( container, context )
let formed = self.form();
on_end.call( formed, context )
}

#[ inline ]
pub fn age< Src >( mut self, src : Src ) -> Self
where
Src : Into< i32 >,
{
debug_assert!( self.container.age.is_none() );
self.container.age = Option::Some( src.into() );
debug_assert!( self.storage.age.is_none() );
self.storage.age = Option::Some( src.into() );
self
}

@@ -199,8 +201,8 @@ fn main()
where
Src : Into< String >,
{
debug_assert!( self.container.username.is_none() );
self.container.username = Option::Some( src.into() );
debug_assert!( self.storage.username.is_none() );
self.storage.username = Option::Some( src.into() );
self
}

@@ -209,8 +211,8 @@ fn main()
where
Src : Into< String >,
{
debug_assert!( self.container.bio_optional.is_none() );
self.container.bio_optional = Option::Some( src.into() );
debug_assert!( self.storage.bio_optional.is_none() );
self.storage.bio_optional = Option::Some( src.into() );
self
}
}
Loading

0 comments on commit 927e66a

Please sign in to comment.