forked from StrafesNET/map-tool
we Love errors
This commit is contained in:
parent
2cb346f49a
commit
742f7b4ec0
21
src/error.rs
Normal file
21
src/error.rs
Normal file
@ -0,0 +1,21 @@
|
||||
#[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<Self> to fit with the result in prelude.rs
|
||||
pub fn new(message: &str) -> Box<Self> {
|
||||
Box::new(Self {
|
||||
message: message.to_string(),
|
||||
})
|
||||
}
|
||||
}
|
37
src/main.rs
37
src/main.rs
@ -2,6 +2,14 @@ use std::unimplemented;
|
||||
|
||||
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<T, E> or just std::result::Result<T, E>
|
||||
// using the new result also means the error type is implicitly Box<dyn std::error::Error> (works for any errors that implement the std::error::Error trait)
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
#[command(propagate_version = true)]
|
||||
@ -87,7 +95,7 @@ fn get_script_refs(dom:&rbx_dom_weak::WeakDom) -> Vec<rbx_dom_weak::types::Ref>{
|
||||
scripts
|
||||
}
|
||||
|
||||
fn get_id() -> Result<u32, Box<dyn std::error::Error>>{
|
||||
fn get_id() -> Result<u32>{
|
||||
match std::fs::read_to_string("id"){
|
||||
Ok(id_file)=>Ok(id_file.parse::<u32>()?),
|
||||
Err(e) => match e.kind() {
|
||||
@ -97,7 +105,7 @@ fn get_id() -> Result<u32, Box<dyn std::error::Error>>{
|
||||
}
|
||||
}
|
||||
|
||||
fn get_set_from_file(file:&str) -> Result<std::collections::HashSet<String>, Box<dyn std::error::Error>>{
|
||||
fn get_set_from_file(file:&str) -> Result<std::collections::HashSet<String>>{
|
||||
let mut set=std::collections::HashSet::<String>::new();
|
||||
for entry in std::fs::read_dir(file)? {
|
||||
set.insert(std::fs::read_to_string(entry?.path())?);
|
||||
@ -105,15 +113,15 @@ fn get_set_from_file(file:&str) -> Result<std::collections::HashSet<String>, Box
|
||||
Ok(set)
|
||||
}
|
||||
|
||||
fn get_allowed_set() -> Result<std::collections::HashSet<String>, Box<dyn std::error::Error>>{
|
||||
fn get_allowed_set() -> Result<std::collections::HashSet<String>>{
|
||||
get_set_from_file("scripts/allowed")
|
||||
}
|
||||
|
||||
fn get_blocked() -> Result<std::collections::HashSet<String>, Box<dyn std::error::Error>>{
|
||||
fn get_blocked() -> Result<std::collections::HashSet<String>>{
|
||||
get_set_from_file("scripts/blocked")
|
||||
}
|
||||
|
||||
fn get_allowed_map() -> Result<std::collections::HashMap::<u32,String>, Box<dyn std::error::Error>>{
|
||||
fn get_allowed_map() -> Result<std::collections::HashMap::<u32,String>>{
|
||||
let mut allowed_map = std::collections::HashMap::<u32,String>::new();
|
||||
for entry in std::fs::read_dir("scripts/allowed")? {
|
||||
let entry=entry?;
|
||||
@ -122,7 +130,7 @@ fn get_allowed_map() -> Result<std::collections::HashMap::<u32,String>, Box<dyn
|
||||
Ok(allowed_map)
|
||||
}
|
||||
|
||||
fn get_replace_map() -> Result<std::collections::HashMap::<String,u32>, Box<dyn std::error::Error>>{
|
||||
fn get_replace_map() -> Result<std::collections::HashMap::<String,u32>>{
|
||||
let mut replace = std::collections::HashMap::<String,u32>::new();
|
||||
for entry in std::fs::read_dir("scripts/replace")? {
|
||||
let entry=entry?;
|
||||
@ -135,7 +143,7 @@ fn check_source_illegal_keywords(source:&String)->bool{
|
||||
source.find("getfenv").is_some()||source.find("require").is_some()
|
||||
}
|
||||
|
||||
fn download(map_list: Vec<u64>) -> Result<(), Box<dyn std::error::Error>>{
|
||||
fn download(map_list: Vec<u64>) -> Result<()>{
|
||||
let header=format!("Cookie: .ROBLOSECURITY={}",std::env::var("RBXCOOKIE")?);
|
||||
let shared_args=&[
|
||||
"-q",
|
||||
@ -158,7 +166,8 @@ enum Scan{
|
||||
Blocked,
|
||||
Flagged,
|
||||
}
|
||||
fn scan() -> Result<(), Box<dyn std::error::Error>>{
|
||||
|
||||
fn scan() -> Result<()>{
|
||||
let mut id = get_id()?;
|
||||
//Construct allowed scripts
|
||||
let allowed_set = get_allowed_set()?;
|
||||
@ -216,7 +225,7 @@ fn scan() -> Result<(), Box<dyn std::error::Error>>{
|
||||
std::fs::write("id",id.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
fn extract(file_id:u64) -> Result<(), Box<dyn std::error::Error>>{
|
||||
fn extract(file_id:u64) -> Result<()>{
|
||||
let mut id = 0;
|
||||
//Construct allowed scripts
|
||||
let mut script_set = std::collections::HashSet::<String>::new();
|
||||
@ -256,7 +265,7 @@ fn extract(file_id:u64) -> Result<(), Box<dyn std::error::Error>>{
|
||||
println!("extracted {} {}",id,if id==1 {"script"}else{"scripts"});
|
||||
Ok(())
|
||||
}
|
||||
fn replace() -> Result<(), Box<dyn std::error::Error>>{
|
||||
fn replace() -> Result<()>{
|
||||
let allowed_map=get_allowed_map()?;
|
||||
let replace_map=get_replace_map()?;
|
||||
|
||||
@ -304,7 +313,7 @@ fn replace() -> Result<(), Box<dyn std::error::Error>>{
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn upload() -> Result<(), Box<dyn std::error::Error>>{
|
||||
fn upload() -> Result<()>{
|
||||
//interactive prompt per upload:
|
||||
//Creator: [auto fill creator]
|
||||
//DisplayName: [auto fill DisplayName]
|
||||
@ -339,7 +348,7 @@ enum ScriptActionParseResult {
|
||||
struct ParseScriptActionErr;
|
||||
impl std::str::FromStr for ScriptActionParseResult {
|
||||
type Err=ParseScriptActionErr;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err>{
|
||||
fn from_str(s: &str) -> StdResult<Self, Self::Err>{
|
||||
if s=="pass\n"||s=="1\n"{
|
||||
Ok(Self::Pass)
|
||||
}else if s=="block\n"{
|
||||
@ -354,7 +363,7 @@ impl std::str::FromStr for ScriptActionParseResult {
|
||||
}
|
||||
}
|
||||
|
||||
fn interactive() -> Result<(), Box<dyn std::error::Error>>{
|
||||
fn interactive() -> Result<()>{
|
||||
let mut id=get_id()?;
|
||||
//Construct allowed scripts
|
||||
let mut allowed_set=get_allowed_set()?;
|
||||
@ -519,7 +528,7 @@ fn interactive() -> Result<(), Box<dyn std::error::Error>>{
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
match cli.command {
|
||||
Commands::Download(map_list)=>download(map_list.maps),
|
||||
|
19
src/prelude.rs
Normal file
19
src/prelude.rs
Normal file
@ -0,0 +1,19 @@
|
||||
pub use crate::error::Error;
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||
pub type StdResult<T, E> = std::result::Result<T, E>;
|
||||
|
||||
// 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)*));
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user