Skip to content

Commit c8db0c8

Browse files
authored
Merge pull request #1889 from mgeisler/simplify-and-then
Simplify the use of `Option::and_then`
2 parents 8cdb8d0 + 3958260 commit c8db0c8

File tree

3 files changed

+35
-39
lines changed

3 files changed

+35
-39
lines changed

src/cmd/serve.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
8989
let input_404 = book
9090
.config
9191
.get("output.html.input-404")
92-
.map(toml::Value::as_str)
93-
.and_then(std::convert::identity) // flatten
92+
.and_then(toml::Value::as_str)
9493
.map(ToString::to_string);
9594
let file_404 = get_404_output_file(&input_404);
9695

src/renderer/html_handlebars/helpers/navigation.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,12 @@ fn render(
148148

149149
trace!("Render template");
150150

151-
_h.template()
152-
.ok_or_else(|| RenderError::new("Error with the handlebars template"))
153-
.and_then(|t| {
154-
let local_ctx = Context::wraps(&context)?;
155-
let mut local_rc = rc.clone();
156-
t.render(r, &local_ctx, &mut local_rc, out)
157-
})?;
158-
159-
Ok(())
151+
let t = _h
152+
.template()
153+
.ok_or_else(|| RenderError::new("Error with the handlebars template"))?;
154+
let local_ctx = Context::wraps(&context)?;
155+
let mut local_rc = rc.clone();
156+
t.render(r, &local_ctx, &mut local_rc, out)
160157
}
161158

162159
pub fn previous(

src/renderer/html_handlebars/helpers/toc.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -117,35 +117,35 @@ impl HelperDef for RenderToc {
117117
}
118118

119119
// Link
120-
let path_exists = if let Some(path) =
121-
item.get("path")
122-
.and_then(|p| if p.is_empty() { None } else { Some(p) })
123-
{
124-
out.write("<a href=\"")?;
125-
126-
let tmp = Path::new(item.get("path").expect("Error: path should be Some(_)"))
127-
.with_extension("html")
128-
.to_str()
129-
.unwrap()
130-
// Hack for windows who tends to use `\` as separator instead of `/`
131-
.replace('\\', "/");
132-
133-
// Add link
134-
out.write(&utils::fs::path_to_root(&current_path))?;
135-
out.write(&tmp)?;
136-
out.write("\"")?;
137-
138-
if path == &current_path || is_first_chapter {
139-
is_first_chapter = false;
140-
out.write(" class=\"active\"")?;
141-
}
120+
let path_exists: bool;
121+
match item.get("path") {
122+
Some(path) if !path.is_empty() => {
123+
out.write("<a href=\"")?;
124+
let tmp = Path::new(path)
125+
.with_extension("html")
126+
.to_str()
127+
.unwrap()
128+
// Hack for windows who tends to use `\` as separator instead of `/`
129+
.replace('\\', "/");
130+
131+
// Add link
132+
out.write(&utils::fs::path_to_root(&current_path))?;
133+
out.write(&tmp)?;
134+
out.write("\"")?;
135+
136+
if path == &current_path || is_first_chapter {
137+
is_first_chapter = false;
138+
out.write(" class=\"active\"")?;
139+
}
142140

143-
out.write(">")?;
144-
true
145-
} else {
146-
out.write("<div>")?;
147-
false
148-
};
141+
out.write(">")?;
142+
path_exists = true;
143+
}
144+
_ => {
145+
out.write("<div>")?;
146+
path_exists = false;
147+
}
148+
}
149149

150150
if !self.no_section_label {
151151
// Section does not necessarily exist

0 commit comments

Comments
 (0)