Skip to content

Commit 07cfd45

Browse files
committed
feat: optimize performance
1 parent a3dc050 commit 07cfd45

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

example-rust/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example-rust/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2018"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10+
anyhow = "1.0.53"
1011
futures = "0.3.19"
1112
glob = "0.3.0"
1213
itertools = "0.10.3"
@@ -16,3 +17,6 @@ once_cell = "1.9.0"
1617
regex = "1.5.4"
1718
tokio = { version = "1.16.1", features = ["full"] }
1819
yaml-rust = "0.4.5"
20+
21+
[profile.release]
22+
debug = 1

example-rust/src/main.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@ fn get_unique_token(src: &str) -> Vec<&str> {
3131
data
3232
}
3333

34-
async fn get_meta_label_from_file(path: &Path) -> tokio::io::Result<Option<String>> {
34+
async fn get_filename_from_meta(path: &Path) -> anyhow::Result<String> {
3535
let yaml = fs::read_to_string(path).await?;
36-
let docs = YamlLoader::load_from_str(&yaml).unwrap();
36+
let docs = YamlLoader::load_from_str(&yaml)?;
3737
let meta = &docs[0];
3838

39-
Ok(meta["label"].as_str().map(|s| s.to_string()))
39+
let label = meta["label"].as_str().ok_or_else(|| anyhow::anyhow!("label not found"))?;
40+
41+
Ok(format!(
42+
"{}/extracted-words-for-{}.txt",
43+
OUTDIR,
44+
label
45+
))
4046
}
4147

4248
#[tokio::main]
@@ -60,21 +66,14 @@ async fn main() -> std::io::Result<()> {
6066
tokens.join("\n")
6167
};
6268

63-
let outfile_submission = async {
64-
let meta_label = get_meta_label_from_file(&yaml_path)
69+
let filename_submission = async {
70+
get_filename_from_meta(&yaml_path)
6571
.await
6672
.expect("should be existed")
67-
.expect("should be Some");
68-
69-
format!(
70-
"{}/extracted-words-for-{}.txt",
71-
OUTDIR,
72-
meta_label
73-
)
7473
};
7574

76-
let (tokens, outfile, _) = tokio::join!(read_text_file_submission, outfile_submission, outdir_submission,);
77-
fs::write(outfile, tokens).await.expect("failed to write");
75+
let (tokens, filename, _) = tokio::join!(read_text_file_submission, filename_submission, outdir_submission,);
76+
fs::write(filename, tokens).await.expect("failed to write");
7877
});
7978

8079
futures::future::join_all(submissions).await;

0 commit comments

Comments
 (0)