enormous code for custom logic

This commit is contained in:
Quaternions 2024-01-24 16:51:19 -08:00
parent d9531f1d4e
commit 56899fa7da

View File

@ -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<DecompileStyle>)->AResult<CompileNode>{
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?,
})
}