From 56899fa7daa0728ff914c8894811299ea4bafa80 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 24 Jan 2024 16:51:19 -0800 Subject: [PATCH] enormous code for custom logic --- src/main.rs | 70 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6774f97..4a0a142 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,7 +72,7 @@ enum Commands{ DownloadAndDecompileHistoryIntoGit, } -#[derive(Clone,Copy)] +#[derive(Clone,Copy,Debug)] enum DecompileStyle{ Rox, Rojo, @@ -1355,36 +1355,52 @@ enum FileDiscernment{ } async fn discern_file(entry:&tokio::fs::DirEntry,style:Option)->AResult{ - let path=entry.path(); - let (ext_len,file_discernment)=match path.extension(){ - Some(extension)=>{ - let ext=extension.to_str().unwrap_or(""); - (ext.len(),match ext{ - "lua" - |"module.lua"=>FileDiscernment::Script(ScriptHint::ModuleScript), - "client.lua"=>FileDiscernment::Script(ScriptHint::LocalScript), - "server.lua"=>FileDiscernment::Script(ScriptHint::Script), - "rbxmx"=>FileDiscernment::Model, - other=>Err(anyhow::Error::msg(format!("Weird file extension: {other:?}")))?, - }) - }, - None=>Err(anyhow::Error::msg("No file extension"))?, - }; - //reject goobers - match (style,&file_discernment){ - (Some(DecompileStyle::Rojo),FileDiscernment::Script(ScriptHint::ModuleScript)) - |(Some(DecompileStyle::Rojo),FileDiscernment::Model)=>Err(anyhow::Error::msg("Invalid file extension for style"))?, - _=>(), - } - let mut search_name=entry + let mut file_name=entry .file_name() .into_string() .map_err(|e|anyhow::Error::msg(format!("insane file name {e:?}")))?; - search_name.truncate(search_name.len()-ext_len-1);//remove period - let file=tokio::fs::File::open(path.as_path()).await?; + //reject goobers + let is_goober=match style{ + Some(DecompileStyle::Rojo)=>true, + _=>false, + }; + let (ext_len,file_discernment)={ + if let Some(captures)=lazy_regex::regex!(r"^.*(.module.lua|.client.lua|.server.lua)$") + .captures(file_name.as_str()){ + let ext=&captures[1]; + (ext.len(),match ext{ + ".module.lua"=>{ + if is_goober{ + Err(anyhow::Error::msg(format!("File extension {ext} not supported in style {style:?}")))?; + } + FileDiscernment::Script(ScriptHint::ModuleScript) + }, + ".client.lua"=>FileDiscernment::Script(ScriptHint::LocalScript), + ".server.lua"=>FileDiscernment::Script(ScriptHint::Script), + _=>panic!("Regex failed"), + }) + }else if let Some(captures)=lazy_regex::regex!(r"^.*(.rbxmx|.lua)$") + .captures(file_name.as_str()){ + let ext=&captures[1]; + (ext.len(),match ext{ + ".rbxmx"=>{ + if is_goober{ + Err(anyhow::Error::msg(format!("File extension {ext} not supported in style {style:?}")))?; + } + FileDiscernment::Model + }, + ".lua"=>FileDiscernment::Script(ScriptHint::ModuleScript), + _=>panic!("Regex failed"), + }) + }else{ + return Err(anyhow::Error::msg("No file extension")); + } + }; + file_name.truncate(file_name.len()-ext_len); + let file=tokio::fs::File::open(entry.path()).await?; Ok(match file_discernment{ - FileDiscernment::Model=>model_node(search_name.as_str(),file).await?, - FileDiscernment::Script(hint)=>script_node(search_name.as_str(),file,hint).await?, + FileDiscernment::Model=>model_node(file_name.as_str(),file).await?, + FileDiscernment::Script(hint)=>script_node(file_name.as_str(),file,hint).await?, }) }