From 56f222638d2b5dd228f341ff6567c5da5bc350a9 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Sun, 31 Dec 2023 11:53:21 -0800 Subject: [PATCH] make kv less bad --- src/main.rs | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index aa1e489..5c5fb0b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,21 @@ use anyhow::Result as AResult; use futures::StreamExt; type AssetID=u64; +type AssetIDFileMap=Vec<(AssetID,std::path::PathBuf)>; + +/// Parse a single key-value pair +fn parse_key_val(s:&str)->AResult<(T,U)> +where + T:std::str::FromStr, + T::Err:std::error::Error+Send+Sync+'static, + U:std::str::FromStr, + U::Err:std::error::Error+Send+Sync+'static, +{ + let pos=s + .find('=') + .ok_or_else(||anyhow::Error::msg(format!("invalid KEY=value: no `=` found in `{s}`")))?; + Ok((s[..pos].parse()?,s[pos+1..].parse()?)) +} #[derive(Parser)] #[command(author,version,about,long_about=None)] @@ -19,21 +34,17 @@ struct Cli{ #[arg(long)] cookie_file:Option, + #[arg(long,value_parser=parse_key_val::)] + asset_ids:AssetIDFileMap, + #[command(subcommand)] command:Commands, } #[derive(Subcommand)] enum Commands{ - Download(AssetIDFileMapBad), - Upload(AssetIDFileMapBad), -} - -//idk how to make this a list of key-value pairs -#[derive(Args)] -struct AssetIDFileMapBad{ - asset_ids:Vec, - files:Vec, + Download, + Upload, } #[derive(Args)] @@ -65,8 +76,8 @@ async fn main()->AResult<()>{ }; match cli.command{ - Commands::Download(asset_id_file_map)=>download_list(cookie,transpose_asset_id_file_map(asset_id_file_map)?).await, - Commands::Upload(asset_id_file_map)=>upload_list(cookie,group,transpose_asset_id_file_map(asset_id_file_map)?).await, + Commands::Download=>download_list(cookie,cli.asset_ids).await, + Commands::Upload=>upload_list(cookie,group,cli.asset_ids).await, } } @@ -98,16 +109,6 @@ fn maybe_gzip_decode(input:&mut R)->AResult>{ } } -type AssetIDFileMap=Vec<(AssetID,std::path::PathBuf)>; - -fn transpose_asset_id_file_map(asset_id_file_map:AssetIDFileMapBad)->AResult{ - if asset_id_file_map.asset_ids.len()==asset_id_file_map.files.len(){ - Ok(asset_id_file_map.asset_ids.into_iter().zip(asset_id_file_map.files.into_iter()).collect()) - }else{ - Err(anyhow::Error::msg("Asset list did not match file list.")) - } -} - async fn upload_list(cookie:String,owner:Owner,asset_id_file_map:AssetIDFileMap)->AResult<()>{ let client=reqwest::Client::new(); futures::stream::iter(asset_id_file_map)