refactor: use match with cmp instead of bunch of if's

This commit is contained in:
Seymur Bagirov 2025-01-16 23:54:33 +04:00
parent 54912b7087
commit 2230c468e4

View File

@ -1,38 +1,42 @@
use std::{env::args_os, ffi::OsString, process::ExitCode}; use std::{cmp::Ordering, env::args_os, ffi::OsString, process::ExitCode};
use izanami::{run_file, run_prompt, RunError}; use izanami::{run_file, run_prompt, RunError};
fn main() -> ExitCode { fn main() -> ExitCode {
let args: Vec<OsString> = args_os().collect(); let args: Vec<OsString> = args_os().collect();
if args.len() > 2 { match args.len().cmp(&2) {
println!("usage: izanami [script]"); Ordering::Greater => {
return ExitCode::from(64); println!("usage: izanami [script]");
} else if args.len() == 2 { return ExitCode::from(64);
let result = run_file(args[1].to_str().unwrap());
if let Err(RunError::FileReadError(e)) = result {
println!("Couldn't read the file. Reason: {}", e);
return ExitCode::from(1);
}
if let Err(RunError::OtherError(e)) = result {
println!("Error occured. Error: {}", e);
return ExitCode::from(75);
} }
Ordering::Equal => {
let result = run_file(args[1].to_str().unwrap());
if let Err(RunError::RuntimeError(r)) = result { if let Err(RunError::FileReadError(e)) = result {
return ExitCode::from(70); println!("Couldn't read the file. Reason: {}", e);
} return ExitCode::from(1);
}
if let Err(RunError::OtherError(e)) = result {
println!("Error occured. Error: {}", e);
return ExitCode::from(75);
}
if let Err(RunError::ParseError) = result { if let Err(RunError::RuntimeError(r)) = result {
return ExitCode::from(75); return ExitCode::from(70);
} }
} else {
let result = run_prompt();
if let Err(res) = result { if let Err(RunError::ParseError) = result {
println!("Error while processing the repl. Reason: {}", &*res); return ExitCode::from(75);
return ExitCode::from(1); }
}
Ordering::Less => {
let result = run_prompt();
if let Err(res) = result {
println!("Error while processing the repl. Reason: {}", &*res);
return ExitCode::from(1);
}
} }
} }