Skip to content

Commit 0efcdd7

Browse files
committed
use nonblocking filesystem in async functions
1 parent 3fd0f75 commit 0efcdd7

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/builder/builder.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clang::{Clang, Entity};
22
use indicatif::ProgressBar;
33
use serde_json::json;
4-
use std::{collections::HashMap, fs, sync::Arc};
4+
use std::{collections::HashMap, sync::Arc};
55
use strfmt::strfmt;
66
use tokio::task::JoinHandle;
77

@@ -56,7 +56,7 @@ impl<'e> Builder<'e> {
5656
fn setup(mut self) -> Result<Self, String> {
5757
// copy & minify CSS
5858
for script in &self.config.scripts.css {
59-
fs::write(
59+
std::fs::write(
6060
self.config.output_dir.join(&script.name),
6161
minify_css(script.content.to_string())?,
6262
)
@@ -65,7 +65,7 @@ impl<'e> Builder<'e> {
6565

6666
// transpile, minify, and copy JS
6767
for script in &self.config.scripts.js {
68-
fs::write(
68+
std::fs::write(
6969
self.config.output_dir.join(&script.name),
7070
minify_js(script.content.to_string())?,
7171
)
@@ -74,7 +74,7 @@ impl<'e> Builder<'e> {
7474

7575
// copy icon
7676
if let Some(ref icon) = self.config.project.icon {
77-
fs::copy(
77+
std::fs::copy(
7878
self.config.input_dir.join(icon),
7979
self.config.output_dir.join("icon.png"),
8080
)
@@ -102,14 +102,14 @@ impl<'e> Builder<'e> {
102102
asset.strip_prefix(&tutorials.dir).unwrap_or(asset),
103103
);
104104
if let Some(parent) = output.parent() {
105-
fs::create_dir_all(self.config.output_dir.join(parent)).map_err(|e| {
105+
std::fs::create_dir_all(self.config.output_dir.join(parent)).map_err(|e| {
106106
format!(
107107
"Unable to create asset directory '{}': {e}",
108108
output.to_string_lossy()
109109
)
110110
})?;
111111
}
112-
fs::copy(self.config.input_dir.join(asset), output).map_err(|e| {
112+
std::fs::copy(self.config.input_dir.join(asset), output).map_err(|e| {
113113
format!(
114114
"Unable to copy asset '{}': {e}, {}",
115115
asset.to_string_lossy(),
@@ -192,37 +192,41 @@ impl<'e> Builder<'e> {
192192
let output_dir = config.output_dir.join(target_url.to_pathbuf());
193193

194194
// Make sure output directory exists
195-
fs::create_dir_all(&output_dir)
195+
tokio::fs::create_dir_all(&output_dir)
196+
.await
196197
.map_err(|e| format!("Unable to create directory for {target_url}: {e}"))?;
197198

198199
// Save metadata to a file
199-
fs::write(
200+
tokio::fs::write(
200201
output_dir.join("metadata.json"),
201202
format!(
202203
r#"{{"title": "{}", "description": "{}"}}"#,
203204
title, description,
204205
),
205206
)
207+
.await
206208
.map_err(|e| format!("Unable to save metadata for {target_url}: {e}"))?;
207209

208210
// Write the plain content output
209-
fs::write(
211+
tokio::fs::write(
210212
config
211213
.output_dir
212214
.join(target_url.to_pathbuf())
213215
.join("content.html"),
214216
content,
215217
)
218+
.await
216219
.map_err(|e| format!("Unable to save {target_url}: {e}"))?;
217220

218221
// Write the full page
219-
fs::write(
222+
tokio::fs::write(
220223
config
221224
.output_dir
222225
.join(target_url.to_pathbuf())
223226
.join("index.html"),
224227
page,
225228
)
229+
.await
226230
.map_err(|e| format!("Unable to save {target_url}: {e}"))?;
227231

228232
Ok(target_url)
@@ -277,7 +281,7 @@ impl<'e> Builder<'e> {
277281
pbar.set_message("Generating metadata".to_string());
278282
}
279283

280-
fs::write(
284+
tokio::fs::write(
281285
self.config.output_dir.join("functions.json"),
282286
serde_json::to_string(
283287
&self
@@ -296,12 +300,14 @@ impl<'e> Builder<'e> {
296300
)
297301
.map_err(|e| format!("Unable to save metadata {e}"))?,
298302
)
303+
.await
299304
.map_err(|e| format!("Unable to save metadata {e}"))?;
300305

301-
fs::write(
306+
tokio::fs::write(
302307
self.config.output_dir.join("nav.json"),
303308
serde_json::to_string(&self.build_nav_metadata()).unwrap(),
304309
)
310+
.await
305311
.unwrap();
306312

307313
Ok(())

0 commit comments

Comments
 (0)