1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
use super::*;
#[ allow( unused ) ]
use collection_tools::Vec;
/// Trait for containers that behave like a vector, providing an interface for element addition.
///
/// This trait enables the use of custom or standard vector-like containers within the builder pattern,
/// allowing for a unified and flexible approach to constructing collections.
///
pub trait VectorLike< E >
{
/// Appends an element to the back of a formed.
fn push( &mut self, element : E );
}
impl< E > VectorLike< E > for Vec< E >
{
fn push( &mut self, element : E )
{
Vec::push( self, element );
}
}
/// A builder for constructing `VectorLike` containers, facilitating a fluent and flexible interface.
///
/// `VectorSubformer` leverages the `VectorLike` trait to enable the construction and manipulation
/// of vector-like containers in a builder pattern style, promoting readability and ease of use.
///
/// # Example
/// ```rust
/// #[ derive( Debug, PartialEq, former::Former ) ]
/// pub struct StructWithVec
/// {
/// #[ subformer( former::VectorSubformer ) ]
/// vec : Vec< &'static str >,
/// }
///
/// let instance = StructWithVec::former()
/// .vec()
/// .push( "apple" )
/// .push( "banana" )
/// .end()
/// .form();
///
/// assert_eq!( instance, StructWithVec { vec: vec![ "apple", "banana" ] } );
///```
///
#[ derive( Debug, Default ) ]
pub struct VectorSubformer< E, Formed, Context, ContainerEnd >
where
Formed : VectorLike< E > + core::default::Default,
ContainerEnd : FormingEnd< Formed, Context >,
{
formed : core::option::Option< Formed >,
context : core::option::Option< Context >,
on_end : core::option::Option< ContainerEnd >,
_phantom : core::marker::PhantomData< E >,
}
impl< E, Formed, Context, ContainerEnd > VectorSubformer< E, Formed, Context, ContainerEnd >
where
Formed : VectorLike< E > + core::default::Default,
ContainerEnd : FormingEnd< Formed, Context >,
{
/// Form current former into target structure.
#[ inline( always ) ]
pub fn form( mut self ) -> Formed
{
let formed = if self.formed.is_some()
{
self.formed.take().unwrap()
}
else
{
let val = Default::default();
val
};
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
(
formed : core::option::Option< Formed >,
context : core::option::Option< Context >,
on_end : ContainerEnd
) -> Self
{
Self
{
context,
formed,
on_end : Some( on_end ),
_phantom : core::marker::PhantomData,
}
}
/// Finalizes the building process, returning the formed or a context incorporating it.
#[ inline( always ) ]
pub fn end( mut self ) -> Context
{
let on_end = self.on_end.take().unwrap();
let context = self.context.take();
let formed = self.form();
on_end.call( formed, context )
}
/// Replaces the current formed with a provided one, allowing for a reset or redirection of the building process.
#[ inline( always ) ]
pub fn replace( mut self, vector : Formed ) -> Self
{
self.formed = Some( vector );
self
}
}
impl< E, Formed > VectorSubformer< E, Formed, Formed, crate::ReturnFormed >
where
Formed : VectorLike< E > + core::default::Default,
{
/// 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() -> Self
{
Self::begin
(
None,
None,
crate::ReturnFormed,
)
}
}
impl< E, Formed, Context, ContainerEnd > VectorSubformer< E, Formed, Context, ContainerEnd >
where
Formed : VectorLike< E > + core::default::Default,
ContainerEnd : FormingEnd< Formed, Context >,
{
/// Appends an element to the end of the formed, expanding the internal collection.
#[ inline( always ) ]
pub fn push< E2 >( mut self, element : E2 ) -> Self
where E2 : core::convert::Into< E >,
{
if self.formed.is_none()
{
self.formed = core::option::Option::Some( Default::default() );
}
if let core::option::Option::Some( ref mut formed ) = self.formed
{
formed.push( element.into() );
}
self
}
}
//
impl< E, Formed, Context, End > FormerBegin< Formed, Formed, Context >
for VectorSubformer< E, Formed, Context, End >
where
End : FormingEnd< Formed, Context >,
Formed : VectorLike< E > + Default,
{
type End = End;
#[ inline( always ) ]
fn _begin
(
formed : core::option::Option< Formed >,
context : core::option::Option< Context >,
on_end : End,
) -> Self
{
Self::begin( formed, context, on_end )
}
}