#builder-pattern #general-purpose #string

woptions

Mechanism to define map of options for a fuction and its defaults laconically

3 releases

0.1.3 May 26, 2022
0.1.2 May 25, 2022
0.1.0 Jan 10, 2022

#1423 in Development tools

Download history 449/week @ 2024-12-10 86/week @ 2024-12-17 124/week @ 2024-12-24 44/week @ 2024-12-31 84/week @ 2025-01-07 63/week @ 2025-01-14 164/week @ 2025-01-21 41/week @ 2025-01-28 89/week @ 2025-02-04 104/week @ 2025-02-11 230/week @ 2025-02-18 190/week @ 2025-02-25 499/week @ 2025-03-04 213/week @ 2025-03-11 129/week @ 2025-03-18 126/week @ 2025-03-25

995 downloads per month

MIT license

79KB
142 lines

Module :: woptions

experimental rust-status docs.rs discord

Mechanism to define map of options for a function and its defaults laconically.

Sample

mod splitter
{
  use former::Former;

  #[ derive( PartialOrd ) ]
  #[ derive( Former, PartialEq, Debug ) ]
  #[ perform( fn perform( self ) -> Box< ( dyn std::iter::Iterator< Item = &'a str > + 'a ) > ) ]
  pub struct Options< 'a >
  {
    pub src : &'a str,
    pub delimeter : &'a str,
    #[ default( true ) ]
    pub left : bool,
  }

  pub trait OptionsAdapter< 'a >
  {
    fn src( &self ) -> &'a str;
    fn delimeter( &self ) -> &'a str;
    fn left( &self ) -> &bool;
    #[ inline ]
    fn perform( self ) -> Box< ( dyn std::iter::Iterator< Item = &'a str > + 'a ) >
    where
      Self : Sized,
    {
      if *self.left()
      {
        Box::new( self.src().split( self.delimeter() ) )
      }
      else
      {
        Box::new( self.src().rsplit( self.delimeter() ) )
      }
    }
  }

  impl< 'a > OptionsAdapter< 'a > for Options< 'a >
  {
    #[ inline ]
    fn src( &self ) -> &'a str
    {
      &self.src
    }
    #[ inline ]
    fn delimeter( &self ) -> &'a str
    {
      &self.delimeter
    }
    #[ inline ]
    fn left( &self ) -> &bool
    {
      &self.left
    }
  }

  #[ inline ]
  pub fn former< 'a >() -> OptionsFormer< 'a >
  {
    Options::< 'a >::former()
  }

}

#[ inline ]
fn splitter< 'a >() -> splitter::OptionsFormer< 'a >
{
  splitter::former::< 'a >()
}

//

fn main()
{
  /* form options */
  let from_former = splitter().src( "abc" ).delimeter( "b" ).form();
  let from_options = splitter::Options
  {
    src : "abc",
    delimeter : "b",
    left : true,
  };
  assert_eq!( from_former, from_options );
}

To add to your project

cargo add woptions

Try out from the repository

git clone https://github.com/Wandalen/wTools
cd wTools
cd sample/rust/woptions_trivial
cargo run

Dependencies

~5MB
~133K SLoC