Skip to content

Commit

Permalink
Merge pull request #1260 from Barsik-sus/wca_refactor
Browse files Browse the repository at this point in the history
READY: (wca): Replace `Args` and `Props` with `VerifiedCommand` in routines
  • Loading branch information
Wandalen authored Apr 1, 2024
2 parents db572a5 + b5c9c52 commit 8c52ccc
Show file tree
Hide file tree
Showing 28 changed files with 240 additions and 372 deletions.
38 changes: 19 additions & 19 deletions module/move/unitore/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use feed_config::SubscriptionConfig;
use gluesql::sled_storage::{ sled::Config, SledStorage };
use retriever::{ FeedClient, FeedFetch };
use storage::{ Store, FeedStorage, feed::FeedStore, config::ConfigStore, table::TableStore, frame::FrameStore };
use wca::{ Args, Type };
use wca::{ Args, Type, VerifiedCommand };
use executor::actions::Report;
use error_tools::Result;

Expand Down Expand Up @@ -52,9 +52,9 @@ pub fn execute() -> Result< (), Box< dyn std::error::Error + Send + Sync > >
"Download frames from feed sources provided in config files.\n",
" Example: .frames.download",
))
.routine( | args |
.routine( | o : VerifiedCommand |
{
match action( download_frames, &args )
match action( download_frames, &o.args )
{
Ok( report ) => report.report(),
Err( err ) => println!( "{:?}", err ),
Expand All @@ -68,9 +68,9 @@ pub fn execute() -> Result< (), Box< dyn std::error::Error + Send + Sync > >
"List all feeds from storage.\n",
" Example: .feeds.list",
))
.routine( | args |
.routine( | o : VerifiedCommand |
{
match action( list_feeds, &args )
match action( list_feeds, &o.args )
{
Ok( report ) => report.report(),
Err( err ) => println!( "{:?}", err ),
Expand All @@ -84,9 +84,9 @@ pub fn execute() -> Result< (), Box< dyn std::error::Error + Send + Sync > >
"List all frames saved in storage.\n",
" Example: .frames.list",
))
.routine( | args |
.routine( | o : VerifiedCommand |
{
match action( list_frames, &args )
match action( list_frames, &o.args )
{
Ok( report ) => report.report(),
Err( err ) => println!( "{:?}", err ),
Expand All @@ -108,9 +108,9 @@ pub fn execute() -> Result< (), Box< dyn std::error::Error + Send + Sync > >
" link = \"https://feeds.bbci.co.uk/news/world/rss.xml\"\n",
))
.subject().hint( "Path" ).kind( Type::Path ).optional( false ).end()
.routine( | args : Args |
.routine( | o : VerifiedCommand |
{
match action( add_config, &args )
match action( add_config, &o.args )
{
Ok( report ) => report.report(),
Err( err ) => println!( "{:?}", err ),
Expand All @@ -125,9 +125,9 @@ pub fn execute() -> Result< (), Box< dyn std::error::Error + Send + Sync > >
" Example: .config.delete ./config/feeds.toml",
))
.subject().hint( "Path" ).kind( Type::Path ).optional( false ).end()
.routine( | args : Args |
.routine( | o : VerifiedCommand |
{
match action( delete_config, &args )
match action( delete_config, &o.args )
{
Ok( report ) => report.report(),
Err( err ) => println!( "{:?}", err ),
Expand All @@ -141,9 +141,9 @@ pub fn execute() -> Result< (), Box< dyn std::error::Error + Send + Sync > >
"List all config files saved in storage.\n",
" Example: .config.list",
))
.routine( | args |
.routine( | o : VerifiedCommand |
{
match action( list_configs, &args )
match action( list_configs, &o.args )
{
Ok( report ) => report.report(),
Err( err ) => println!( "{:?}", err ),
Expand All @@ -157,9 +157,9 @@ pub fn execute() -> Result< (), Box< dyn std::error::Error + Send + Sync > >
"List all tables saved in storage.\n",
" Example: .tables.list",
))
.routine( | args |
.routine( | o : VerifiedCommand |
{
match action( list_tables, &args )
match action( list_tables, &o.args )
{
Ok( report ) => report.report(),
Err( err ) => println!( "{:?}", err ),
Expand All @@ -175,9 +175,9 @@ pub fn execute() -> Result< (), Box< dyn std::error::Error + Send + Sync > >
" Example: .table.list feed",
))
.subject().hint( "Name" ).kind( wca::Type::String ).optional( false ).end()
.routine( | args : Args |
.routine( | o : VerifiedCommand |
{
match action( list_columns, &args )
match action( list_columns, &o.args )
{
Ok( report ) => report.report(),
Err( err ) => println!( "{:?}", err ),
Expand All @@ -198,9 +198,9 @@ pub fn execute() -> Result< (), Box< dyn std::error::Error + Send + Sync > >
"\n\n",
))
.subject().hint( "Query" ).kind( Type::List( Type::String.into(), ' ' ) ).optional( false ).end()
.routine( | args : Args |
.routine( | o : VerifiedCommand |
{
match action( execute_query, &args )
match action( execute_query, &o.args )
{
Ok( report ) => report.report(),
Err( err ) => println!( "{:?}", err ),
Expand Down
6 changes: 3 additions & 3 deletions module/move/wca/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The tool to make CLI ( commands user interface ). It is able to aggregate extern
```rust
#[ cfg( not( feature = "no_std" ) ) ]
{
use wca::{ Args, Context, Type };
use wca::{ VerifiedCommand, Context, Type };

fn main()
{
Expand All @@ -24,12 +24,12 @@ The tool to make CLI ( commands user interface ). It is able to aggregate extern
.hint( "prints all subjects and properties" )
.subject().hint( "Subject" ).kind( Type::String ).optional( true ).end()
.property( "property" ).hint( "simple property" ).kind( Type::String ).optional( true ).end()
.routine( | args : Args, props | { println!( "= Args\n{args:?}\n\n= Properties\n{props:?}\n" ) } )
.routine( | o : VerifiedCommand | { println!( "= Args\n{:?}\n\n= Properties\n{:?}\n", o.args, o.props ) } )
.end()
.command( "error" )
.hint( "prints all subjects and properties" )
.subject().hint( "Error message" ).kind( Type::String ).optional( true ).end()
.routine( | args : Args | { println!( "Returns an error" ); Err( format!( "{}", args.get_owned::< String >( 0 ).unwrap_or_default() ) ) } )
.routine( | o : VerifiedCommand | { println!( "Returns an error" ); Err( format!( "{}", o.args.get_owned::< String >( 0 ).unwrap_or_default() ) ) } )
.end()
.command( "exit" )
.hint( "just exit" )
Expand Down
6 changes: 3 additions & 3 deletions module/move/wca/examples/wca_fluent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//!

use wca::{ Args, Context, Type };
use wca::{ Context, Type, VerifiedCommand };
use std::sync::{ Arc, Mutex };

fn main()
Expand All @@ -18,7 +18,7 @@ fn main()
.hint( "prints all subjects and properties" )
.subject().kind( Type::String ).optional( true ).end()
.property( "property" ).hint( "simple property" ).kind( Type::String ).optional( true ).end()
.routine( | args : Args, props | { println!( "= Args\n{args:?}\n\n= Properties\n{props:?}\n" ) } )
.routine( | o : VerifiedCommand | { println!( "= Args\n{:?}\n\n= Properties\n{:?}\n", o.args, o.props ) } )
.end()
.command( "inc" )
.hint( "This command increments a state number each time it is called consecutively. (E.g. `.inc .inc`)" )
Expand All @@ -33,7 +33,7 @@ fn main()
.command( "error" )
.hint( "prints all subjects and properties" )
.subject().kind( Type::String ).optional( true ).end()
.routine( | args : Args | { println!( "Returns an error" ); Err( format!( "{}", args.get_owned::< String >( 0 ).unwrap_or_default() ) ) } )
.routine( | o : VerifiedCommand | { println!( "Returns an error" ); Err( format!( "{}", o.args.get_owned::< String >( 0 ).unwrap_or_default() ) ) } )
.end()
.command( "exit" )
.hint( "just exit" )
Expand Down
6 changes: 3 additions & 3 deletions module/move/wca/examples/wca_suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//! ```
//!
use wca::{ CommandsAggregator, Args, Props, Type };
use wca::{ CommandsAggregator, Type, VerifiedCommand };

fn main()
{
Expand All @@ -30,9 +30,9 @@ fn main()
.hint( "prints all subjects and properties" )
.subject().kind( Type::String ).optional( true ).end()
.property( "property" ).hint( "simple property" ).kind( Type::String ).optional( true ).end()
.routine( | args : Args, props : Props |
.routine( | o : VerifiedCommand |
{
println!( "= Args\n{args:?}\n\n= Properties\n{props:?}\n" );
println!( "= Args\n{:?}\n\n= Properties\n{:?}\n", o.args, o.props );
})
.end()
.perform();
Expand Down
6 changes: 3 additions & 3 deletions module/move/wca/examples/wca_trivial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
//! A trivial example.
//!
use wca::{ CommandsAggregator, Args, Props, Type };
use wca::{ CommandsAggregator, Type, VerifiedCommand };

fn f1( args : Args, props : Props )
fn f1( o : VerifiedCommand )
{
println!( "= Args\n{args:?}\n\n= Properties\n{props:?}\n" );
println!( "= Args\n{:?}\n\n= Properties\n{:?}\n", o.args, o.props );
}

fn exit()
Expand Down
4 changes: 2 additions & 2 deletions module/move/wca/src/ca/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ pub( crate ) mod private
/// # Example:
///
/// ```
/// use wca::{ CommandsAggregator, Args, Props, Type };
/// use wca::{ CommandsAggregator, VerifiedCommand, Type };
///
/// # fn main() -> Result< (), Box< dyn std::error::Error > > {
/// let ca = CommandsAggregator::former()
/// .command( "echo" )
/// .hint( "prints all subjects and properties" )
/// .subject().hint( "argument" ).kind( Type::String ).optional( false ).end()
/// .property( "property" ).hint( "simple property" ).kind( Type::String ).optional( false ).end()
/// .routine( | args : Args, props : Props | println!( "= Args\n{args:?}\n\n= Properties\n{props:?}\n" ) )
/// .routine( | o : VerifiedCommand | println!( "= Args\n{:?}\n\n= Properties\n{:?}\n", o.args, o.props ) )
/// .end()
/// .perform();
///
Expand Down
21 changes: 13 additions & 8 deletions module/move/wca/src/ca/executor/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,29 @@ pub( crate ) mod private
/// ```
///
/// ```
/// # use wca::{ Routine, Context, Value, Args, Props };
/// # use wca::{ Routine, Handler, Context, Value, Args, Props, VerifiedCommand };
/// # use std::sync::{ Arc, Mutex };
/// let routine = Routine::new_with_ctx
/// let routine = Routine::from( Handler::from
/// (
/// | ( args, props ), ctx |
/// | ctx : Context, o : VerifiedCommand |
/// {
/// let first_arg : i32 = args.get_owned( 0 ).unwrap_or_default();
/// let first_arg : i32 = o.args.get_owned( 0 ).unwrap_or_default();
/// let ctx_value : Arc< Mutex< i32 > > = ctx.get_or_default();
///
/// *ctx_value.lock().unwrap() += first_arg;
///
/// Ok( () )
/// }
/// );
/// ) );
/// let ctx = Context::default();
/// if let Routine::WithContext( callback ) = routine
/// {
/// callback( ( Args( vec![ Value::Number( 1.0 ) ] ), Props( Default::default() ) ), ctx.clone() ).unwrap();
/// let w_command = VerifiedCommand
/// {
/// phrase : "command".into(),
/// internal_command : false,
/// args : Args( vec![ Value::Number( 1.0 ) ] ),
/// props : Props( Default::default() ),
/// };
/// callback( ctx.clone(), w_command ).unwrap();
/// }
/// assert_eq!( 1, *ctx.get::< Arc< Mutex< i32 > > >().unwrap().lock().unwrap() );
/// ```
Expand Down
4 changes: 2 additions & 2 deletions module/move/wca/src/ca/executor/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ pub( crate ) mod private
{
match routine
{
Routine::WithoutContext( routine ) => routine(( Args( command.subjects ), Props( command.properties ) )),
Routine::WithContext( routine ) => routine( ( Args( command.subjects ), Props( command.properties ) ), ctx ),
Routine::WithoutContext( routine ) => routine( command ),
Routine::WithContext( routine ) => routine( ctx, command ),
}
}

Expand Down
Loading

0 comments on commit 8c52ccc

Please sign in to comment.