Skip to content

Commit

Permalink
unitore: we can define async command in prettier way (#1464)
Browse files Browse the repository at this point in the history
Logic is unchanged. Compiles to the same code. Looks prettier.
  • Loading branch information
Barsik-sus authored Oct 12, 2024
1 parent ee5934a commit 19f187e
Showing 1 changed file with 47 additions and 67 deletions.
114 changes: 47 additions & 67 deletions module/move/unitore/src/command/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,28 @@ impl ConfigCommand
/// Create command for adding config.
pub fn add() -> Result< Command >
{
let rt = tokio::runtime::Runtime::new()?;
#[ tokio::main ]
async fn add_command( o : VerifiedCommand )
{
// qqq: could we print something on None value?
let Some( path ) = o.args.get_owned::< PathBuf >( 0 ) else { return; };

let path_to_storage = std::env::var( "UNITORE_STORAGE_PATH" ).ok()
.unwrap_or_else( || String::from( "./_data" ) );
let config = Config::default().path( path_to_storage );

let res = ( || async
{
let feed_storage = FeedStorage::init_storage( &config ).await?;
config_add( feed_storage, &path ).await
} )().await;

match res
{
Ok( report ) => report.report(),
Err( err ) => println!( "{:?}", err ),
}
}

Ok
(
Expand All @@ -37,47 +58,37 @@ impl ConfigCommand
" link = \"https://feeds.bbci.co.uk/news/world/rss.xml\"\n",
))
.subject().hint( "Path" ).kind( Type::Path ).optional( false ).end()
.routine( move | o : VerifiedCommand |
{
let path_arg = o.args
.get_owned::< wca::Value >( 0 );

if let Some( path ) = path_arg
{
let path : PathBuf = path.into();

let res = rt.block_on
( async move
{
let path_to_storage = std::env::var( "UNITORE_STORAGE_PATH" )
.unwrap_or( String::from( "./_data" ) )
;

let config = Config::default()
.path( path_to_storage )
;

let feed_storage = FeedStorage::init_storage( &config ).await?;
config_add( feed_storage, &path ).await
}
);

match res
{
Ok( report ) => report.report(),
Err( err ) => println!( "{:?}", err ),
}
}
})
.routine( add_command )
.end()
)
}

/// Create command for deleting config.
pub fn delete() -> Result< Command >
{
let rt = tokio::runtime::Runtime::new()?;

#[ tokio::main ]
async fn delete_command( o : VerifiedCommand )
{
// qqq: could we print something on None value?
let Some( path ) = o.args.get_owned::< PathBuf >( 0 ) else { return; };

let path_to_storage = std::env::var( "UNITORE_STORAGE_PATH" ).ok()
.unwrap_or_else( || String::from( "./_data" ) );
let config = Config::default().path( path_to_storage );

let res = ( || async
{
let feed_storage = FeedStorage::init_storage( &config ).await?;
config_delete( feed_storage, &path ).await
} )().await;

match res
{
Ok( report ) => report.report(),
Err( err ) => println!( "{:?}", err ),
}
}

Ok(
Command::former()
.phrase( "config.delete" )
Expand All @@ -87,38 +98,7 @@ impl ConfigCommand
" Example: .config.delete ./config/feeds.toml",
))
.subject().hint( "Path" ).kind( Type::Path ).optional( false ).end()
.routine( move | o : VerifiedCommand |
{
let path_arg = o.args
.get_owned::< wca::Value >( 0 );

if let Some( path ) = path_arg
{
let path : PathBuf = path.into();

let res = rt.block_on
( async move
{
let path_to_storage = std::env::var( "UNITORE_STORAGE_PATH" )
.unwrap_or( String::from( "./_data" ) )
;

let config = Config::default()
.path( path_to_storage )
;

let feed_storage = FeedStorage::init_storage( &config ).await?;
config_delete( feed_storage, &path ).await
}
);

match res
{
Ok( report ) => report.report(),
Err( err ) => println!( "{:?}", err ),
}
}
})
.routine( delete_command )
.end()
)
}
Expand Down

0 comments on commit 19f187e

Please sign in to comment.