Skip to content

Commit

Permalink
refactor(tauri): change to a simplified stmt
Browse files Browse the repository at this point in the history
  • Loading branch information
SARDONYX-sard committed Oct 7, 2023
1 parent d0aeca3 commit 564598c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
7 changes: 3 additions & 4 deletions dar2oar_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::{arg, Parser};
use dar2oar_core::{convert_dar_to_oar, read_mapping_table};
use std::path::PathBuf;

/// dar2oar --src "DAR path" --src "OAR path" --name "" --author ""
/// dar2oar --src "DAR path" --dist "OAR path"
#[derive(Debug, Parser)]
#[command(version, about)]
pub struct Args {
Expand Down Expand Up @@ -66,7 +66,6 @@ pub fn run_cli(args: Args) -> anyhow::Result<()> {
args.name.as_deref(),
args.author.as_deref(),
table,
table_1person
)?;
Ok(())
table_1person,
)
}
67 changes: 38 additions & 29 deletions src-tauri/src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
use dar2oar_core::{convert_dar_to_oar, read_mapping_table};
use std::path::Path;

/// early return with Err() and write log error.
macro_rules! bail {
($err:expr) => {{
log::error!("{}", $err);
return Err($err.to_string());
}};
}

macro_rules! try_get_mapping_table {
($mapping_path:ident) => {
match $mapping_path {
Some(ref table_path) => {
let mapping = match read_mapping_table(table_path) {
Ok(table) => table,
Err(err) => bail!(err),
};
Some(mapping)
}
None => None,
}
};
}

#[tauri::command]
pub fn convert_dar2oar(
dar_mod_folder: &str,
Expand All @@ -15,42 +38,28 @@ pub fn convert_dar2oar(
true => None,
false => Some(Path::new(dist).to_path_buf()),
});
let table = match mapping_path {
Some(ref table_path) => {
let mapping = match read_mapping_table(table_path) {
Ok(table) => table,
Err(err) => return Err(err.to_string()),
};
Some(mapping)
}
None => None,
};
let table_1person = match mapping_1person_path {
Some(ref table_path) => {
let mapping = match read_mapping_table(table_path) {
Ok(table) => table,
Err(err) => return Err(err.to_string()),
};
Some(mapping)
}
None => None,
};

let log_level = match log_level {
Some(level) => match level.as_str() {
"trace" | "debug" | "info" | "warn" | "error" => level,
_ => "error".to_owned(),
},
None => "error".to_owned(),
};
let table = try_get_mapping_table!(mapping_path);
let table_1person = try_get_mapping_table!(mapping_1person_path);

let log_level = log_level
.as_deref()
.and_then(|level| match level {
"trace" | "debug" | "info" | "warn" | "error" => Some(level),
unknown_level => {
log::warn!("unknown log level {}. fallback to error", unknown_level);
None
}
})
.unwrap_or("error");

log::debug!("src: {}", dar_mod_folder);
log::debug!("dist: {:?}", dist);
log::debug!("mod_name: {:?}", mod_name);
log::debug!("mod_author: {:?}", mod_author);
log::debug!("table path: {:?}", mapping_path.as_ref());
log::debug!("1st person table path: {:?}", mapping_1person_path.as_ref());
log::debug!("log level: {:?}", log_level.as_str());
log::debug!("log level: {:?}", log_level);

match convert_dar_to_oar(
dar_mod_folder,
Expand All @@ -61,6 +70,6 @@ pub fn convert_dar2oar(
table_1person,
) {
Ok(_) => Ok(()),
Err(err) => return Err(err.to_string()),
Err(err) => bail!(err),
}
}

0 comments on commit 564598c

Please sign in to comment.