script action

This commit is contained in:
Quaternions 2024-12-21 22:16:45 -08:00
parent 0799aa311f
commit 86b892e614

View File

@ -29,6 +29,25 @@ async fn main(){
} }
} }
enum ScriptActionParseResult{
Pass,
Block,
Exit,
Delete,
}
struct ParseScriptActionErr;
impl std::str::FromStr for ScriptActionParseResult{
type Err=ParseScriptActionErr;
fn from_str(s:&str)->Result<Self,Self::Err>{
match s{
"pass\n"=>Ok(Self::Pass),
"block\n"=>Ok(Self::Block),
"exit\n"=>Ok(Self::Exit),
"delete\n"=>Ok(Self::Delete),
_=>Err(ParseScriptActionErr),
}
}
}
#[derive(Debug)] #[derive(Debug)]
enum ReviewError{ enum ReviewError{
Cookie(submissions_api::CookieError), Cookie(submissions_api::CookieError),
@ -37,6 +56,8 @@ enum ReviewError{
GetScriptFromHash(submissions_api::types::SingleItemError), GetScriptFromHash(submissions_api::types::SingleItemError),
NoScript, NoScript,
WriteCurrent(std::io::Error), WriteCurrent(std::io::Error),
ActionIO(std::io::Error),
ReadCurrent(std::io::Error),
} }
struct ReviewConfig{ struct ReviewConfig{
@ -57,17 +78,31 @@ async fn review(config:ReviewConfig)->Result<(),ReviewError>{
Policy:Some(submissions_api::types::Policy::None), Policy:Some(submissions_api::types::Policy::None),
}).await.map_err(ReviewError::GetPolicies)?; }).await.map_err(ReviewError::GetPolicies)?;
for policy in unreviewed_policies{ for unreviewed_policy in unreviewed_policies{
// download source code // download source code
let script_response=api.get_script_from_hash(submissions_api::types::HashRequest{ let script_response=api.get_script_from_hash(submissions_api::types::HashRequest{
hash:policy.FromScriptHash.as_str(), hash:unreviewed_policy.FromScriptHash.as_str(),
}).await }).await
.map_err(ReviewError::GetScriptFromHash)? .map_err(ReviewError::GetScriptFromHash)?
.ok_or(ReviewError::NoScript)?; .ok_or(ReviewError::NoScript)?;
let source=script_response.Source; let source=script_response.Source;
//load source into current.lua //load source into current.lua
tokio::fs::write("current.lua",source).await.map_err(ReviewError::WriteCurrent)?; tokio::fs::write("current.lua",source.as_str()).await.map_err(ReviewError::WriteCurrent)?;
//prompt action in terminal
//wait for input
let script_action;
loop{
print!("action: ");
std::io::Write::flush(&mut std::io::stdout()).map_err(ReviewError::ActionIO)?;
let mut action_string=String::new();
std::io::stdin().read_line(&mut action_string).map_err(ReviewError::ActionIO)?;
if let Ok(parsed_script_action)=action_string.parse::<ScriptActionParseResult>(){
script_action=parsed_script_action;
break;
}
}
} }
Ok(()) Ok(())