Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change sender to async fn #20

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Cargo.lock

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

3 changes: 1 addition & 2 deletions dar2oar_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ path = "./src/main.rs"
anyhow = { version = "1.0.75", features = ["backtrace"] }
clap = { version = "4.4.4", features = ["derive"] } # For CLI
dar2oar_core = { path = "../dar2oar_core" }
log = "0.4.20" # Logger
tokio = { version = "1.33.0", features = [
"fs",
"rt",
"rt-multi-thread",
"macros",
] }
tracing = "0.1.40"
tracing = "0.1.40" # Logger
tracing-subscriber = "0.3.17"

[dev-dependencies]
Expand Down
24 changes: 16 additions & 8 deletions dar2oar_cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::{arg, Parser};
use dar2oar_core::{
convert_dar_to_oar,
fs::{parallel, ConvertOptions},
fs::{async_closure::AsyncClosure, parallel, ConvertOptions},
read_mapping_table,
};
use std::fs::File;
Expand Down Expand Up @@ -71,13 +71,21 @@ pub async fn run_cli(args: Args) -> anyhow::Result<()> {
section_table: read_table!(args.mapping_file),
section_1person_table: read_table!(args.mapping_1person_file),
hide_dar: args.hide_dar,
..Default::default()
};

let msg = match args.run_parallel {
true => parallel::convert_dar_to_oar(config).await,
false => convert_dar_to_oar(config).await,
}?;
log::debug!("{}", msg);
Ok(())
let res = match args.run_parallel {
true => parallel::convert_dar_to_oar(config, AsyncClosure::default).await,
false => convert_dar_to_oar(config, AsyncClosure::default).await,
};

match res {
Ok(msg) => {
tracing::info!("{}", msg);
Ok(())
}
Err(err) => {
tracing::error!("{}", err);
anyhow::bail!("{}", err)
}
}
}
1 change: 1 addition & 0 deletions dar2oar_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ criterion = { version = "0.5.1", features = [
"async_tokio",
"html_reports",
] }
once_cell = "1.18.0"
tracing-appender = "0.2"
pretty_assertions = "1.4.0"
tracing-subscriber = "0.3.17"
Expand Down
27 changes: 17 additions & 10 deletions dar2oar_core/benches/convert_n_thread.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use criterion::async_executor::FuturesExecutor;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use dar2oar_core::fs::async_closure::AsyncClosure;
use dar2oar_core::{
convert_dar_to_oar,
fs::{parallel, ConvertOptions},
Expand All @@ -24,11 +25,14 @@ fn criterion_benchmark(c: &mut Criterion) {
}
let mapping = read_mapping_table(TABLE_PATH).await.unwrap();

parallel::convert_dar_to_oar(black_box(ConvertOptions {
dar_dir: TARGET,
section_table: Some(mapping),
..Default::default()
}))
parallel::convert_dar_to_oar(
black_box(ConvertOptions {
dar_dir: TARGET,
section_table: Some(mapping),
..Default::default()
}),
AsyncClosure::default,
)
.await
})
});
Expand All @@ -40,11 +44,14 @@ fn criterion_benchmark(c: &mut Criterion) {
}
let mapping = read_mapping_table(TABLE_PATH).await.unwrap();

convert_dar_to_oar(black_box(ConvertOptions {
dar_dir: TARGET,
section_table: Some(mapping),
..Default::default()
}))
convert_dar_to_oar(
black_box(ConvertOptions {
dar_dir: TARGET,
section_table: Some(mapping),
..Default::default()
}),
AsyncClosure::default,
)
.await
})
});
Expand Down
8 changes: 4 additions & 4 deletions dar2oar_core/src/conditions/namespace_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use serde::{Deserialize, Serialize};

/// name space config.json
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct MainConfig {
pub struct MainConfig<'a> {
#[serde(default)]
pub name: String,
pub name: &'a str,
#[serde(default)]
pub description: String,
pub description: &'a str,
#[serde(default)]
pub author: String,
pub author: &'a str,
}
27 changes: 27 additions & 0 deletions dar2oar_core/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#[derive(Debug, thiserror::Error)]
pub enum ConvertError {
#[error("Failed to write section config target: {0}")]
FailedWriteSectionConfig(String),
#[error("Neither 1st or 3rd person \"DynamicAnimationReplacer.mohidden\" found.")]
NotFoundUnhideTarget,
#[error("Not found \"OpenAnimationReplacer\" directory")]
NotFoundOarDir,
#[error("Not found \"DynamicAnimationReplacer\" directory")]
NotFoundDarDir,
#[error("Not found file name")]
NotFoundFileName,
#[error("This is not valid utf8")]
InvalidUtf8,
#[error(transparent)]
AnyhowError(#[from] anyhow::Error),
/// Convert json error.
#[error(transparent)]
JsonError(#[from] serde_json::Error),
#[error(transparent)]
ParseIntError(#[from] core::num::ParseIntError),
/// Represents all other cases of `std::io::Error`.
#[error(transparent)]
IOError(#[from] std::io::Error),
}

pub type Result<T, Error = ConvertError> = core::result::Result<T, Error>;
4 changes: 4 additions & 0 deletions dar2oar_core/src/fs/async_closure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub struct AsyncClosure;
impl AsyncClosure {
pub async fn default(_: usize) {}
}
13 changes: 5 additions & 8 deletions dar2oar_core/src/fs/mapping_table.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use anyhow::bail;
use std::collections::HashMap;
use std::path::Path;
use tokio::{fs::File, io::AsyncReadExt};
Expand All @@ -7,13 +6,11 @@ pub async fn read_mapping_table(
table_path: impl AsRef<Path>,
) -> anyhow::Result<HashMap<String, String>> {
let mut file_contents = String::new();
match File::open(table_path).await {
Ok(mut file) => match file.read_to_string(&mut file_contents).await {
Ok(_) => Ok(parse_mapping_table(&file_contents)),
Err(e) => bail!("Error reading file: {}", e),
},
Err(e) => bail!("Error opening file: {}", e),
}
File::open(table_path)
.await?
.read_to_string(&mut file_contents)
.await?;
Ok(parse_mapping_table(&file_contents))
}

fn parse_mapping_table(table: &str) -> HashMap<String, String> {
Expand Down
Loading
Loading