make kv less bad

This commit is contained in:
Quaternions 2023-12-31 11:53:21 -08:00
parent c080634a53
commit 56f222638d

View File

@ -4,6 +4,21 @@ use anyhow::Result as AResult;
use futures::StreamExt; use futures::StreamExt;
type AssetID=u64; type AssetID=u64;
type AssetIDFileMap=Vec<(AssetID,std::path::PathBuf)>;
/// Parse a single key-value pair
fn parse_key_val<T,U>(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)] #[derive(Parser)]
#[command(author,version,about,long_about=None)] #[command(author,version,about,long_about=None)]
@ -19,21 +34,17 @@ struct Cli{
#[arg(long)] #[arg(long)]
cookie_file:Option<std::path::PathBuf>, cookie_file:Option<std::path::PathBuf>,
#[arg(long,value_parser=parse_key_val::<AssetID,std::path::PathBuf>)]
asset_ids:AssetIDFileMap,
#[command(subcommand)] #[command(subcommand)]
command:Commands, command:Commands,
} }
#[derive(Subcommand)] #[derive(Subcommand)]
enum Commands{ enum Commands{
Download(AssetIDFileMapBad), Download,
Upload(AssetIDFileMapBad), Upload,
}
//idk how to make this a list of key-value pairs
#[derive(Args)]
struct AssetIDFileMapBad{
asset_ids:Vec<AssetID>,
files:Vec<std::path::PathBuf>,
} }
#[derive(Args)] #[derive(Args)]
@ -65,8 +76,8 @@ async fn main()->AResult<()>{
}; };
match cli.command{ match cli.command{
Commands::Download(asset_id_file_map)=>download_list(cookie,transpose_asset_id_file_map(asset_id_file_map)?).await, Commands::Download=>download_list(cookie,cli.asset_ids).await,
Commands::Upload(asset_id_file_map)=>upload_list(cookie,group,transpose_asset_id_file_map(asset_id_file_map)?).await, Commands::Upload=>upload_list(cookie,group,cli.asset_ids).await,
} }
} }
@ -98,16 +109,6 @@ fn maybe_gzip_decode<R:Read+Seek>(input:&mut R)->AResult<ReaderType<R>>{
} }
} }
type AssetIDFileMap=Vec<(AssetID,std::path::PathBuf)>;
fn transpose_asset_id_file_map(asset_id_file_map:AssetIDFileMapBad)->AResult<AssetIDFileMap>{
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<()>{ async fn upload_list(cookie:String,owner:Owner,asset_id_file_map:AssetIDFileMap)->AResult<()>{
let client=reqwest::Client::new(); let client=reqwest::Client::new();
futures::stream::iter(asset_id_file_map) futures::stream::iter(asset_id_file_map)