refactor: exit codes

This commit is contained in:
iff 2024-12-06 19:12:40 +01:00
parent dae09adb76
commit a8561ed488
5 changed files with 91 additions and 70 deletions

View file

@ -37,28 +37,39 @@ mod requests;
extern crate rust_i18n;
i18n!("i18n", fallback = "en", minify_key = true);
fn main() -> Result<(), std::io::Error>{
fn main() -> Result<(), std::io::Error> {
colored::control::set_override(true);
let mut data = {
let init = init();
if init.is_none() {
return Ok(());
} else {
init.unwrap()
let init = init();
let mut data = if let Err(status) = init {
match status {
args::Status::Exit => {
return Ok(());
}
args::Status::Error => {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Invalid input",
));
}
_ => {
unreachable!()
}
}
} else {
init.ok().unwrap()
};
data.expand_command();
use shell::Mode;
match data.mode {
Mode::Suggestion => modes::suggestion(&mut data),
Mode::CNF => modes::cnf(&mut data),
Mode::Cnf => modes::cnf(&mut data),
}
Ok(())
}
fn init() -> Option<shell::Data> {
fn init() -> Result<shell::Data, args::Status> {
let locale = {
let sys_locale = get_locale().unwrap_or("en-US".to_string());
if sys_locale.len() < 2 {
@ -69,9 +80,15 @@ fn init() -> Option<shell::Data> {
};
rust_i18n::set_locale(&locale[0..2]);
let exit = args::handle_args();
if exit {
return None;
let status = args::handle_args();
match status {
args::Status::Exit => {
return Err(status);
}
args::Status::Error => {
return Err(status);
}
_ => {}
}
#[cfg(feature = "request-ai")]
@ -81,5 +98,5 @@ fn init() -> Option<shell::Data> {
}
}
Some(shell::Data::init())
Ok(shell::Data::init())
}