Skip to content

Commit

Permalink
feat(backend): add mapping path error
Browse files Browse the repository at this point in the history
  • Loading branch information
SARDONYX-sard committed Jan 16, 2024
1 parent 881a929 commit 5ff1c7b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 37 deletions.
12 changes: 0 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions dar2oar_core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub enum ConvertError {
NeverConverted,
#[error("Not found file name")]
NotFoundFileName,
#[error("Could not find the mapping table file \"{0}\".")]
NotFoundSpecifiedMappingTable(String),
#[error("This is not valid utf8")]
InvalidUtf8,
#[error("Incomplete conversion")]
Expand Down
9 changes: 8 additions & 1 deletion dar2oar_core/src/fs/mapping_table.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::Result;
use crate::error::{ConvertError, Result};
use std::collections::HashMap;
use std::path::Path;
use tokio::{fs::File, io::AsyncReadExt};
Expand All @@ -15,6 +15,13 @@ pub async fn get_mapping_table(

/// Try to read mapping table from path
pub async fn read_mapping_table(table_path: impl AsRef<Path>) -> Result<HashMap<String, String>> {
if !table_path.as_ref().exists() {
return Err(ConvertError::NotFoundSpecifiedMappingTable(format!(
"{:?}",
table_path.as_ref()
)));
};

let mut file_contents = String::new();
File::open(table_path)
.await?
Expand Down
1 change: 0 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ dist = false # To run CI and build separately from CLI (cargo dist)
tauri-build = { version = "1.4.0", features = [] }

[dependencies]
async-trait = "0.1.74"
anyhow = { version = "1.0.75", features = ["backtrace"] }
chrono = "0.4.31"
dar2oar_core = { path = "../dar2oar_core" }
Expand Down
26 changes: 17 additions & 9 deletions src-tauri/src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::convert_option::{AsyncFrom, GuiConverterOptions};
use dar2oar_core::{convert_dar_to_oar, remove_oar, unhide_dar, Closure, ConvertOptions};
use crate::convert_option::GuiConverterOptions;
use dar2oar_core::{convert_dar_to_oar, remove_oar, unhide_dar, Closure};
use std::time::Instant;
use tauri::Window;

Expand Down Expand Up @@ -27,6 +27,18 @@ macro_rules! time {
}};
}

/// Cast the conversion options in the GUI and perform the conversion.
macro_rules! dar_to_oar {
($options:ident, $sender:expr) => {
convert_dar_to_oar(
GuiConverterOptions::to_convert_options($options)
.await
.or_else(|err| bail!(err))?,
$sender,
)
};
}

/// # Progress report for progress bar
///
/// - First: number of files/dirs explored
Expand All @@ -51,20 +63,16 @@ macro_rules! sender {

#[tauri::command]
pub(crate) async fn convert_dar2oar(options: GuiConverterOptions) -> Result<(), String> {
let config = ConvertOptions::async_from(options).await;
time!("Conversion", convert_dar_to_oar(config, Closure::default))
time!("Conversion", dar_to_oar!(options, Closure::default))
}

#[tauri::command]
pub(crate) async fn convert_dar2oar_with_progress(
window: Window,
options: GuiConverterOptions,
) -> Result<(), String> {
let config = ConvertOptions::async_from(options).await;
time!(
"Conversion with progress",
convert_dar_to_oar(config, sender!(window))
)
let sender = sender!(window);
time!("Conversion with progress", dar_to_oar!(options, sender))
}

#[tauri::command]
Expand Down
33 changes: 19 additions & 14 deletions src-tauri/src/convert_option.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use dar2oar_core::{get_mapping_table, ConvertOptions};
use dar2oar_core::error::Result;
use dar2oar_core::{read_mapping_table, ConvertOptions};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -14,15 +15,10 @@ pub(crate) struct GuiConverterOptions {
pub(crate) hide_dar: Option<bool>,
}

#[async_trait::async_trait]
pub(crate) trait AsyncFrom<T> {
async fn async_from(options: T) -> Self;
}

#[async_trait::async_trait]
impl AsyncFrom<GuiConverterOptions> for ConvertOptions {
async fn async_from(options: GuiConverterOptions) -> Self {
let GuiConverterOptions {
impl GuiConverterOptions {
/// Cast to [`ConvertOptions`]
pub(crate) async fn to_convert_options(options: Self) -> Result<ConvertOptions> {
let Self {
dar_dir,
oar_dir,
mod_name,
Expand All @@ -33,15 +29,24 @@ impl AsyncFrom<GuiConverterOptions> for ConvertOptions {
hide_dar,
} = options;

Self {
let section_table = match mapping_path {
Some(path) => Some(read_mapping_table(path).await?),
None => None,
};
let section_1person_table = match mapping_1person_path {
Some(path) => Some(read_mapping_table(path).await?),
None => None,
};

Ok(ConvertOptions {
dar_dir,
oar_dir,
mod_name,
author,
section_table: get_mapping_table(mapping_path).await,
section_1person_table: get_mapping_table(mapping_1person_path).await,
section_table,
section_1person_table,
run_parallel: run_parallel.unwrap_or(false),
hide_dar: hide_dar.unwrap_or(false),
}
})
}
}

0 comments on commit 5ff1c7b

Please sign in to comment.