From 6915a71802907bd021f9155bf76eca90f8b9fc6f Mon Sep 17 00:00:00 2001 From: alemi Date: Sun, 20 Oct 2024 01:33:45 +0200 Subject: [PATCH] feat: default to single thread runtime but allow multi-thread-runtime with --multi-threaded flag --- src/main.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 921e25e..24215e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,10 @@ struct PostWomanArgs { /// action to run #[clap(subcommand)] action: Option, + + /// start a multi-thread runtime, with multiple worker threads + #[arg(long, default_value_t = false)] + multi_threaded: bool, } #[derive(Subcommand, Debug, Default)] @@ -62,12 +66,29 @@ pub enum PostWomanActions { const TIMESTAMP_FMT: &str = "%H:%M:%S%.6f"; -#[tokio::main] -async fn main() -> Result<(), PostWomanError> { +fn main() -> Result<(), PostWomanError> { let args = PostWomanArgs::parse(); - let collection = std::fs::read_to_string(args.collection)?; - let config: PostWomanConfig = toml::from_str(&collection)?; + let collection_raw = std::fs::read_to_string(&args.collection)?; + let collection: PostWomanCollection = toml::from_str(&collection_raw)?; + + if args.multi_threaded { + tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .expect("failed creating tokio multi-thread runtime") + .block_on(async { run_postwoman(args, collection).await }) + } else { + tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .expect("failed creating tokio current-thread runtime") + .block_on(async { run_postwoman(args, collection).await }) + } +} + +async fn run_postwoman(args: PostWomanArgs, collection: PostWomanCollection) -> Result<(), PostWomanError> { + let action = args.action.unwrap_or(PostWomanActions::List { verbose: false }); match args.action.unwrap_or_default() { PostWomanActions::List => {