Crates.io | woptions |
lib.rs | woptions |
version | 0.1.3 |
source | src |
created_at | 2022-01-10 15:16:55.40246 |
updated_at | 2022-05-26 22:17:06.672239 |
description | Mechanism to define map of options for a fuction and its defaults laconically. |
homepage | https://github.com/Wandalen/wTools/tree/master/module/rust/woptions |
repository | https://github.com/Wandalen/wTools/tree/master/module/rust/woptions |
max_upload_size | |
id | 511461 |
size | 19,495 |
Mechanism to define map of options for a function and its defaults laconically.
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