#builder-pattern #general-purpose #fundamental

woptions

通过简洁的方式定义函数及其默认值的选项映射机制

3 个版本

0.1.3 2022年5月26日
0.1.2 2022年5月25日
0.1.0 2022年1月10日

#37#builder-pattern

Download history 497/week @ 2024-04-11 360/week @ 2024-04-18 716/week @ 2024-04-25 631/week @ 2024-05-02 1419/week @ 2024-05-09 721/week @ 2024-05-16 494/week @ 2024-05-23 1217/week @ 2024-05-30 438/week @ 2024-06-06 297/week @ 2024-06-13 196/week @ 2024-06-20 318/week @ 2024-06-27 173/week @ 2024-07-04 308/week @ 2024-07-11 370/week @ 2024-07-18 199/week @ 2024-07-25

1,225 每月下载量

MIT 许可证

79KB
142

模块 :: woptions

experimental rust-status docs.rs discord

通过简洁的方式定义函数及其默认值的选项映射机制。

示例

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 );
}

添加到您的项目中

cargo add woptions

从仓库尝试

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

依赖项

~5MB
~130K SLoC