From d6dd1b8abde133d357524fa675e46291976f9309 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Sun, 1 Oct 2023 20:07:34 -0700 Subject: [PATCH] drop Jeftai Error, convert errors to anyhow --- src/error.rs | 21 ----------------- src/main.rs | 64 ++++++++++++++++++++++---------------------------- src/prelude.rs | 18 -------------- 3 files changed, 28 insertions(+), 75 deletions(-) delete mode 100644 src/error.rs delete mode 100644 src/prelude.rs diff --git a/src/error.rs b/src/error.rs deleted file mode 100644 index eac6475..0000000 --- a/src/error.rs +++ /dev/null @@ -1,21 +0,0 @@ -#[derive(Debug)] -pub struct Error { - pub message: String, -} - -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{}", self.message) - } -} - -impl std::error::Error for Error {} - -impl Error { - // has to be Box to fit with the result in prelude.rs - pub fn new(message: &str) -> Box { - Box::new(Self { - message: message.to_string(), - }) - } -} diff --git a/src/main.rs b/src/main.rs index 2c365f2..657dad4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,6 @@ use std::io::{Read, Seek}; - use clap::{Args, Parser, Subcommand}; - -mod error; -mod prelude; - -// this * means we are importing everything from the prelude module and in turn we overwrite the default `Result` with our own -// if you want the original back you can use StdResult or just std::result::Result -// using the new result also means the error type is implicitly Box (works for any errors that implement the std::error::Error trait) -use crate::prelude::*; +use anyhow::Result as AResult; #[derive(Parser)] #[command(author, version, about, long_about = None)] @@ -105,25 +97,25 @@ fn get_texture_refs(dom:&rbx_dom_weak::WeakDom) -> Vec objects } -fn get_dom(input:&mut R)->Result{ +fn get_dom(input:&mut R)->AResult{ let mut first_8=[0u8;8]; if let (Ok(()),Ok(()))=(std::io::Read::read_exact(input, &mut first_8),std::io::Seek::rewind(input)){ match &first_8[0..4]{ b"{ match &first_8[4..8]{ - b"lox!"=>rbx_binary::from_reader(input).map_err(|e|format!("{:?}",e)), - b"lox "=>rbx_xml::from_reader(input,rbx_xml::DecodeOptions::default()).map_err(|e|format!("{:?}",e)), - other=>Err(format!("Unknown Roblox file type {:?}",other)), + b"lox!"=>return rbx_binary::from_reader(input).map_err(anyhow::Error::msg), + b"lox "=>return rbx_xml::from_reader(input,rbx_xml::DecodeOptions::default()).map_err(anyhow::Error::msg), + other=>Err(anyhow::Error::msg(format!("Unknown Roblox file type {:?}",other))), } } - _=>Err("unsupported file type".to_owned()), + _=>Err(anyhow::Error::msg("unsupported file type")), } }else{ - Err("peek failed".to_owned()) + Err(anyhow::Error::msg("peek failed")) } } -fn get_id() -> BoxResult{ +fn get_id() -> AResult{ match std::fs::read_to_string("id"){ Ok(id_file)=>Ok(id_file.parse::()?), Err(e) => match e.kind() { @@ -133,7 +125,7 @@ fn get_id() -> BoxResult{ } } -fn get_set_from_file(file:&str) -> BoxResult>{ +fn get_set_from_file(file:&str) -> AResult>{ let mut set=std::collections::HashSet::::new(); for entry in std::fs::read_dir(file)? { set.insert(std::fs::read_to_string(entry?.path())?); @@ -141,15 +133,15 @@ fn get_set_from_file(file:&str) -> BoxResult>{ Ok(set) } -fn get_allowed_set() -> BoxResult>{ +fn get_allowed_set() -> AResult>{ get_set_from_file("scripts/allowed") } -fn get_blocked() -> BoxResult>{ +fn get_blocked() -> AResult>{ get_set_from_file("scripts/blocked") } -fn get_allowed_map() -> BoxResult>{ +fn get_allowed_map() -> AResult>{ let mut allowed_map = std::collections::HashMap::::new(); for entry in std::fs::read_dir("scripts/allowed")? { let entry=entry?; @@ -158,7 +150,7 @@ fn get_allowed_map() -> BoxResult>{ Ok(allowed_map) } -fn get_replace_map() -> BoxResult>{ +fn get_replace_map() -> AResult>{ let mut replace = std::collections::HashMap::::new(); for entry in std::fs::read_dir("scripts/replace")? { let entry=entry?; @@ -182,10 +174,10 @@ fn find_first_child_class<'a>(dom:&'a rbx_dom_weak::WeakDom,instance:&'a rbx_dom None } -fn get_mapinfo(dom:&rbx_dom_weak::WeakDom) -> BoxResult<(String,String,String)>{ +fn get_mapinfo(dom:&rbx_dom_weak::WeakDom) -> AResult<(String,String,String)>{ let workspace_children=dom.root().children(); if workspace_children.len()!=1{ - return Err(Error::new("there can only be one model")); + return Err(anyhow::Error::msg("there can only be one model")); } if let Some(model_instance) = dom.get_by_ref(workspace_children[0]) { if let (Some(creator),Some(displayname))=(find_first_child_class(dom, model_instance, "Creator", "StringValue"),find_first_child_class(dom, model_instance, "DisplayName", "StringValue")){ @@ -200,10 +192,10 @@ fn get_mapinfo(dom:&rbx_dom_weak::WeakDom) -> BoxResult<(String,String,String)>{ } } } - return Err(Error::new("no stuff in map")); + return Err(anyhow::Error::msg("no stuff in map")); } -fn download(map_list: Vec) -> BoxResult<()>{ +fn download(map_list: Vec) -> AResult<()>{ let header=format!("Cookie: .ROBLOSECURITY={}",std::env::var("RBXCOOKIE")?); let shared_args=&[ "-q", @@ -268,7 +260,7 @@ SurfaceAppearance.NormalMap SurfaceAppearance.RoughnessMap SurfaceAppearance.TexturePack */ -fn download_textures(paths: Vec) -> BoxResult<()>{ +fn download_textures(paths: Vec) -> AResult<()>{ println!("download_textures paths:{:?}",paths); let header=format!("Cookie: .ROBLOSECURITY={}",std::env::var("RBXCOOKIE")?); let shared_args=&[ @@ -314,7 +306,7 @@ fn download_textures(paths: Vec) -> BoxResult<()>{ Ok(()) } -fn convert(file_thing:std::fs::DirEntry) -> BoxResult<()>{ +fn convert(file_thing:std::fs::DirEntry) -> AResult<()>{ let mut input = std::io::BufReader::new(std::fs::File::open(file_thing.path())?); let mut fourcc=[0u8;4]; input.read_exact(&mut fourcc)?; @@ -330,7 +322,7 @@ fn convert(file_thing:std::fs::DirEntry) -> BoxResult<()>{ extracted_input=Some(extracted.clone()); image::load(std::io::Cursor::new(extracted),image::ImageFormat::Png)?.to_rgba8() }else{ - Err(error::Error::new("Unknown texture format"))? + return Err(anyhow::Error::msg("Unknown texture format")); }; let format=if image.width()%4!=0||image.height()%4!=0{ @@ -368,7 +360,7 @@ fn convert(file_thing:std::fs::DirEntry) -> BoxResult<()>{ } Ok(()) } -fn convert_textures() -> BoxResult<()>{ +fn convert_textures() -> AResult<()>{ let start = std::time::Instant::now(); let mut threads=Vec::new(); for entry in std::fs::read_dir("textures/unprocessed")? { @@ -401,7 +393,7 @@ enum Scan{ Flagged, } -fn scan() -> BoxResult<()>{ +fn scan() -> AResult<()>{ let mut id = get_id()?; //Construct allowed scripts let allowed_set = get_allowed_set()?; @@ -460,7 +452,7 @@ fn scan() -> BoxResult<()>{ Ok(()) } -fn extract(paths: Vec) -> BoxResult<()>{ +fn extract(paths: Vec) -> AResult<()>{ let mut id = 0; //Construct allowed scripts let mut script_set = std::collections::HashSet::::new(); @@ -495,7 +487,7 @@ fn extract(paths: Vec) -> BoxResult<()>{ println!("extracted {} {}",id,if id==1 {"script"}else{"scripts"}); Ok(()) } -fn replace() -> BoxResult<()>{ +fn replace() -> AResult<()>{ let allowed_map=get_allowed_map()?; let replace_map=get_replace_map()?; @@ -568,7 +560,7 @@ impl std::str::FromStr for UploadAction { } } -fn upload() -> BoxResult<()>{ +fn upload() -> AResult<()>{ //interactive prompt per upload: for entry in std::fs::read_dir("maps/passed")? { let file_thing=entry?; @@ -677,7 +669,7 @@ impl std::str::FromStr for ScriptActionParseResult { } } -fn interactive() -> BoxResult<()>{ +fn interactive() -> AResult<()>{ let mut id=get_id()?; //Construct allowed scripts let mut allowed_set=get_allowed_set()?; @@ -824,7 +816,7 @@ fn interactive() -> BoxResult<()>{ //write workspace:GetChildren()[1] let workspace_children=dom.root().children(); if workspace_children.len()!=1{ - return Err(Error::new("there can only be one model")); + return Err(anyhow::Error::msg("there can only be one model")); } rbx_binary::to_writer(output, &dom, &[workspace_children[0]])?; //move original to processed folder @@ -847,7 +839,7 @@ fn interactive() -> BoxResult<()>{ Ok(()) } -fn main() -> BoxResult<()> { +fn main() -> AResult<()> { let cli = Cli::parse(); match cli.command { Commands::Download(map_list)=>download(map_list.maps), diff --git a/src/prelude.rs b/src/prelude.rs deleted file mode 100644 index e4d7bac..0000000 --- a/src/prelude.rs +++ /dev/null @@ -1,18 +0,0 @@ -pub use crate::error::Error; - -pub type BoxResult = std::result::Result>; - -// i just wanted to mess around with macros a bit -// so heres labelprint as a macro -#[macro_export] -macro_rules! lprint { - ($expr:expr) => {{ - let ___this_file = std::file!(); - let ___line = std::line!(); - // let ___column = column!(); - println!("[{}:{}] {}", ___this_file, ___line, $expr); - }}; - ($expr:expr, $($arg:tt)*) => {{ - lprint!(format!($expr, $($arg)*)); - }}; -}