former 2.43.0

A flexible implementation of the Builder pattern supporting nested builders and collection-specific subformers. Simplify the construction of complex objects.
Documentation
#![allow(clippy::used_underscore_binding, clippy::all, warnings, missing_docs)]
#[ allow( unused_imports ) ]
use super::*;
#[ allow( unused_imports ) ]
use test_tools::a_id;

#[ derive( Debug, PartialEq, the_module::Former ) ]
pub struct StructWithCustomSetters {
  ordinary: String,
  #[ scalar( setter = false ) ]
  magic: String,
}

impl<Definition> StructWithCustomSettersFormer<Definition>
where
  Definition: former::FormerDefinition<Storage = StructWithCustomSettersFormerStorage>,
{
  /// Custom alternative setter of ordinary field.
  fn ordinary_exclamaited<IntoString>(mut self, val: IntoString) -> Self
  where
    IntoString: Into<String>,
  {
    debug_assert!(self.storage.ordinary.is_none());
    self.storage.ordinary = Some(format!("{}!", val.into()));
    self
  }

  /// Custom primary setter of field without autogenerated setter.
  fn magic<IntoString>(mut self, val: IntoString) -> Self
  where
    IntoString: Into<String>,
  {
    debug_assert!(self.storage.magic.is_none());
    self.storage.magic = Some(format!("Some magic : < {} >", val.into()));
    self
  }
}

#[ test ]
fn basic() {
  // ordinary + magic
  let got = StructWithCustomSetters::former().ordinary("val1").magic("val2").form();
  let exp = StructWithCustomSetters {
    ordinary: "val1".to_string(),
    magic: "Some magic : < val2 >".to_string(),
  };
  a_id!(got, exp);

  // alternative
  let got = StructWithCustomSetters::former().ordinary_exclamaited("val1").form();
  let exp = StructWithCustomSetters {
    ordinary: "val1!".to_string(),
    magic: String::new(),
  };
  a_id!(got, exp);
}