Skip to content

Commit 9293a37

Browse files
committed
Use anyhow
1 parent f22b512 commit 9293a37

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

i18n-helpers/src/bin/mdbook-i18n.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::{anyhow, Result};
12
use mdbook::renderer::RenderContext;
23
use serde::Deserialize;
34
use std::collections::BTreeMap;
@@ -27,30 +28,29 @@ struct I18nConfiguration {
2728
/// Whether to translate all languages or just the selected language, defaults to false.
2829
#[serde(default)]
2930
translate_all_languages: bool,
30-
/// Whether to move the translations to the html directory, defaults to false.
31+
/// Whether to move the translations to their renderer's directory, defaults to false.
3132
///
3233
/// By default, translations' output will live in `book/i18n/<language>/<renderer>`.
3334
/// For all renderers in this list, we will move individual translations to `book/<renderer>/<language>`.
3435
#[serde(default)]
3536
move_translations_directories: Vec<String>,
3637
}
3738

38-
fn main() {
39+
fn main() -> Result<()> {
3940
let mut stdin = io::stdin();
4041

4142
// Get the configs
42-
let ctx = RenderContext::from_json(&mut stdin).unwrap();
43+
let ctx = RenderContext::from_json(&mut stdin)?;
4344
let i18n_config: I18nConfiguration = ctx
4445
.config
45-
.get_deserialized_opt("output.i18n")
46-
.unwrap()
47-
.unwrap();
46+
.get_deserialized_opt("output.i18n")?
47+
.ok_or_else(|| anyhow!("No output.i18n config in book.toml"))?;
4848

4949
if !i18n_config.translate_all_languages {
50-
return;
50+
return Ok(());
5151
}
5252

53-
let mut mdbook = mdbook::MDBook::load(&ctx.root).expect("Failed to load book");
53+
let mut mdbook = mdbook::MDBook::load(&ctx.root)?;
5454
// Overwrite with current values from stdin. This is necessary because mdbook will add data to the config.
5555
mdbook.book = ctx.book.clone();
5656
mdbook.config = ctx.config.clone();
@@ -59,11 +59,11 @@ fn main() {
5959
let book_config = mdbook
6060
.config
6161
.get_mut("output.i18n")
62-
.expect("No output.i18n config in book.toml");
62+
.ok_or_else(|| anyhow!("No output.i18n config in book.toml"))?;
6363
// Set translate_all_languages to false for nested builds to prevent infinite recursion.
6464
book_config
6565
.as_table_mut()
66-
.expect("output.i18n config in book.toml is not a table")
66+
.ok_or_else(|| anyhow!("output.i18n config in book.toml is not a table"))?
6767
.insert(String::from("translate_all_languages"), false.into());
6868

6969
let output_directory = ctx.destination;
@@ -74,7 +74,7 @@ fn main() {
7474
if Some(language) == ctx.config.book.language.as_ref() {
7575
continue;
7676
}
77-
if default_language == Some(language) {
77+
if default_language.as_ref() == Some(language) {
7878
continue;
7979
}
8080
let translation_path = output_directory.join(language);
@@ -83,23 +83,23 @@ fn main() {
8383
mdbook.config.book.language = Some(language.clone());
8484
mdbook.config.book.multilingual = true;
8585
mdbook.config.build.build_dir = translation_path;
86-
mdbook
87-
.build()
88-
.unwrap_or_else(|_| panic!("Failed to build translation for language: {}", language));
86+
mdbook.build()?;
8987
for renderer in &i18n_config.move_translations_directories {
90-
std::fs::create_dir_all(output_directory.parent().unwrap().join(renderer))
91-
.unwrap_or_else(|_| panic!("Failed to create html directory in output directory"));
88+
std::fs::create_dir_all(
89+
output_directory
90+
.parent()
91+
.ok_or_else(|| anyhow!("Failed to retrieve parent directory"))?
92+
.join(renderer),
93+
)?;
9294
std::fs::rename(
9395
output_directory.join(language).join(renderer),
9496
output_directory
9597
.parent()
96-
.unwrap()
98+
.ok_or_else(|| anyhow!("Failed to retrieve parent directory"))?
9799
.join(renderer)
98100
.join(language),
99-
)
100-
.unwrap_or_else(|_| {
101-
panic!("Failed to move translation for language {language} to output directory")
102-
});
101+
)?;
103102
}
104103
}
104+
Ok(())
105105
}

0 commit comments

Comments
 (0)