Skip to content

Commit

Permalink
better error handling in mass decode and encode
Browse files Browse the repository at this point in the history
  • Loading branch information
cqb13 committed Jul 30, 2024
1 parent 81a9ffd commit 7f2edae
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 32 deletions.
87 changes: 70 additions & 17 deletions src/commands/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::fs;
use std::io::Write;
use std::path::Path;

//TODO: on mass decoding/encoding if the error Quit is returned, the program should continue to the next file, if the error is not Quit, the program should exit
pub fn decode_command(
input_path_string: String,
output_path_string: Option<String>,
Expand All @@ -22,16 +21,24 @@ pub fn decode_command(
};

if !mass {
let program = decode_file(input_path, display_mode, content, preview);
let program = match decode_file(input_path, display_mode, content, preview) {
Ok(program) => program,
Err(err) => {
err.print().exit();
}
};

let name = program.metadata.name.to_string();

if output_path_string.is_some() {
save_file(
match save_file(
program,
Path::new(&output_path_string.unwrap()),
display_mode_string.as_str(),
)
) {
Ok(_) => (),
Err(err) => err.print().exit(),
}
}

println!("Successfully converted {} to txt", name)
Expand All @@ -53,15 +60,15 @@ pub fn decode_command(
std::io::stdout().flush().unwrap();
std::io::stdin().read_line(&mut input).unwrap();
let input = input.trim();
if input == "y" || input == "Y" {
println!("Creating output directory");
fs::create_dir(Path::new(&output_path_string.as_ref().unwrap()))
.expect("Failed to create directory")
} else {
if input != "y" && input != "Y" {
CliError::Quit("Missing output directory".to_string())
.print()
.exit()
}

println!("Creating output directory");
fs::create_dir(Path::new(&output_path_string.as_ref().unwrap()))
.expect("Failed to create directory")
}

if !Path::new(&output_path_string.as_ref().unwrap()).is_dir() {
Expand Down Expand Up @@ -90,7 +97,28 @@ pub fn decode_command(
Err(_) => continue,
};

let program = decode_file(&path, display_mode.clone(), content, preview);
let program = match decode_file(&path, display_mode.clone(), content, preview) {
Ok(program) => program,
Err(err) => {
println!("Failed to decode file:");
err.print();

println!("Would you like to skip this file and continue? [y/N]");
let mut input = String::new();
print!("> ");
input.clear();
std::io::stdout().flush().unwrap();
std::io::stdin().read_line(&mut input).unwrap();
let input = input.trim();
if input != "y" && input != "Y" {
CliError::Quit("User chose to quit".to_string())
.print()
.exit()
}

continue;
}
};

let name = program.metadata.name.to_string();

Expand All @@ -99,7 +127,28 @@ pub fn decode_command(
.join(&name)
.with_extension("txt");

save_file(program, output_path.as_path(), display_mode_string.as_str())
match save_file(program, output_path.as_path(), display_mode_string.as_str()) {
Ok(_) => (),
Err(err) => {
println!("Failed to save file:");
err.print();

println!("Would you like to skip this file and continue? [y/N]");
let mut input = String::new();
print!("> ");
input.clear();
std::io::stdout().flush().unwrap();
std::io::stdin().read_line(&mut input).unwrap();
let input = input.trim();
if input != "y" && input != "Y" {
CliError::Quit("User chose to quit".to_string())
.print()
.exit()
}

continue;
}
}
}

println!("Successfully converted {} to txt", name)
Expand All @@ -112,12 +161,12 @@ fn decode_file(
display_mode: DisplayMode,
content: bool,
preview: bool,
) -> Program {
) -> Result<Program, CliError> {
let program = Program::load_from_8xp(input_path.to_path_buf(), display_mode);

let program = match program {
Ok(program) => program,
Err(err) => err.print().exit(),
Err(err) => return Err(err),
};

if content {
Expand All @@ -136,19 +185,23 @@ fn decode_file(
println!();
}

program
Ok(program)
}

fn save_file(program: Program, output_path: &Path, display_mode_string: &str) {
fn save_file(
program: Program,
output_path: &Path,
display_mode_string: &str,
) -> Result<(), CliError> {
let result = program.save_to(output_path.to_path_buf());

if display_mode_string == "pretty" {
println!("Warning: Pretty tokens can't be accurately encoded")
}

match result {
Ok(_) => {}
Err(err) => err.print().exit(),
Ok(_) => Ok(()),
Err(err) => Err(err),
}
}

Expand Down
81 changes: 66 additions & 15 deletions src/commands/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ pub fn encode_command(
let input_path = Path::new(&input_path_string);

if !mass {
let program = encode_file(input_path, &encode_mode, content, preview);
let program = match encode_file(&input_path, &encode_mode, content, preview) {
Ok(program) => program,
Err(err) => {
err.print().exit();
}
};

let name = program.metadata.name.to_string();

if output_path_string.is_some() {
save_file(program, Path::new(&output_path_string.unwrap()))
match save_file(program, Path::new(&output_path_string.unwrap())) {
Ok(_) => (),
Err(err) => err.print().exit(),
}
}

println!("Successfully converted {} to 8xp", name)
Expand All @@ -48,15 +56,16 @@ pub fn encode_command(
std::io::stdout().flush().unwrap();
std::io::stdin().read_line(&mut input).unwrap();
let input = input.trim();
if input == "y" || input == "Y" {
println!("Creating output directory");
fs::create_dir(Path::new(&output_path_string.as_ref().unwrap()))
.expect("Failed to create directory")
} else {
if input != "y" && input != "Y" {
println!("{}", input);
CliError::Quit("Missing output directory".to_string())
.print()
.exit()
}

println!("Creating output directory");
fs::create_dir(Path::new(&output_path_string.as_ref().unwrap()))
.expect("Failed to create directory")
}

if !Path::new(&output_path_string.as_ref().unwrap()).is_dir() {
Expand Down Expand Up @@ -85,7 +94,28 @@ pub fn encode_command(
Err(_) => continue,
};

let program = encode_file(&path, &encode_mode, content, preview);
let program = match encode_file(&path, &encode_mode, content, preview) {
Ok(program) => program,
Err(err) => {
println!("Failed to encode file:");
err.print();

println!("Would you like to skip this file and continue? [y/N]");
let mut input = String::new();
print!("> ");
input.clear();
std::io::stdout().flush().unwrap();
std::io::stdin().read_line(&mut input).unwrap();
let input = input.trim();
if input != "y" && input != "Y" {
CliError::Quit("User chose to quit".to_string())
.print()
.exit()
}

continue;
}
};

let name = program.metadata.name.to_string();

Expand All @@ -94,7 +124,28 @@ pub fn encode_command(
.join(&name)
.with_extension("8xp");

save_file(program, output_path.as_path())
match save_file(program, output_path.as_path()) {
Ok(_) => (),
Err(err) => {
println!("Failed to save file:");
err.print();

println!("Would you like to skip this file and continue? [y/N]");
let mut input = String::new();
print!("> ");
input.clear();
std::io::stdout().flush().unwrap();
std::io::stdin().read_line(&mut input).unwrap();
let input = input.trim();
if input != "y" && input != "Y" {
CliError::Quit("User chose to quit".to_string())
.print()
.exit()
}

continue;
}
}
}

println!("Successfully converted {} to txt", name)
Expand All @@ -107,12 +158,12 @@ fn encode_file(
encode_mode: &EncodeMode,
content: bool,
preview: bool,
) -> Program {
) -> Result<Program, CliError> {
let program = Program::load_from_txt(input_path.to_path_buf(), encode_mode);

let program = match program {
Ok(program) => program,
Err(err) => err.print().exit(),
Err(err) => return Err(err),
};

if content {
Expand All @@ -131,15 +182,15 @@ fn encode_file(
println!();
}

program
Ok(program)
}

fn save_file(program: Program, output_path: &Path) {
fn save_file(program: Program, output_path: &Path) -> Result<(), CliError> {
let result = program.save_to(output_path.to_path_buf());

match result {
Ok(_) => {}
Err(err) => err.print().exit(),
Ok(_) => Ok(()),
Err(err) => Err(err),
}
}

Expand Down

0 comments on commit 7f2edae

Please sign in to comment.