From 1dff5d6856648dd0d3c36fc66b787fc4d465dbd1 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 8 Mar 2024 09:35:10 -0800 Subject: [PATCH] rewrite clap usage --- src/main.rs | 290 ++++++++++++++++++++++++++-------------------------- 1 file changed, 145 insertions(+), 145 deletions(-) diff --git a/src/main.rs b/src/main.rs index 288dac7..6fc27e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,81 +14,103 @@ const CONCURRENT_REQUESTS:usize=32; #[command(author,version,about,long_about=None)] #[command(propagate_version = true)] struct Cli{ - //asset options - #[arg(short,long)] - group:Option, - #[arg(long)] - asset_id:Option, - //idk how to do this better - #[arg(long)] - cookie_literal:Option, - #[arg(long)] - cookie_env:Option, - #[arg(long)] - cookie_file:Option, - //TODO: read the versions.json file instead of doing this - //TODO: write file dates instead of versions.json - #[arg(long)] - start_version:Option, - #[arg(long)] - end_version:Option, - #[arg(long)] - r#continue:bool, - - //decompile options - #[arg(long)] - no_models:Option, - #[arg(long)] - no_scripts:Option, - #[arg(long)] - no_template:Option, - #[arg(long)] - style:Option, - - //git options - #[arg(long)] - git_committer_name:Option, - #[arg(long)] - git_committer_email:Option, - - #[arg(short,long)] - input:Option, - - #[arg(short,long)] - output:Option, - #[command(subcommand)] command:Commands, } #[derive(Subcommand)] enum Commands{ - DownloadHistory, - Download(AssetIDList), - Upload, - Compile, - Decompile, - DecompileHistoryIntoGit, - DownloadAndDecompileHistoryIntoGit, + DownloadHistory(DownloadHistorySubcommand), + Download(DownloadSubcommand), + Upload(UploadSubcommand), + Compile(CompileSubcommand), + Decompile(DecompileSubcommand), + DecompileHistoryIntoGit(DecompileHistoryIntoGitSubcommand), + DownloadAndDecompileHistoryIntoGit(DownloadAndDecompileHistoryIntoGitSubcommand), } -#[derive(Clone,Copy,Debug)] +#[derive(Args)] +struct DownloadHistorySubcommand{ + asset_id:AssetID, + cookie_type:CookieType, + cookie:String, + output_folder:Option, + continue_from_versions:bool, + start_version:Option, + end_version:Option, +} +#[derive(Args)] +struct DownloadSubcommand{ + cookie_type:CookieType, + cookie:String, + output_folder:Option, + asset_id_list:Vec, +} +#[derive(Args)] +struct UploadSubcommand{ + asset_id:AssetID, + cookie_type:CookieType, + cookie:String, + input_file:PathBuf, + group:Option, +} +#[derive(Args)] +struct CompileSubcommand{ + input_folder:PathBuf, + output_file:PathBuf, + style:Option, + template:Option, +} +#[derive(Args)] +struct DecompileSubcommand{ + input_file:PathBuf, + output_folder:PathBuf, + style:DecompileStyle, + write_template:bool, + write_models:bool, + write_scripts:bool, +} +#[derive(Args)] +struct DecompileHistoryIntoGitSubcommand{ + input_folder:PathBuf, + //currently output folder must be the current folder due to git2 limitations + //output_folder:cli.output.unwrap(), + style:DecompileStyle, + git_committer_name:String, + git_committer_email:String, + write_template:bool, + write_models:bool, + write_scripts:bool, +} +#[derive(Args)] +struct DownloadAndDecompileHistoryIntoGitSubcommand{ + asset_id:AssetID, + cookie_type:CookieType, + cookie:String, + //currently output folder must be the current folder due to git2 limitations + //output_folder:cli.output.unwrap(), + style:DecompileStyle, + git_committer_name:String, + git_committer_email:String, + write_template:bool, + write_models:bool, + write_scripts:bool, +} + +#[derive(Clone,clap::ValueEnum)] +enum CookieType{ + Literal, + Environment, + File, +} + +#[derive(Clone,Copy,Debug,clap::ValueEnum)] enum DecompileStyle{ Rox, Rojo, RoxRojo, } -#[derive(Args)] -struct AssetIDList{ - asset_ids:Vec -} - -#[derive(Args)] -struct PathBufList{ - paths:Vec -} - #[derive(serde::Deserialize)] #[allow(nonstandard_style,dead_code)] struct VersionPage{ @@ -112,100 +134,78 @@ struct AssetVersion{ #[tokio::main] async fn main()->AResult<()>{ let cli=Cli::parse(); - - let cookie_enum={ - match (cli.cookie_literal,cli.cookie_env,cli.cookie_file){ - (Some(literal),None,None)=>Some(Cookie::Literal(literal)), - (None,Some(env_var),None)=>Some(Cookie::Environment(env_var)), - (None,None,Some(path))=>Some(Cookie::File(path)), - (None,None,None)=>None, - _=>Err(anyhow::Error::msg("Cookie was specified multiple times."))?, - } - }; - let cookie=match cookie_enum{ - Some(c)=>Some(format!(".ROBLOSECURITY={}",match c{ - Cookie::Literal(s)=>s, - Cookie::Environment(var)=>std::env::var(var)?, - Cookie::File(path)=>tokio::fs::read_to_string(path).await?, - })), - None=>None, - }; - - let decompile_style=match cli.style.as_deref(){ - Some("rox") - |Some("Rox")=>Some(DecompileStyle::Rox), - Some("rojo") - |Some("Rojo")=>Some(DecompileStyle::Rojo), - Some("rox-rojo") - |Some("rojo-rox") - |Some("roxrojo") - |Some("rojorox") - |Some("RoxRojo") - |Some("RojoRox")=>Some(DecompileStyle::RoxRojo), - None=>None, - _=>Err(anyhow::Error::msg("Invalid style"))?, - }; - match cli.command{ - Commands::DownloadHistory=>download_history(DownloadHistoryConfig{ - continue_from_versions:cli.r#continue, - end_version:cli.end_version, - start_version:cli.start_version.unwrap_or(0), - output_folder:cli.output.unwrap(), - cookie:cookie.unwrap(), - asset_id:cli.asset_id.unwrap(), + Commands::DownloadHistory(subcommand)=>download_history(DownloadHistoryConfig{ + continue_from_versions:subcommand.continue_from_versions, + end_version:subcommand.end_version, + start_version:subcommand.start_version.unwrap_or(0), + output_folder:subcommand.output_folder.unwrap_or_else(||std::env::current_dir().unwrap()), + cookie:Cookie::from_type(subcommand.cookie_type,subcommand.cookie).await?.0, + asset_id:subcommand.asset_id, }).await, - Commands::Download(asset_id_list)=>download_list( - cookie.unwrap(), - asset_id_list.asset_ids.into_iter().map(|asset_id|{ - let mut path=cli.output.clone().unwrap(); - path.push(asset_id.to_string()); - (asset_id,path) - }).collect() + Commands::Download(subcommand)=>{ + let output_folder=subcommand.output_folder.unwrap_or_else(||std::env::current_dir().unwrap()); + download_list( + Cookie::from_type(subcommand.cookie_type,subcommand.cookie).await?.0, + subcommand.asset_id_list.into_iter().map(|asset_id|{ + let mut path=output_folder.clone(); + path.push(asset_id.to_string()); + (asset_id,path) + }).collect() + ).await + }, + Commands::Upload(subcommand)=>upload_list( + Cookie::from_type(subcommand.cookie_type,subcommand.cookie).await?.0, + subcommand.group, + vec![(subcommand.asset_id,subcommand.input_file)] ).await, - Commands::Upload=>upload_list(cookie.unwrap(),cli.group,vec![(cli.asset_id.unwrap(),cli.output.unwrap())]).await, - Commands::Compile=>compile(CompileConfig{ - input_folder:cli.input.unwrap(), - output_file:cli.output.unwrap(), - template:None, - style:None, + Commands::Compile(subcommand)=>compile(CompileConfig{ + input_folder:subcommand.input_folder, + output_file:subcommand.output_file, + template:subcommand.template, + style:subcommand.style, }).await, - Commands::Decompile=>decompile(DecompileConfig{ - style:decompile_style.unwrap(), - input_file:cli.input.unwrap(), - output_folder:cli.output.unwrap(), - write_template:!cli.no_template.unwrap_or(false), - write_models:!cli.no_models.unwrap_or(false), - write_scripts:!cli.no_scripts.unwrap_or(false), + Commands::Decompile(subcommand)=>decompile(DecompileConfig{ + style:subcommand.style, + input_file:subcommand.input_file, + output_folder:subcommand.output_folder, + write_template:subcommand.write_template, + write_models:subcommand.write_models, + write_scripts:subcommand.write_scripts, }).await, - Commands::DecompileHistoryIntoGit=>decompile_history_into_git(DecompileHistoryConfig{ - git_committer_name:cli.git_committer_name.unwrap(), - git_committer_email:cli.git_committer_email.unwrap(), - input_folder:cli.input.unwrap(), - output_folder:cli.output.unwrap(), - style:decompile_style.unwrap(), - write_template:!cli.no_template.unwrap_or(false), - write_models:!cli.no_models.unwrap_or(false), - write_scripts:!cli.no_scripts.unwrap_or(false), + Commands::DecompileHistoryIntoGit(subcommand)=>decompile_history_into_git(DecompileHistoryConfig{ + git_committer_name:subcommand.git_committer_name, + git_committer_email:subcommand.git_committer_email, + input_folder:subcommand.input_folder, + output_folder:std::env::current_dir()?, + style:subcommand.style, + write_template:subcommand.write_template, + write_models:subcommand.write_models, + write_scripts:subcommand.write_scripts, }).await, - Commands::DownloadAndDecompileHistoryIntoGit=>download_and_decompile_history_into_git(DownloadAndDecompileHistoryConfig{ - git_committer_name:cli.git_committer_name.unwrap(), - git_committer_email:cli.git_committer_email.unwrap(), - cookie:cookie.unwrap(), - asset_id:cli.asset_id.unwrap(), - output_folder:cli.output.unwrap(), - style:decompile_style.unwrap(), - write_template:!cli.no_template.unwrap_or(false), - write_models:!cli.no_models.unwrap_or(false), - write_scripts:!cli.no_scripts.unwrap_or(false), + Commands::DownloadAndDecompileHistoryIntoGit(subcommand)=>download_and_decompile_history_into_git(DownloadAndDecompileHistoryConfig{ + git_committer_name:subcommand.git_committer_name, + git_committer_email:subcommand.git_committer_email, + cookie:Cookie::from_type(subcommand.cookie_type,subcommand.cookie).await?.0, + asset_id:subcommand.asset_id, + output_folder:std::env::current_dir()?, + style:subcommand.style, + write_template:subcommand.write_template, + write_models:subcommand.write_models, + write_scripts:subcommand.write_scripts, }).await, } } -enum Cookie{ - Literal(String), - Environment(String), - File(PathBuf), +struct Cookie(String); +impl Cookie{ + async fn from_type(cookie_type:CookieType,cookie_string:String)->AResult{ + Ok(Self(format!(".ROBLOSECURITY={}",match cookie_type{ + CookieType::Literal=>cookie_string, + CookieType::Environment=>std::env::var(cookie_string)?, + CookieType::File=>tokio::fs::read_to_string(cookie_string).await?, + }))) + } } enum ReaderType{